Prechádzať zdrojové kódy

login: change NotifyUserOfResetPasswordLinkFunc to hook

xuxin 1 rok pred
rodič
commit
7ac268d37c
2 zmenil súbory, kde vykonal 26 pridanie a 39 odobranie
  1. 21 35
      login/builder.go
  2. 5 4
      login/example/main.go

+ 21 - 35
login/builder.go

@@ -36,9 +36,8 @@ var (
 )
 
 type HomeURLFunc func(r *http.Request, user interface{}) string
-type NotifyUserOfResetPasswordLinkFunc func(user interface{}, resetLink string) error
 type PasswordValidationFunc func(password string) (message string, ok bool)
-type HookFunc func(r *http.Request, user interface{}, vals ...interface{}) error
+type HookFunc func(r *http.Request, user interface{}, extraVals ...interface{}) error
 
 type void struct{}
 
@@ -109,19 +108,18 @@ type Builder struct {
 	totpSetupPageFunc             web.PageFunc
 	totpValidatePageFunc          web.PageFunc
 
-	notifyUserOfResetPasswordLinkFunc NotifyUserOfResetPasswordLinkFunc
-	passwordValidationFunc            PasswordValidationFunc
+	passwordValidationFunc PasswordValidationFunc
 
-	afterLoginHook                 HookFunc
-	afterFailedToLoginHook         HookFunc
-	afterUserLockedHook            HookFunc
-	afterLogoutHook                HookFunc
-	afterSendResetPasswordLinkHook HookFunc
-	afterResetPasswordHook         HookFunc
-	afterChangePasswordHook        HookFunc
-	afterExtendSessionHook         HookFunc
-	afterTOTPCodeReusedHook        HookFunc
-	afterOAuthCompleteHook         HookFunc
+	afterLoginHook                        HookFunc
+	afterFailedToLoginHook                HookFunc
+	afterUserLockedHook                   HookFunc
+	afterLogoutHook                       HookFunc
+	afterConfirmSendResetPasswordLinkHook HookFunc
+	afterResetPasswordHook                HookFunc
+	afterChangePasswordHook               HookFunc
+	afterExtendSessionHook                HookFunc
+	afterTOTPCodeReusedHook               HookFunc
+	afterOAuthCompleteHook                HookFunc
 
 	db                   *gorm.DB
 	userModel            interface{}
@@ -293,11 +291,6 @@ func (b *Builder) TOTPValidatePageFunc(v web.PageFunc) (r *Builder) {
 	return b
 }
 
-func (b *Builder) NotifyUserOfResetPasswordLinkFunc(v NotifyUserOfResetPasswordLinkFunc) (r *Builder) {
-	b.notifyUserOfResetPasswordLinkFunc = v
-	return b
-}
-
 func (b *Builder) PasswordValidationFunc(v PasswordValidationFunc) (r *Builder) {
 	b.passwordValidationFunc = v
 	return b
@@ -308,11 +301,11 @@ func (b *Builder) wrapHook(v HookFunc) HookFunc {
 		return nil
 	}
 
-	return func(r *http.Request, user interface{}, vals ...interface{}) error {
+	return func(r *http.Request, user interface{}, extraVals ...interface{}) error {
 		if GetCurrentUser(r) == nil {
 			r = r.WithContext(context.WithValue(r.Context(), UserKey, user))
 		}
-		return v(r, user, vals...)
+		return v(r, user, extraVals...)
 	}
 }
 
@@ -336,8 +329,10 @@ func (b *Builder) AfterLogout(v HookFunc) (r *Builder) {
 	return b
 }
 
-func (b *Builder) AfterSendResetPasswordLink(v HookFunc) (r *Builder) {
-	b.afterSendResetPasswordLinkHook = b.wrapHook(v)
+// extra vals:
+// - reset link
+func (b *Builder) AfterConfirmSendResetPasswordLink(v HookFunc) (r *Builder) {
+	b.afterConfirmSendResetPasswordLinkHook = b.wrapHook(v)
 	return b
 }
 
@@ -351,7 +346,7 @@ func (b *Builder) AfterChangePassword(v HookFunc) (r *Builder) {
 	return b
 }
 
-// vals:
+// extra vals:
 // - old session token
 func (b *Builder) AfterExtendSession(v HookFunc) (r *Builder) {
 	b.afterExtendSessionHook = b.wrapHook(v)
@@ -1006,17 +1001,8 @@ func (b *Builder) sendResetPasswordLink(w http.ResponseWriter, r *http.Request)
 	if doTOTP {
 		link = MustSetQuery(link, "totp", "1")
 	}
-	if err = b.notifyUserOfResetPasswordLinkFunc(u, link); err != nil {
-		setFailCodeFlash(w, FailCodeSystemError)
-		setWrongForgetPasswordInputFlash(w, WrongForgetPasswordInputFlash{
-			Account: account,
-		})
-		http.Redirect(w, r, failRedirectURL, http.StatusFound)
-		return
-	}
-
-	if b.afterSendResetPasswordLinkHook != nil {
-		if herr := b.afterSendResetPasswordLinkHook(r, u); herr != nil {
+	if b.afterConfirmSendResetPasswordLinkHook != nil {
+		if herr := b.afterConfirmSendResetPasswordLinkHook(r, u, link); herr != nil {
 			setFailCodeFlash(w, FailCodeSystemError)
 			http.Redirect(w, r, failRedirectURL, http.StatusFound)
 			return

+ 5 - 4
login/example/main.go

@@ -6,10 +6,10 @@ import (
 	"net/http"
 	"os"
 
-	"github.com/qor5/x/i18n"
-	"github.com/qor5/x/login"
 	"github.com/markbates/goth/providers/google"
 	"github.com/markbates/goth/providers/twitter"
+	"github.com/qor5/x/i18n"
+	"github.com/qor5/x/login"
 	. "github.com/theplant/htmlgo"
 	"github.com/theplant/testingutils"
 	"golang.org/x/text/language"
@@ -67,9 +67,10 @@ func main() {
 		HomeURLFunc(func(r *http.Request, user interface{}) string {
 			return "/admin"
 		}).
-		NotifyUserOfResetPasswordLinkFunc(func(user interface{}, resetLink string) error {
+		AfterConfirmSendResetPasswordLink(func(r *http.Request, user interface{}, extraVals ...interface{}) error {
+			link := extraVals[0]
 			fmt.Println("#########################################start")
-			testingutils.PrintlnJson(resetLink)
+			testingutils.PrintlnJson(link)
 			fmt.Println("#########################################end")
 			return nil
 		})