chore(auth): add service structs and test error handling
All checks were successful
Build Docker Image / Explore-Gitea-Actions (push) Successful in 49s

This commit is contained in:
2024-09-20 22:19:02 +02:00
parent 7e3e9388e2
commit e53a0e5cf7
4 changed files with 130 additions and 99 deletions

View File

@@ -15,32 +15,37 @@ import (
"net/url"
)
func authUi(db *sql.DB) http.Handler {
type AuthHandler struct {
db *sql.DB
service *service.AuthService
}
func (a *AuthHandler) authUi() http.Handler {
router := http.NewServeMux()
router.Handle("/auth/signin", handleSignInPage(db))
router.Handle("/auth/signup", handleSignUpPage(db))
router.Handle("/auth/verify", handleSignUpVerifyPage(db)) // Hint for the user to verify their email
router.Handle("/auth/delete-account", handleDeleteAccountPage(db))
router.Handle("/auth/verify-email", handleSignUpVerifyResponsePage(db)) // The link contained in the email
router.Handle("/auth/change-password", handleChangePasswordPage(db))
router.Handle("/auth/reset-password", handleResetPasswordPage(db))
router.Handle("/", handleNotFound(db))
router.Handle("/auth/signin", handleSignInPage(a.db))
router.Handle("/auth/signup", handleSignUpPage(a.db))
router.Handle("/auth/verify", handleSignUpVerifyPage(a.db)) // Hint for the user to verify their email
router.Handle("/auth/delete-account", handleDeleteAccountPage(a.db))
router.Handle("/auth/verify-email", handleSignUpVerifyResponsePage(a.db)) // The link contained in the email
router.Handle("/auth/change-password", handleChangePasswordPage(a.db))
router.Handle("/auth/reset-password", handleResetPasswordPage(a.db))
router.Handle("/", handleNotFound(a.db))
return router
}
func authApi(db *sql.DB) http.Handler {
func (a *AuthHandler) authApi() http.Handler {
router := http.NewServeMux()
router.Handle("/api/auth/signup", handleSignUp(db))
router.Handle("/api/auth/signin", handleSignIn(db))
router.Handle("/api/auth/signout", handleSignOut(db))
router.Handle("/api/auth/delete-account", handleDeleteAccount(db))
router.Handle("/api/auth/verify-resend", handleVerifyResend(db))
router.Handle("/api/auth/change-password", handleChangePassword(db))
router.Handle("/api/auth/reset-password", handleResetPassword(db))
router.Handle("/api/auth/reset-password-actual", handleActualResetPassword(db))
router.Handle("/api/auth/signup", handleSignUp(a.db))
router.Handle("/api/auth/signin", a.handleSignIn())
router.Handle("/api/auth/signout", handleSignOut(a.db))
router.Handle("/api/auth/delete-account", handleDeleteAccount(a.db))
router.Handle("/api/auth/verify-resend", handleVerifyResend(a.db))
router.Handle("/api/auth/change-password", handleChangePassword(a.db))
router.Handle("/api/auth/reset-password", handleResetPassword(a.db))
router.Handle("/api/auth/reset-password-actual", handleActualResetPassword(a.db))
return router
}
@@ -215,14 +220,15 @@ func createSessionCookie(sessionId types.SessionId) *http.Cookie {
return &cookie
}
func handleSignIn(db *sql.DB) http.HandlerFunc {
func (a *AuthHandler) handleSignIn() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var email = r.FormValue("email")
var password = r.FormValue("password")
sessionId, err := service.SignIn(db, email, password)
sessionId, err := a.service.SignIn(email, password)
if err != nil {
utils.TriggerToast(w, r, "error", "Invalid username or password")
utils.TriggerToast(w, r, "error", err.Error())
return
}
http.SetCookie(w, createSessionCookie(*sessionId))