diff options
author | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2025-10-12 00:05:30 +0530 |
---|---|---|
committer | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2025-10-12 00:05:30 +0530 |
commit | a356803594ab36fa69e7dbcbd79261d8b46f4262 (patch) | |
tree | 0146efe4c52b1c65c1967ab1f412306c410c10d4 /auth/controller.go | |
parent | 193be465b21838d2796fafbe1c5d9854038a3f8c (diff) |
removed useless user fields and functions, added rolesv0.19.0
Diffstat (limited to 'auth/controller.go')
-rw-r--r-- | auth/controller.go | 36 |
1 files changed, 19 insertions, 17 deletions
diff --git a/auth/controller.go b/auth/controller.go index 961518a..8de7370 100644 --- a/auth/controller.go +++ b/auth/controller.go @@ -1,5 +1,5 @@ /* openbills - Server for web based Libre Billing Software - * Copyright (C) 2023 Vidhu Kant Sharma <vidhukant@vidhukant.com> + * Copyright (C) 2023-2025 Vidhu Kant Sharma <vidhukant@vidhukant.com> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -39,36 +39,37 @@ func init() { } func handleSignUp (ctx *gin.Context) { - var user user.User - ctx.Bind(&user) + var u user.User + ctx.Bind(&u) var err error // hash password var bytes []byte - bytes, err = bcrypt.GenerateFromPassword([]byte(user.Password), 14) + bytes, err = bcrypt.GenerateFromPassword([]byte(u.Password), 14) if err != nil { // TODO: handle potential errors ctx.Error(err) ctx.Abort() return } - user.Password = string(bytes) + u.Password = string(bytes) + + // for now everyone's an admin + // TODO: fix this shit + u.Roles = []user.Role{ + {0, 0, "admin"}, + } - err = user.Create() + err = u.Create() if err != nil { ctx.Error(err) ctx.Abort() return } - // remove password hash from response - user.Password = "" - - ctx.JSON(http.StatusOK, gin.H{ - "message": "success", - "data": user, - }) + // TODO: email verification and shit before this + ctx.JSON(http.StatusOK, nil) } func handleSignIn (ctx *gin.Context) { @@ -93,6 +94,7 @@ func handleSignIn (ctx *gin.Context) { ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Minute * 2)), }, u.ID, + user.RolesToStringList(u.Roles), }, ).SignedString(AUTH_KEY) if err != nil { @@ -125,7 +127,6 @@ func handleSignIn (ctx *gin.Context) { ctx.JSON(http.StatusOK, gin.H{ "auth_token": authToken, "refresh_token": refreshToken, - "message": "success", "data": u, }) } @@ -147,9 +148,10 @@ func handleRefresh (ctx *gin.Context) { // check token version var u user.User - err := user.GetUser(&u, claims.UserID) + err := user.GetUserById(&u, claims.UserID) if err != nil { if err == errors.ErrNotFound { + // user doesn't exist ctx.Error(errors.ErrUnauthorized) ctx.Abort() return @@ -184,7 +186,8 @@ func handleRefresh (ctx *gin.Context) { IssuedAt: jwt.NewNumericDate(time.Now()), ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Minute * 2)), }, - claims.UserID, + u.ID, + user.RolesToStringList(u.Roles), }, ).SignedString(AUTH_KEY) if err != nil { @@ -196,6 +199,5 @@ func handleRefresh (ctx *gin.Context) { ctx.JSON(http.StatusOK, gin.H{ "auth_token": authToken, - "message": "success", }) } |