Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 72394d1

Browse filesBrowse files
committed
updated
1 parent 684351b commit 72394d1
Copy full SHA for 72394d1

File tree

3 files changed

+62
-27
lines changed
Filter options

3 files changed

+62
-27
lines changed

‎controllers/auth.controller.go

Copy file name to clipboardExpand all lines: controllers/auth.controller.go
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,43 @@ func (ac *AuthController) ForgotPassword(ctx *gin.Context) {
276276
}
277277
ctx.JSON(http.StatusOK, gin.H{"status": "success", "message": message})
278278
}
279+
280+
func (ac *AuthController) ResetPassword(ctx *gin.Context) {
281+
var userCredential *models.ResetPasswordInput
282+
resetToken := ctx.Params.ByName("resetToken")
283+
284+
if err := ctx.ShouldBindJSON(&userCredential); err != nil {
285+
ctx.JSON(http.StatusBadRequest, gin.H{"status": "fail", "message": err.Error()})
286+
return
287+
}
288+
289+
if userCredential.Password != userCredential.PasswordConfirm {
290+
ctx.JSON(http.StatusBadRequest, gin.H{"status": "fail", "message": "Passwords do not match"})
291+
return
292+
}
293+
294+
hashedPassword, _ := utils.HashPassword(userCredential.Password)
295+
296+
passwordResetToken := utils.Encode(resetToken)
297+
298+
// Update User in Database
299+
query := bson.D{{Key: "passwordResetToken", Value: passwordResetToken}}
300+
update := bson.D{{Key: "$set", Value: bson.D{{Key: "password", Value: hashedPassword}}}, {Key: "$unset", Value: bson.D{{Key: "passwordResetToken", Value: ""}, {Key: "passwordResetAt", Value: ""}}}}
301+
result, err := ac.collection.UpdateOne(ac.ctx, query, update)
302+
303+
if result.MatchedCount == 0 {
304+
ctx.JSON(http.StatusBadRequest, gin.H{"status": "success", "message": "Token is invalid or has expired"})
305+
return
306+
}
307+
308+
if err != nil {
309+
ctx.JSON(http.StatusForbidden, gin.H{"status": "success", "message": err.Error()})
310+
return
311+
}
312+
313+
ctx.SetCookie("access_token", "", -1, "/", "localhost", false, true)
314+
ctx.SetCookie("refresh_token", "", -1, "/", "localhost", false, true)
315+
ctx.SetCookie("logged_in", "", -1, "/", "localhost", false, true)
316+
317+
ctx.JSON(http.StatusOK, gin.H{"status": "success", "message": "Password data updated successfully"})
318+
}

‎models/user.model.go

Copy file name to clipboardExpand all lines: models/user.model.go
+20-26Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,14 @@ import (
88

99
// 👈 SignUpInput struct
1010
type SignUpInput struct {
11-
Name string `json:"name" bson:"name" binding:"required"`
12-
Email string `json:"email" bson:"email" binding:"required"`
13-
Password string `json:"password" bson:"password" binding:"required,min=8"`
14-
PasswordConfirm string `json:"passwordConfirm" bson:"passwordConfirm,omitempty" binding:"required"`
15-
Role string `json:"role" bson:"role"`
16-
VerificationCode string `json:"verificationCode,omitempty" bson:"verificationCode,omitempty"`
17-
ResetPasswordToken string `json:"resetPasswordToken,omitempty" bson:"resetPasswordToken,omitempty"`
18-
ResetPasswordAt time.Time `json:"resetPasswordAt,omitempty" bson:"resetPasswordAt,omitempty"`
19-
Verified bool `json:"verified" bson:"verified"`
20-
CreatedAt time.Time `json:"created_at" bson:"created_at"`
21-
UpdatedAt time.Time `json:"updated_at" bson:"updated_at"`
11+
Name string `json:"name" bson:"name" binding:"required"`
12+
Email string `json:"email" bson:"email" binding:"required"`
13+
Password string `json:"password" bson:"password" binding:"required,min=8"`
14+
PasswordConfirm string `json:"passwordConfirm" bson:"passwordConfirm,omitempty" binding:"required"`
15+
Role string `json:"role" bson:"role"`
16+
Verified bool `json:"verified" bson:"verified"`
17+
CreatedAt time.Time `json:"created_at" bson:"created_at"`
18+
UpdatedAt time.Time `json:"updated_at" bson:"updated_at"`
2219
}
2320

2421
// 👈 SignInInput struct
@@ -29,18 +26,15 @@ type SignInInput struct {
2926

3027
// 👈 DBResponse struct
3128
type DBResponse struct {
32-
ID primitive.ObjectID `json:"id" bson:"_id"`
33-
Name string `json:"name" bson:"name"`
34-
Email string `json:"email" bson:"email"`
35-
Password string `json:"password" bson:"password"`
36-
PasswordConfirm string `json:"passwordConfirm,omitempty" bson:"passwordConfirm,omitempty"`
37-
Role string `json:"role" bson:"role"`
38-
VerificationCode string `json:"verificationCode,omitempty" bson:"verificationCode"`
39-
ResetPasswordToken string `json:"resetPasswordToken,omitempty" bson:"resetPasswordToken,omitempty"`
40-
ResetPasswordAt time.Time `json:"resetPasswordAt,omitempty" bson:"resetPasswordAt,omitempty"`
41-
Verified bool `json:"verified" bson:"verified"`
42-
CreatedAt time.Time `json:"created_at" bson:"created_at"`
43-
UpdatedAt time.Time `json:"updated_at" bson:"updated_at"`
29+
ID primitive.ObjectID `json:"id" bson:"_id"`
30+
Name string `json:"name" bson:"name"`
31+
Email string `json:"email" bson:"email"`
32+
Password string `json:"password" bson:"password"`
33+
PasswordConfirm string `json:"passwordConfirm,omitempty" bson:"passwordConfirm,omitempty"`
34+
Role string `json:"role" bson:"role"`
35+
Verified bool `json:"verified" bson:"verified"`
36+
CreatedAt time.Time `json:"created_at" bson:"created_at"`
37+
UpdatedAt time.Time `json:"updated_at" bson:"updated_at"`
4438
}
4539

4640
// 👈 UserResponse struct
@@ -55,13 +49,13 @@ type UserResponse struct {
5549

5650
// 👈 ForgotPasswordInput struct
5751
type ForgotPasswordInput struct {
58-
Email string `json:"email" bson:"email" binding:"required"`
52+
Email string `json:"email" binding:"required"`
5953
}
6054

6155
// 👈 ResetPasswordInput struct
6256
type ResetPasswordInput struct {
63-
Password string `json:"password" bson:"password"`
64-
PasswordConfirm string `json:"passwordConfirm,omitempty" bson:"passwordConfirm,omitempty"`
57+
Password string `json:"password" binding:"required"`
58+
PasswordConfirm string `json:"passwordConfirm" binding:"required"`
6559
}
6660

6761
func FilteredResponse(user *DBResponse) UserResponse {

‎routes/auth.routes.go

Copy file name to clipboardExpand all lines: routes/auth.routes.go
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ func (rc *AuthRouteController) AuthRoute(rg *gin.RouterGroup, userService servic
2323
router.GET("/refresh", rc.authController.RefreshAccessToken)
2424
router.GET("/logout", middleware.DeserializeUser(userService), rc.authController.LogoutUser)
2525
router.GET("/verifyemail/:verificationCode", rc.authController.VerifyEmail)
26-
router.POST("/forgotPassword", rc.authController.ForgotPassword)
26+
router.POST("/forgotpassword", rc.authController.ForgotPassword)
27+
router.PATCH("/resetpassword/:resetToken", rc.authController.ResetPassword)
2728
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.