fixed signup

This commit is contained in:
ACoolName 2024-05-18 21:43:25 +03:00
parent f2f0e18e6c
commit 46675c7b2f
2 changed files with 41 additions and 10 deletions

View File

@ -12,6 +12,7 @@ import (
"github.com/golang-jwt/jwt" "github.com/golang-jwt/jwt"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
) )
@ -33,9 +34,9 @@ type AuthClaims struct {
} }
type InviteToken struct { type InviteToken struct {
Email string `bson:"Email"` Email string `bson:"Email"`
Permissions []models.Permission `bson:"Permissions"` Permissions models.Permission `bson:"Permissions"`
Token string `bson:"Token"` Token string `bson:"Token"`
} }
func signToken(token TokenInfo) (string, error) { func signToken(token TokenInfo) (string, error) {
@ -102,20 +103,50 @@ type SignUpRequest struct {
} }
func (con Connection) signUp(c *gin.Context) { func (con Connection) signUp(c *gin.Context) {
var token TokenInfo var request SignUpRequest
err := json.NewDecoder(c.Request.Body).Decode(&token) err := json.NewDecoder(c.Request.Body).Decode(&request)
if err != nil { if err != nil {
c.AbortWithError(500, err) c.AbortWithError(500, err)
} }
signedToken, err := signToken(token) var token InviteToken
err = con.connection.Database("Backend").Collection("Tokens").FindOne(
context.TODO(),
bson.D{{}},
options.FindOne(),
).Decode(&token)
if err != nil { if err != nil {
c.AbortWithError(500, err) c.AbortWithError(500, err)
return
}
if token.Token == "" {
c.AbortWithStatusJSON(403, "PermissionDenied")
return
} }
c.SetCookie("auth", signedToken, -1, "", "", false, false) hashedPass, err := hashPassword(request.Password)
c.IndentedJSON(http.StatusOK, signedToken) if err != nil {
c.AbortWithError(500, err)
return
}
_, err = con.connection.Database("Backend").Collection("Users").InsertOne(context.TODO(), &models.User{
Username: request.Username,
HashedPass: hashedPass,
Permissions: token.Permissions,
MaxOwnedServers: 5,
Email: token.Email,
}, &options.InsertOneOptions{})
if err != nil {
c.AbortWithError(500, err)
return
}
con.signIn(c)
} }
type SignInRequest struct { type SignInRequest struct {

View File

@ -35,8 +35,8 @@ func (con Connection) GetUsers(c *gin.Context) {
} }
type InviteUser struct { type InviteUser struct {
Email string `json:"Email"` Email string `json:"Email"`
Permissions []models.Permission `json:"Permissions"` Permissions models.Permission `json:"Permissions"`
} }
func (con Connection) InviteUser(c *gin.Context) { func (con Connection) InviteUser(c *gin.Context) {