mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 11:43:22 +02:00
feat: WIP mail verification flow
This commit is contained in:
+23
-5
@@ -6,6 +6,7 @@ import (
|
||||
"encoding/json"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/jwtauth/v5"
|
||||
"github.com/google/uuid"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -61,6 +62,13 @@ func IsLoggedIn(r *http.Request) bool {
|
||||
return token != nil && err == nil
|
||||
}
|
||||
|
||||
func IsVerified(r *http.Request) bool {
|
||||
_, claims, _ := jwtauth.FromContext(r.Context())
|
||||
userIDClaim, _ := claims["user_id"].(uint32)
|
||||
currentUser, _ := Container.GetUserUseCase.GetUser(userIDClaim)
|
||||
return currentUser.IsVerified
|
||||
}
|
||||
|
||||
func getUserFromCredentials(credentials LoginCredentials) (user.User, error) {
|
||||
dbUser, err := Container.GetUserUseCase.GetUserByUsername(credentials.Username)
|
||||
if err != nil {
|
||||
@@ -75,6 +83,19 @@ func getUserFromCredentials(credentials LoginCredentials) (user.User, error) {
|
||||
return dbUser, nil
|
||||
}
|
||||
|
||||
func getNewUser(newUserCredentials RegisterCredentials, verificationCode string) (user.User, error) {
|
||||
createdUser, err := Container.CreateUserUseCase.CreateUser(useCases.CreateUserCommand{
|
||||
Username: newUserCredentials.Username,
|
||||
Password: newUserCredentials.Password,
|
||||
Email: newUserCredentials.Email,
|
||||
VerificationCode: verificationCode,
|
||||
})
|
||||
if err != nil {
|
||||
return user.User{}, err
|
||||
}
|
||||
return createdUser, nil
|
||||
}
|
||||
|
||||
func login(w http.ResponseWriter, r *http.Request) {
|
||||
var credentials LoginCredentials
|
||||
|
||||
@@ -120,11 +141,8 @@ func register(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
createdUser, err := Container.CreateUserUseCase.CreateUser(useCases.CreateUserCommand{
|
||||
Username: credentials.Username,
|
||||
Password: credentials.Password,
|
||||
Email: credentials.Email,
|
||||
})
|
||||
verificationCode := uuid.NewString()
|
||||
createdUser, err := getNewUser(credentials, verificationCode)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
|
||||
+17
-1
@@ -47,6 +47,16 @@ func IsLoggedInMiddleware(next http.Handler) http.Handler {
|
||||
})
|
||||
}
|
||||
|
||||
func IsVerifiedMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if !IsVerified(r) {
|
||||
http.Redirect(w, r, "/register/pending", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func GetLogin(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, LoginRoute, http.StatusPermanentRedirect)
|
||||
}
|
||||
@@ -91,7 +101,13 @@ func NewPageRouter() http.Handler {
|
||||
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(IsLoggedInMiddleware)
|
||||
r.Get("/register-confirm", GetRegisterConfirmPage)
|
||||
r.Get("/register/pending", GetRegisterPendingPage)
|
||||
r.Get("/register/validate", GetRegisterValidatePage)
|
||||
})
|
||||
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(IsLoggedInMiddleware)
|
||||
r.Use(IsVerifiedMiddleware)
|
||||
r.Get("/home", GetHomePage)
|
||||
r.Get("/post", GetPostsPage)
|
||||
r.Get("/post/edit", GetPostEditPage)
|
||||
|
||||
+19
-19
@@ -1,8 +1,7 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/google/uuid"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
@@ -29,7 +28,7 @@ func GetRegisterPageHandler(registerPage *RegisterPage) http.HandlerFunc {
|
||||
PostRegisterPage(w, r)
|
||||
return
|
||||
}
|
||||
bs, err := Container.GetPageUseCase.GetPage("setup1", map[string]interface{}{
|
||||
bs, err := Container.GetPageUseCase.GetPage("register", map[string]interface{}{
|
||||
"PageError": registerPage.PageError,
|
||||
"Username": registerPage.Username,
|
||||
"Email": registerPage.Email,
|
||||
@@ -45,11 +44,12 @@ func GetRegisterPageHandler(registerPage *RegisterPage) http.HandlerFunc {
|
||||
func PostRegisterPage(w http.ResponseWriter, r *http.Request) {
|
||||
_ = r.ParseForm()
|
||||
|
||||
credentials, err := json.Marshal(&RegisterCredentials{
|
||||
credentials := RegisterCredentials{
|
||||
Username: r.FormValue("username"),
|
||||
Password: r.FormValue("password"),
|
||||
Email: r.FormValue("email"),
|
||||
})
|
||||
}
|
||||
err := validate.Struct(credentials)
|
||||
if err != nil {
|
||||
r.Method = http.MethodGet
|
||||
GetRegisterPageHandler(&RegisterPage{
|
||||
@@ -60,22 +60,22 @@ func PostRegisterPage(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
response, err := http.Post(
|
||||
"http://localhost:"+os.Getenv("PORT")+"/v1/auth/register",
|
||||
"application/json",
|
||||
bytes.NewBuffer(credentials))
|
||||
|
||||
if err != nil || response.StatusCode != http.StatusOK {
|
||||
r.Method = http.MethodGet
|
||||
GetRegisterPageHandler(&RegisterPage{
|
||||
PageError: NewPageError("Username should be between 3 and 20 characters long, password should be between 8 and 20 characters long, and email should be a valid email address."),
|
||||
Username: r.FormValue("username"),
|
||||
Email: r.FormValue("email"),
|
||||
})(w, r)
|
||||
verificationCode := uuid.NewString()
|
||||
createdUser, err := getNewUser(credentials, verificationCode)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Set-Cookie", response.Header.Get("Set-Cookie"))
|
||||
err = Container.SendMailUseCase.SendMail(createdUser.Email, "mailValidation", map[string]string{
|
||||
"Host": os.Getenv("HOST"),
|
||||
"VerificationCode": verificationCode,
|
||||
})
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/register-confirm", http.StatusSeeOther)
|
||||
_ = SetJwtCookie(&w, createdUser.ID)
|
||||
|
||||
http.Redirect(w, r, "/register/pending", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func GetRegisterConfirmPage(w http.ResponseWriter, _ *http.Request) {
|
||||
registerConfirmTmpl, _ := Container.GetPageUseCase.GetPage("setup2", map[string]interface{}{
|
||||
"Head": headTmpl,
|
||||
})
|
||||
_, _ = w.Write(registerConfirmTmpl)
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package api
|
||||
|
||||
import "net/http"
|
||||
|
||||
func GetRegisterPendingPage(w http.ResponseWriter, _ *http.Request) {
|
||||
registerPendingTmpl, _ := Container.GetPageUseCase.GetPage("registerPending", map[string]interface{}{
|
||||
"Head": headTmpl,
|
||||
})
|
||||
_, _ = w.Write(registerPendingTmpl)
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/go-chi/jwtauth/v5"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func GetRegisterValidatePage(w http.ResponseWriter, r *http.Request) {
|
||||
queryVerificationCode := r.URL.Query().Get("c")
|
||||
if queryVerificationCode == "" {
|
||||
http.Redirect(w, r, "/register/pending", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
_, claims, _ := jwtauth.FromContext(r.Context())
|
||||
userId, _ := claims["user_id"].(uint32)
|
||||
|
||||
user, _ := Container.GetUserUseCase.GetUser(userId)
|
||||
errorMessage := ""
|
||||
|
||||
if user.VerificationCode != queryVerificationCode || user.VerificationExpiration.Before(time.Now()) {
|
||||
errorMessage = "Verification link is incorrect or has expired."
|
||||
} else {
|
||||
// TODO New usecase "UpdateUserUseCase" to update its verification status
|
||||
}
|
||||
|
||||
registerValidateTmpl, _ := Container.GetPageUseCase.GetPage("registerValidate", map[string]interface{}{
|
||||
"Head": headTmpl,
|
||||
"PageError": NewPageError(errorMessage),
|
||||
})
|
||||
_, _ = w.Write(registerValidateTmpl)
|
||||
}
|
||||
Reference in New Issue
Block a user