fixed signup
This commit is contained in:
parent
f2f0e18e6c
commit
46675c7b2f
43
auth/auth.go
43
auth/auth.go
@ -12,6 +12,7 @@ import (
|
||||
"github.com/golang-jwt/jwt"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
@ -34,7 +35,7 @@ type AuthClaims struct {
|
||||
|
||||
type InviteToken struct {
|
||||
Email string `bson:"Email"`
|
||||
Permissions []models.Permission `bson:"Permissions"`
|
||||
Permissions models.Permission `bson:"Permissions"`
|
||||
Token string `bson:"Token"`
|
||||
}
|
||||
|
||||
@ -102,20 +103,50 @@ type SignUpRequest struct {
|
||||
}
|
||||
|
||||
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 {
|
||||
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 {
|
||||
c.AbortWithError(500, err)
|
||||
return
|
||||
}
|
||||
if token.Token == "" {
|
||||
c.AbortWithStatusJSON(403, "PermissionDenied")
|
||||
return
|
||||
}
|
||||
|
||||
c.SetCookie("auth", signedToken, -1, "", "", false, false)
|
||||
c.IndentedJSON(http.StatusOK, signedToken)
|
||||
hashedPass, err := hashPassword(request.Password)
|
||||
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 {
|
||||
|
@ -36,7 +36,7 @@ func (con Connection) GetUsers(c *gin.Context) {
|
||||
|
||||
type InviteUser struct {
|
||||
Email string `json:"Email"`
|
||||
Permissions []models.Permission `json:"Permissions"`
|
||||
Permissions models.Permission `json:"Permissions"`
|
||||
}
|
||||
|
||||
func (con Connection) InviteUser(c *gin.Context) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user