|
1 |
| -package controllers |
2 |
| - |
3 |
| -import ( |
4 |
| - "net/http" |
5 |
| - "time" |
6 |
| - |
7 |
| - "github.com/gin-gonic/gin" |
8 |
| - db "github.com/wpcodevo/golang-postgresql-grpc/db/sqlc" |
9 |
| - "github.com/wpcodevo/golang-postgresql-grpc/utils" |
10 |
| -) |
11 |
| - |
12 |
| -type AuthController struct { |
13 |
| - db *db.Queries |
14 |
| -} |
15 |
| - |
16 |
| -func NewAuthController(db *db.Queries) *AuthController { |
17 |
| - return &AuthController{db} |
18 |
| -} |
19 |
| - |
20 |
| -func (ac *AuthController) SignUpUser(ctx *gin.Context) { |
21 |
| - var credentials *db.User |
22 |
| - |
23 |
| - if err := ctx.ShouldBindJSON(&credentials); err != nil { |
24 |
| - ctx.JSON(http.StatusBadRequest, err.Error()) |
25 |
| - return |
26 |
| - } |
27 |
| - |
28 |
| - hashedPassword := utils.HashPassword(credentials.Password) |
29 |
| - |
30 |
| - args := &db.CreateUserParams{ |
31 |
| - Name: credentials.Name, |
32 |
| - Email: credentials.Email, |
33 |
| - Password: hashedPassword, |
34 |
| - Photo: "default.jpeg", |
35 |
| - Verified: true, |
36 |
| - Role: "user", |
37 |
| - UpdatedAt: time.Now(), |
38 |
| - } |
39 |
| - |
40 |
| - user, err := ac.db.CreateUser(ctx, *args) |
41 |
| - |
42 |
| - if err != nil { |
43 |
| - ctx.JSON(http.StatusBadGateway, err.Error()) |
44 |
| - return |
45 |
| - } |
46 |
| - |
47 |
| - ctx.JSON(http.StatusCreated, gin.H{"status": "success", "data": gin.H{"user": user}}) |
48 |
| -} |
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/gin-gonic/gin" |
| 10 | + "github.com/google/uuid" |
| 11 | + "github.com/wpcodevo/golang-postgresql-grpc/config" |
| 12 | + db "github.com/wpcodevo/golang-postgresql-grpc/db/sqlc" |
| 13 | + "github.com/wpcodevo/golang-postgresql-grpc/models" |
| 14 | + "github.com/wpcodevo/golang-postgresql-grpc/utils" |
| 15 | +) |
| 16 | + |
| 17 | +type AuthController struct { |
| 18 | + db *db.Queries |
| 19 | + ctx context.Context |
| 20 | +} |
| 21 | + |
| 22 | +func NewAuthController(db *db.Queries, ctx context.Context) *AuthController { |
| 23 | + return &AuthController{db, ctx} |
| 24 | +} |
| 25 | + |
| 26 | +func (ac *AuthController) SignUpUser(ctx *gin.Context) { |
| 27 | + var credentials *db.CreateUserParams |
| 28 | + |
| 29 | + if err := ctx.ShouldBindJSON(&credentials); err != nil { |
| 30 | + ctx.JSON(http.StatusBadRequest, err.Error()) |
| 31 | + return |
| 32 | + } |
| 33 | + |
| 34 | + hashedPassword := utils.HashPassword(credentials.Password) |
| 35 | + |
| 36 | + args := &db.CreateUserParams{ |
| 37 | + Name: credentials.Name, |
| 38 | + Email: credentials.Email, |
| 39 | + Password: hashedPassword, |
| 40 | + Photo: "default.jpeg", |
| 41 | + Verified: true, |
| 42 | + Role: "user", |
| 43 | + UpdatedAt: time.Now(), |
| 44 | + } |
| 45 | + |
| 46 | + user, err := ac.db.CreateUser(ctx, *args) |
| 47 | + |
| 48 | + if err != nil { |
| 49 | + ctx.JSON(http.StatusBadGateway, err.Error()) |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + userResponse := models.FilteredResponse(&user) |
| 54 | + |
| 55 | + ctx.JSON(http.StatusCreated, gin.H{"status": "success", "data": gin.H{"user": userResponse}}) |
| 56 | +} |
| 57 | + |
| 58 | +func (ac *AuthController) SignInUser(ctx *gin.Context) { |
| 59 | + var credentials *models.SignInInput |
| 60 | + |
| 61 | + if err := ctx.ShouldBindJSON(&credentials); err != nil { |
| 62 | + ctx.JSON(http.StatusBadRequest, gin.H{"status": "fail", "message": err.Error()}) |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + user, err := ac.db.GetUserByEmail(ac.ctx, credentials.Email) |
| 67 | + if err != nil { |
| 68 | + ctx.JSON(http.StatusBadRequest, gin.H{"status": "fail", "message": "Invalid email or password"}) |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + if err := utils.ComparePassword(user.Password, credentials.Password); err != nil { |
| 73 | + ctx.JSON(http.StatusBadRequest, gin.H{"status": "fail", "message": "Invalid email or password"}) |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + config, _ := config.LoadConfig(".") |
| 78 | + |
| 79 | + // Generate Tokens |
| 80 | + access_token, err := utils.CreateToken(config.AccessTokenExpiresIn, user.ID, config.AccessTokenPrivateKey) |
| 81 | + if err != nil { |
| 82 | + ctx.JSON(http.StatusBadRequest, gin.H{"status": "fail", "message": err.Error()}) |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + refresh_token, err := utils.CreateToken(config.RefreshTokenExpiresIn, user.ID, config.RefreshTokenPrivateKey) |
| 87 | + if err != nil { |
| 88 | + ctx.JSON(http.StatusBadRequest, gin.H{"status": "fail", "message": err.Error()}) |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + ctx.SetCookie("access_token", access_token, config.AccessTokenMaxAge*60, "/", "localhost", false, true) |
| 93 | + ctx.SetCookie("refresh_token", refresh_token, config.RefreshTokenMaxAge*60, "/", "localhost", false, true) |
| 94 | + ctx.SetCookie("logged_in", "true", config.AccessTokenMaxAge*60, "/", "localhost", false, false) |
| 95 | + |
| 96 | + ctx.JSON(http.StatusOK, gin.H{"status": "success", "access_token": access_token}) |
| 97 | +} |
| 98 | + |
| 99 | +func (ac *AuthController) RefreshAccessToken(ctx *gin.Context) { |
| 100 | + message := "could not refresh access token" |
| 101 | + |
| 102 | + cookie, err := ctx.Cookie("refresh_token") |
| 103 | + |
| 104 | + if err != nil { |
| 105 | + ctx.AbortWithStatusJSON(http.StatusForbidden, gin.H{"status": "fail", "message": message}) |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + config, _ := config.LoadConfig(".") |
| 110 | + |
| 111 | + sub, err := utils.ValidateToken(cookie, config.RefreshTokenPublicKey) |
| 112 | + if err != nil { |
| 113 | + ctx.AbortWithStatusJSON(http.StatusForbidden, gin.H{"status": "fail", "message": err.Error()}) |
| 114 | + return |
| 115 | + } |
| 116 | + |
| 117 | + user, err := ac.db.GetUserById(ac.ctx, uuid.MustParse(fmt.Sprint(sub))) |
| 118 | + if err != nil { |
| 119 | + ctx.AbortWithStatusJSON(http.StatusForbidden, gin.H{"status": "fail", "message": "the user belonging to this token no logger exists"}) |
| 120 | + return |
| 121 | + } |
| 122 | + |
| 123 | + access_token, err := utils.CreateToken(config.AccessTokenExpiresIn, user.ID, config.AccessTokenPrivateKey) |
| 124 | + if err != nil { |
| 125 | + ctx.AbortWithStatusJSON(http.StatusForbidden, gin.H{"status": "fail", "message": err.Error()}) |
| 126 | + return |
| 127 | + } |
| 128 | + |
| 129 | + ctx.SetCookie("access_token", access_token, config.AccessTokenMaxAge*60, "/", "localhost", false, true) |
| 130 | + ctx.SetCookie("logged_in", "true", config.AccessTokenMaxAge*60, "/", "localhost", false, false) |
| 131 | + |
| 132 | + ctx.JSON(http.StatusOK, gin.H{"status": "success", "access_token": access_token}) |
| 133 | +} |
| 134 | + |
| 135 | +func (ac *AuthController) LogoutUser(ctx *gin.Context) { |
| 136 | + ctx.SetCookie("access_token", "", -1, "/", "localhost", false, true) |
| 137 | + ctx.SetCookie("refresh_token", "", -1, "/", "localhost", false, true) |
| 138 | + ctx.SetCookie("logged_in", "", -1, "/", "localhost", false, true) |
| 139 | + |
| 140 | + ctx.JSON(http.StatusOK, gin.H{"status": "success"}) |
| 141 | +} |
0 commit comments