Auto-création du compte utilisateur à la première connexion
- Sauvegarde de l'adresse courriel de l'utilisateur en session - Implémentation d'une première Query GraphQL pour récupérer le profil de l'utilisateur connecté - Utilisation de la pattern CQRS pour les commandes/requêtes sur la base de données
This commit is contained in:
@ -3,6 +3,13 @@ package route
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"forge.cadoles.com/Cadoles/daddy/internal/command"
|
||||
"gitlab.com/wpetit/goweb/cqrs"
|
||||
|
||||
"forge.cadoles.com/Cadoles/daddy/internal/session"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"forge.cadoles.com/Cadoles/daddy/internal/config"
|
||||
oidc "forge.cadoles.com/wpetit/goweb-oidc"
|
||||
"gitlab.com/wpetit/goweb/logger"
|
||||
@ -15,6 +22,11 @@ func handleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
client.Login(w, r)
|
||||
}
|
||||
|
||||
type emailClaims struct {
|
||||
Email string `json:"email"`
|
||||
EmailVerified bool `json:"email_verified"`
|
||||
}
|
||||
|
||||
func handleLoginCallback(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
ctn := container.Must(ctx)
|
||||
@ -31,5 +43,39 @@ func handleLoginCallback(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
logger.Info(ctx, "user logged in", logger.F("sub", idToken.Subject))
|
||||
|
||||
claims := &emailClaims{}
|
||||
if err := idToken.Claims(claims); err != nil {
|
||||
panic(errors.WithStack(err))
|
||||
}
|
||||
|
||||
// TODO implements better UX in case of errors
|
||||
|
||||
if claims.Email == "" {
|
||||
http.Error(w, "an email is expected to access this app", http.StatusForbidden)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if !claims.EmailVerified {
|
||||
http.Error(w, "your email must be verified to access this app", http.StatusForbidden)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
dispatcher := cqrs.Must(ctn)
|
||||
|
||||
cmd := &command.CreateUserCommandRequest{
|
||||
Email: claims.Email,
|
||||
Connected: true,
|
||||
}
|
||||
|
||||
if _, err := dispatcher.Exec(ctx, cmd); err != nil {
|
||||
panic(errors.WithStack(err))
|
||||
}
|
||||
|
||||
if err := session.SaveUserEmail(w, r, claims.Email); err != nil {
|
||||
panic(errors.WithStack(err))
|
||||
}
|
||||
|
||||
http.Redirect(w, r, conf.HTTP.FrontendURL, http.StatusSeeOther)
|
||||
}
|
||||
|
Reference in New Issue
Block a user