fix: restructure auth handler
This commit was merged in pull request #297.
This commit is contained in:
164
handler/auth.go
164
handler/auth.go
@@ -35,21 +35,21 @@ func (handler HandlerAuthImpl) Handle(router *http.ServeMux) {
|
|||||||
|
|
||||||
router.Handle("/auth/signup", handler.handleSignUpPage())
|
router.Handle("/auth/signup", handler.handleSignUpPage())
|
||||||
router.Handle("/auth/verify", handler.handleSignUpVerifyPage())
|
router.Handle("/auth/verify", handler.handleSignUpVerifyPage())
|
||||||
router.Handle("/api/auth/verify-resend", handler.HandleVerifyResendComp())
|
router.Handle("/api/auth/verify-resend", handler.handleVerifyResendComp())
|
||||||
router.Handle("/auth/verify-email", handler.HandleSignUpVerifyResponsePage())
|
router.Handle("/auth/verify-email", handler.handleSignUpVerifyResponsePage())
|
||||||
router.Handle("/api/auth/signup", handler.handleSignUp())
|
router.Handle("/api/auth/signup", handler.handleSignUp())
|
||||||
|
|
||||||
router.Handle("/api/auth/signout", handler.handleSignOut())
|
router.Handle("/api/auth/signout", handler.handleSignOut())
|
||||||
|
|
||||||
router.Handle("/auth/delete-account", handler.handleDeleteAccountPage())
|
router.Handle("/auth/delete-account", handler.handleDeleteAccountPage())
|
||||||
router.Handle("/api/auth/delete-account", handler.HandleDeleteAccountComp())
|
router.Handle("/api/auth/delete-account", handler.handleDeleteAccountComp())
|
||||||
|
|
||||||
router.Handle("/auth/change-password", handler.handleChangePasswordPage())
|
router.Handle("/auth/change-password", handler.handleChangePasswordPage())
|
||||||
router.Handle("/api/auth/change-password", handler.HandleChangePasswordComp())
|
router.Handle("/api/auth/change-password", handler.handleChangePasswordComp())
|
||||||
|
|
||||||
router.Handle("/auth/reset-password", handler.handleResetPasswordPage())
|
router.Handle("/auth/reset-password", handler.handleResetPasswordPage())
|
||||||
router.Handle("/api/auth/reset-password", handler.HandleForgotPasswordComp())
|
router.Handle("/api/auth/reset-password", handler.handleForgotPasswordComp())
|
||||||
router.Handle("/api/auth/reset-password-actual", handler.HandleForgotPasswordResponseComp())
|
router.Handle("/api/auth/reset-password-actual", handler.handleForgotPasswordResponseComp())
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -137,6 +137,56 @@ func (handler HandlerAuthImpl) handleSignUpPage() http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (handler HandlerAuthImpl) handleSignUpVerifyPage() http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
user, _ := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
||||||
|
if user == nil {
|
||||||
|
utils.DoRedirect(w, r, "/auth/signin")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if user.EmailVerified {
|
||||||
|
utils.DoRedirect(w, r, "/")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
signIn := auth.VerifyComp()
|
||||||
|
handler.render.RenderLayout(r, w, signIn, user)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (handler HandlerAuthImpl) handleVerifyResendComp() http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
user, err := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
||||||
|
if err != nil {
|
||||||
|
utils.DoRedirect(w, r, "/auth/signin")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
go handler.service.SendVerificationMail(user.Id, user.Email)
|
||||||
|
|
||||||
|
_, err = w.Write([]byte("<p class=\"mt-8\">Verification email sent</p>"))
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Could not write response: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (handler HandlerAuthImpl) handleSignUpVerifyResponsePage() http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
token := r.URL.Query().Get("token")
|
||||||
|
|
||||||
|
err := handler.service.VerifyUserEmail(token)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
utils.DoRedirect(w, r, "/auth/signin")
|
||||||
|
} else {
|
||||||
|
utils.DoRedirect(w, r, "/")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (handler HandlerAuthImpl) handleSignUp() http.HandlerFunc {
|
func (handler HandlerAuthImpl) handleSignUp() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
var email = r.FormValue("email")
|
var email = r.FormValue("email")
|
||||||
@@ -191,24 +241,6 @@ func (handler HandlerAuthImpl) handleSignOut() http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler HandlerAuthImpl) handleSignUpVerifyPage() http.HandlerFunc {
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
user, _ := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
|
||||||
if user == nil {
|
|
||||||
utils.DoRedirect(w, r, "/auth/signin")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if user.EmailVerified {
|
|
||||||
utils.DoRedirect(w, r, "/")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
signIn := auth.VerifyComp()
|
|
||||||
handler.render.RenderLayout(r, w, signIn, user)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (handler HandlerAuthImpl) handleDeleteAccountPage() http.HandlerFunc {
|
func (handler HandlerAuthImpl) handleDeleteAccountPage() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
// An unverified email should be able to delete their account
|
// An unverified email should be able to delete their account
|
||||||
@@ -222,52 +254,7 @@ func (handler HandlerAuthImpl) handleDeleteAccountPage() http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler HandlerAuthImpl) handleChangePasswordPage() http.HandlerFunc {
|
func (handler HandlerAuthImpl) handleDeleteAccountComp() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
|
|
||||||
isPasswordReset := r.URL.Query().Has("token")
|
|
||||||
|
|
||||||
user, _ := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
|
||||||
|
|
||||||
if user == nil && !isPasswordReset {
|
|
||||||
utils.DoRedirect(w, r, "/auth/signin")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
comp := auth.ChangePasswordComp(isPasswordReset)
|
|
||||||
handler.render.RenderLayout(r, w, comp, user)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (handler HandlerAuthImpl) handleResetPasswordPage() http.HandlerFunc {
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
|
|
||||||
user, err := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
|
||||||
if err != nil {
|
|
||||||
utils.DoRedirect(w, r, "/auth/signin")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
comp := auth.ResetPasswordComp()
|
|
||||||
handler.render.RenderLayout(r, w, comp, user)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (handler HandlerAuthImpl) HandleResetPasswordPage() http.HandlerFunc {
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
|
|
||||||
user, err := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
|
||||||
if err != nil {
|
|
||||||
utils.DoRedirect(w, r, "/auth/signin")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
comp := auth.ResetPasswordComp()
|
|
||||||
handler.render.RenderLayout(r, w, comp, user)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (handler HandlerAuthImpl) HandleDeleteAccountComp() http.HandlerFunc {
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
user, err := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
user, err := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -293,24 +280,24 @@ func (handler HandlerAuthImpl) HandleDeleteAccountComp() http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler HandlerAuthImpl) HandleVerifyResendComp() http.HandlerFunc {
|
func (handler HandlerAuthImpl) handleChangePasswordPage() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
user, err := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
|
||||||
if err != nil {
|
isPasswordReset := r.URL.Query().Has("token")
|
||||||
|
|
||||||
|
user, _ := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
||||||
|
|
||||||
|
if user == nil && !isPasswordReset {
|
||||||
utils.DoRedirect(w, r, "/auth/signin")
|
utils.DoRedirect(w, r, "/auth/signin")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
go handler.service.SendVerificationMail(user.Id, user.Email)
|
comp := auth.ChangePasswordComp(isPasswordReset)
|
||||||
|
handler.render.RenderLayout(r, w, comp, user)
|
||||||
_, err = w.Write([]byte("<p class=\"mt-8\">Verification email sent</p>"))
|
|
||||||
if err != nil {
|
|
||||||
log.Error("Could not write response: %v", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler HandlerAuthImpl) HandleChangePasswordComp() http.HandlerFunc {
|
func (handler HandlerAuthImpl) handleChangePasswordComp() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
user, err := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
user, err := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
||||||
@@ -332,22 +319,21 @@ func (handler HandlerAuthImpl) HandleChangePasswordComp() http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler HandlerAuthImpl) HandleSignUpVerifyResponsePage() http.HandlerFunc {
|
func (handler HandlerAuthImpl) handleResetPasswordPage() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
token := r.URL.Query().Get("token")
|
user, err := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
||||||
|
|
||||||
err := handler.service.VerifyUserEmail(token)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.DoRedirect(w, r, "/auth/signin")
|
utils.DoRedirect(w, r, "/auth/signin")
|
||||||
} else {
|
return
|
||||||
utils.DoRedirect(w, r, "/")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
comp := auth.ResetPasswordComp()
|
||||||
|
handler.render.RenderLayout(r, w, comp, user)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler HandlerAuthImpl) HandleForgotPasswordComp() http.HandlerFunc {
|
func (handler HandlerAuthImpl) handleForgotPasswordComp() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
email := r.FormValue("email")
|
email := r.FormValue("email")
|
||||||
@@ -365,7 +351,7 @@ func (handler HandlerAuthImpl) HandleForgotPasswordComp() http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler HandlerAuthImpl) HandleForgotPasswordResponseComp() http.HandlerFunc {
|
func (handler HandlerAuthImpl) handleForgotPasswordResponseComp() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
pageUrl, err := url.Parse(r.Header.Get("HX-Current-URL"))
|
pageUrl, err := url.Parse(r.Header.Get("HX-Current-URL"))
|
||||||
|
|||||||
Reference in New Issue
Block a user