|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | +"fmt" |
| 5 | +"net/http" |
| 6 | + |
| 7 | +"github.com/gin-gonic/gin" |
| 8 | +"github.com/wpcodevo/golang-mongodb/config" |
| 9 | +"github.com/wpcodevo/golang-mongodb/models" |
| 10 | +"github.com/wpcodevo/golang-mongodb/services" |
| 11 | +"github.com/wpcodevo/golang-mongodb/utils" |
| 12 | +"go.mongodb.org/mongo-driver/mongo" |
| 13 | +) |
| 14 | + |
| 15 | +typeAuthControllerstruct { |
| 16 | +authService services.AuthService |
| 17 | +userService services.UserService |
| 18 | +} |
| 19 | + |
| 20 | +funcNewAuthController(authService services.AuthService,userService services.UserService)AuthController { |
| 21 | +returnAuthController{authService,userService} |
| 22 | +} |
| 23 | + |
| 24 | +func (ac*AuthController)SignUpUser(ctx*gin.Context) { |
| 25 | +varuser*models.SignUpInput |
| 26 | + |
| 27 | +iferr:=ctx.ShouldBindJSON(&user);err!=nil { |
| 28 | +ctx.JSON(http.StatusBadRequest, gin.H{"status":"fail","message":err.Error()}) |
| 29 | +return |
| 30 | +} |
| 31 | + |
| 32 | +ifuser.Password!=user.PasswordConfirm { |
| 33 | +ctx.JSON(http.StatusBadRequest, gin.H{"status":"fail","message":"Passwords do not match"}) |
| 34 | +return |
| 35 | +} |
| 36 | + |
| 37 | +newUser,err:=ac.authService.SignUpUser(user) |
| 38 | + |
| 39 | +iferr!=nil { |
| 40 | +ctx.JSON(http.StatusBadGateway, gin.H{"status":"error","message":err.Error()}) |
| 41 | +return |
| 42 | +} |
| 43 | + |
| 44 | +ctx.JSON(http.StatusCreated, gin.H{"status":"success","data": gin.H{"user":models.FilteredResponse(newUser)}}) |
| 45 | +} |
| 46 | + |
| 47 | +func (ac*AuthController)SignInUser(ctx*gin.Context) { |
| 48 | +varcredentials*models.SignInInput |
| 49 | + |
| 50 | +iferr:=ctx.ShouldBindJSON(&credentials);err!=nil { |
| 51 | +ctx.JSON(http.StatusBadRequest, gin.H{"status":"fail","message":err.Error()}) |
| 52 | +return |
| 53 | +} |
| 54 | + |
| 55 | +user,err:=ac.userService.FindUserByEmail(credentials.Email) |
| 56 | +iferr!=nil { |
| 57 | +iferr==mongo.ErrNoDocuments { |
| 58 | +ctx.JSON(http.StatusBadRequest, gin.H{"status":"fail","message":"Invalid email or password"}) |
| 59 | +return |
| 60 | +} |
| 61 | +ctx.JSON(http.StatusBadRequest, gin.H{"status":"fail","message":err.Error()}) |
| 62 | +return |
| 63 | +} |
| 64 | + |
| 65 | +iferr:=utils.VerifyPassword(user.Password,credentials.Password);err!=nil { |
| 66 | +ctx.JSON(http.StatusBadRequest, gin.H{"status":"fail","message":"Invalid email or Password"}) |
| 67 | +return |
| 68 | +} |
| 69 | + |
| 70 | +config,_:=config.LoadConfig(".") |
| 71 | + |
| 72 | +// Generate Tokens |
| 73 | +access_token,err:=utils.CreateToken(config.AccessTokenExpiresIn,user.ID,config.AccessTokenPrivateKey) |
| 74 | +iferr!=nil { |
| 75 | +ctx.JSON(http.StatusBadRequest, gin.H{"status":"fail","message":err.Error()}) |
| 76 | +return |
| 77 | +} |
| 78 | + |
| 79 | +refresh_token,err:=utils.CreateToken(config.RefreshTokenExpiresIn,user.ID,config.RefreshTokenPrivateKey) |
| 80 | +iferr!=nil { |
| 81 | +ctx.JSON(http.StatusBadRequest, gin.H{"status":"fail","message":err.Error()}) |
| 82 | +return |
| 83 | +} |
| 84 | + |
| 85 | +ctx.SetCookie("access_token",access_token,config.AccessTokenMaxAge*60,"/","localhost",false,true) |
| 86 | +ctx.SetCookie("refresh_token",refresh_token,config.RefreshTokenMaxAge*60,"/","localhost",false,true) |
| 87 | +ctx.SetCookie("logged_in","true",config.AccessTokenMaxAge*60,"/","localhost",false,false) |
| 88 | + |
| 89 | +ctx.JSON(http.StatusOK, gin.H{"status":"success","access_token":access_token}) |
| 90 | +} |
| 91 | + |
| 92 | +func (ac*AuthController)RefreshAccessToken(ctx*gin.Context) { |
| 93 | +message:="could not refresh access token" |
| 94 | + |
| 95 | +cookie,err:=ctx.Cookie("refresh_token") |
| 96 | + |
| 97 | +iferr!=nil { |
| 98 | +ctx.AbortWithStatusJSON(http.StatusForbidden, gin.H{"status":"fail","message":message}) |
| 99 | +return |
| 100 | +} |
| 101 | + |
| 102 | +config,_:=config.LoadConfig(".") |
| 103 | + |
| 104 | +sub,err:=utils.ValidateToken(cookie,config.RefreshTokenPublicKey) |
| 105 | +iferr!=nil { |
| 106 | +ctx.AbortWithStatusJSON(http.StatusForbidden, gin.H{"status":"fail","message":err.Error()}) |
| 107 | +return |
| 108 | +} |
| 109 | + |
| 110 | +user,err:=ac.userService.FindUserById(fmt.Sprint(sub)) |
| 111 | +iferr!=nil { |
| 112 | +ctx.AbortWithStatusJSON(http.StatusForbidden, gin.H{"status":"fail","message":"the user belonging to this token no logger exists"}) |
| 113 | +return |
| 114 | +} |
| 115 | + |
| 116 | +access_token,err:=utils.CreateToken(config.AccessTokenExpiresIn,user.ID,config.AccessTokenPrivateKey) |
| 117 | +iferr!=nil { |
| 118 | +ctx.AbortWithStatusJSON(http.StatusForbidden, gin.H{"status":"fail","message":err.Error()}) |
| 119 | +return |
| 120 | +} |
| 121 | + |
| 122 | +ctx.SetCookie("access_token",access_token,config.AccessTokenMaxAge*60,"/","localhost",false,true) |
| 123 | +ctx.SetCookie("logged_in","true",config.AccessTokenMaxAge*60,"/","localhost",false,false) |
| 124 | + |
| 125 | +ctx.JSON(http.StatusOK, gin.H{"status":"success","access_token":access_token}) |
| 126 | +} |