11package controllers
22
33import (
4- "fmt"
54 "net/http"
65 "strings"
76 "time"
87
98 "github.com/gin-gonic/gin"
9+ "github.com/thanhpk/randstr"
1010 "github.com/wpcodevo/golang-gorm-postgres/initializers"
1111 "github.com/wpcodevo/golang-gorm-postgres/models"
1212 "github.com/wpcodevo/golang-gorm-postgres/utils"
@@ -47,7 +47,7 @@ func (ac *AuthController) SignUpUser(ctx *gin.Context) {
4747 Email : strings .ToLower (payload .Email ),
4848 Password : hashedPassword ,
4949 Role : "user" ,
50- Verified : true ,
50+ Verified : false ,
5151 Photo : payload .Photo ,
5252 Provider : "local" ,
5353 CreatedAt : now ,
@@ -61,17 +61,34 @@ func (ac *AuthController) SignUpUser(ctx *gin.Context) {
6161 return
6262 }
6363
64- userResponse := & models.UserResponse {
65- ID : newUser .ID ,
66- Name : newUser .Name ,
67- Email : newUser .Email ,
68- Photo : newUser .Photo ,
69- Role : newUser .Role ,
70- Provider : newUser .Provider ,
71- CreatedAt : newUser .CreatedAt ,
72- UpdatedAt : newUser .UpdatedAt ,
64+ config , _ := initializers .LoadConfig ("." )
65+
66+ // Generate Verification Code
67+ code := randstr .String (20 )
68+
69+ verification_code := utils .Encode (code )
70+
71+ // Update User in Database
72+ newUser .VerificationCode = verification_code
73+ ac .DB .Save (newUser )
74+
75+ var firstName = newUser .Name
76+
77+ if strings .Contains (firstName , " " ) {
78+ firstName = strings .Split (firstName , " " )[1 ]
79+ }
80+
81+ // 👇 Send Email
82+ emailData := utils.EmailData {
83+ URL : config .ClientOrigin + "/verifyemail/" + code ,
84+ FirstName : firstName ,
85+ Subject : "Your account verification code" ,
7386 }
74- ctx .JSON (http .StatusCreated , gin.H {"status" : "success" , "data" : gin.H {"user" : userResponse }})
87+
88+ utils .SendEmail (& newUser , & emailData )
89+
90+ message := "We sent an email with a verification code to " + newUser .Email
91+ ctx .JSON (http .StatusCreated , gin.H {"status" : "success" , "message" : message })
7592}
7693
7794func (ac * AuthController ) SignInUser (ctx * gin.Context ) {
@@ -96,68 +113,43 @@ func (ac *AuthController) SignInUser(ctx *gin.Context) {
96113
97114 config , _ := initializers .LoadConfig ("." )
98115
99- // Generate Tokens
100- access_token , err := utils .CreateToken (config .AccessTokenExpiresIn , user .ID , config .AccessTokenPrivateKey )
101- if err != nil {
102- ctx .JSON (http .StatusBadRequest , gin.H {"status" : "fail" , "message" : err .Error ()})
103- return
104- }
105-
106- refresh_token , err := utils .CreateToken (config .RefreshTokenExpiresIn , user .ID , config .RefreshTokenPrivateKey )
116+ // Generate Token
117+ token , err := utils .GenerateToken (config .TokenExpiresIn , user .ID , config .TokenSecret )
107118 if err != nil {
108119 ctx .JSON (http .StatusBadRequest , gin.H {"status" : "fail" , "message" : err .Error ()})
109120 return
110121 }
111122
112- ctx .SetCookie ("access_token" , access_token , config .AccessTokenMaxAge * 60 , "/" , "localhost" , false , true )
113- ctx .SetCookie ("refresh_token" , refresh_token , config .RefreshTokenMaxAge * 60 , "/" , "localhost" , false , true )
114- ctx .SetCookie ("logged_in" , "true" , config .AccessTokenMaxAge * 60 , "/" , "localhost" , false , false )
123+ ctx .SetCookie ("token" , token , config .TokenMaxAge * 60 , "/" , "localhost" , false , true )
115124
116- ctx .JSON (http .StatusOK , gin.H {"status" : "success" , "access_token " : access_token })
125+ ctx .JSON (http .StatusOK , gin.H {"status" : "success" , "token " : token })
117126}
118127
119- // Refresh Access Token
120- func (ac * AuthController ) RefreshAccessToken (ctx * gin.Context ) {
121- message := "could not refresh access token"
122-
123- cookie , err := ctx .Cookie ("refresh_token" )
124-
125- if err != nil {
126- ctx .AbortWithStatusJSON (http .StatusForbidden , gin.H {"status" : "fail" , "message" : message })
127- return
128- }
128+ func (ac * AuthController ) LogoutUser (ctx * gin.Context ) {
129+ ctx .SetCookie ("token" , "" , - 1 , "/" , "localhost" , false , true )
130+ ctx .JSON (http .StatusOK , gin.H {"status" : "success" })
131+ }
129132
130- config , _ := initializers . LoadConfig ( "." )
133+ func ( ac * AuthController ) VerifyEmail ( ctx * gin. Context ) {
131134
132- sub , err := utils .ValidateToken (cookie , config .RefreshTokenPublicKey )
133- if err != nil {
134- ctx .AbortWithStatusJSON (http .StatusForbidden , gin.H {"status" : "fail" , "message" : err .Error ()})
135- return
136- }
135+ code := ctx .Params .ByName ("verificationCode" )
136+ verification_code := utils .Encode (code )
137137
138- var user models.User
139- result := ac .DB .First (& user , "id = ?" , fmt . Sprint ( sub ) )
138+ var updatedUser models.User
139+ result := ac .DB .First (& updatedUser , "verification_code = ?" , verification_code )
140140 if result .Error != nil {
141- ctx .AbortWithStatusJSON (http .StatusForbidden , gin.H {"status" : "fail" , "message" : "the user belonging to this token no logger exists" })
141+ ctx .JSON (http .StatusBadRequest , gin.H {"status" : "fail" , "message" : "Invalid verification code or user doesn't exists" })
142142 return
143143 }
144144
145- access_token , err := utils .CreateToken (config .AccessTokenExpiresIn , user .ID , config .AccessTokenPrivateKey )
146- if err != nil {
147- ctx .AbortWithStatusJSON (http .StatusForbidden , gin.H {"status" : "fail" , "message" : err .Error ()})
145+ if updatedUser .Verified {
146+ ctx .JSON (http .StatusConflict , gin.H {"status" : "fail" , "message" : "User already verified" })
148147 return
149148 }
150149
151- ctx .SetCookie ("access_token" , access_token , config .AccessTokenMaxAge * 60 , "/" , "localhost" , false , true )
152- ctx .SetCookie ("logged_in" , "true" , config .AccessTokenMaxAge * 60 , "/" , "localhost" , false , false )
153-
154- ctx .JSON (http .StatusOK , gin.H {"status" : "success" , "access_token" : access_token })
155- }
156-
157- func (ac * AuthController ) LogoutUser (ctx * gin.Context ) {
158- ctx .SetCookie ("access_token" , "" , - 1 , "/" , "localhost" , false , true )
159- ctx .SetCookie ("refresh_token" , "" , - 1 , "/" , "localhost" , false , true )
160- ctx .SetCookie ("logged_in" , "" , - 1 , "/" , "localhost" , false , false )
150+ updatedUser .VerificationCode = ""
151+ updatedUser .Verified = true
152+ ac .DB .Save (& updatedUser )
161153
162- ctx .JSON (http .StatusOK , gin.H {"status" : "success" })
154+ ctx .JSON (http .StatusOK , gin.H {"status" : "success" , "message" : "Email verified successfully" })
163155}
0 commit comments