Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

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

Sign up
Appearance settings

Commit72394d1

Browse files
committed
updated
1 parent684351b commit72394d1

File tree

3 files changed

+62
-27
lines changed

3 files changed

+62
-27
lines changed

‎controllers/auth.controller.go

Lines 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+
varuserCredential*models.ResetPasswordInput
282+
resetToken:=ctx.Params.ByName("resetToken")
283+
284+
iferr:=ctx.ShouldBindJSON(&userCredential);err!=nil {
285+
ctx.JSON(http.StatusBadRequest, gin.H{"status":"fail","message":err.Error()})
286+
return
287+
}
288+
289+
ifuserCredential.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+
ifresult.MatchedCount==0 {
304+
ctx.JSON(http.StatusBadRequest, gin.H{"status":"success","message":"Token is invalid or has expired"})
305+
return
306+
}
307+
308+
iferr!=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

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,14 @@ import (
88

99
// 👈 SignUpInput struct
1010
typeSignUpInputstruct {
11-
Namestring`json:"name" bson:"name" binding:"required"`
12-
Emailstring`json:"email" bson:"email" binding:"required"`
13-
Passwordstring`json:"password" bson:"password" binding:"required,min=8"`
14-
PasswordConfirmstring`json:"passwordConfirm" bson:"passwordConfirm,omitempty" binding:"required"`
15-
Rolestring`json:"role" bson:"role"`
16-
VerificationCodestring`json:"verificationCode,omitempty" bson:"verificationCode,omitempty"`
17-
ResetPasswordTokenstring`json:"resetPasswordToken,omitempty" bson:"resetPasswordToken,omitempty"`
18-
ResetPasswordAt time.Time`json:"resetPasswordAt,omitempty" bson:"resetPasswordAt,omitempty"`
19-
Verifiedbool`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+
Namestring`json:"name" bson:"name" binding:"required"`
12+
Emailstring`json:"email" bson:"email" binding:"required"`
13+
Passwordstring`json:"password" bson:"password" binding:"required,min=8"`
14+
PasswordConfirmstring`json:"passwordConfirm" bson:"passwordConfirm,omitempty" binding:"required"`
15+
Rolestring`json:"role" bson:"role"`
16+
Verifiedbool`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
typeDBResponsestruct {
32-
ID primitive.ObjectID`json:"id" bson:"_id"`
33-
Namestring`json:"name" bson:"name"`
34-
Emailstring`json:"email" bson:"email"`
35-
Passwordstring`json:"password" bson:"password"`
36-
PasswordConfirmstring`json:"passwordConfirm,omitempty" bson:"passwordConfirm,omitempty"`
37-
Rolestring`json:"role" bson:"role"`
38-
VerificationCodestring`json:"verificationCode,omitempty" bson:"verificationCode"`
39-
ResetPasswordTokenstring`json:"resetPasswordToken,omitempty" bson:"resetPasswordToken,omitempty"`
40-
ResetPasswordAt time.Time`json:"resetPasswordAt,omitempty" bson:"resetPasswordAt,omitempty"`
41-
Verifiedbool`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+
Namestring`json:"name" bson:"name"`
31+
Emailstring`json:"email" bson:"email"`
32+
Passwordstring`json:"password" bson:"password"`
33+
PasswordConfirmstring`json:"passwordConfirm,omitempty" bson:"passwordConfirm,omitempty"`
34+
Rolestring`json:"role" bson:"role"`
35+
Verifiedbool`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
typeForgotPasswordInputstruct {
58-
Emailstring`json:"email"bson:"email"binding:"required"`
52+
Emailstring`json:"email" binding:"required"`
5953
}
6054

6155
// 👈 ResetPasswordInput struct
6256
typeResetPasswordInputstruct {
63-
Passwordstring`json:"password"bson:"password"`
64-
PasswordConfirmstring`json:"passwordConfirm,omitempty" bson:"passwordConfirm,omitempty"`
57+
Passwordstring`json:"password"binding:"required"`
58+
PasswordConfirmstring`json:"passwordConfirm" binding:"required"`
6559
}
6660

6761
funcFilteredResponse(user*DBResponse)UserResponse {

‎routes/auth.routes.go

Lines 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)

[8]ページ先頭

©2009-2025 Movatter.jp