lot of new functionalities
This commit is contained in:
13
servers/web/auth/disconnect.go
Normal file
13
servers/web/auth/disconnect.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package auth
|
||||
|
||||
import "net/http"
|
||||
|
||||
func (h *Handler) Disconnect(w http.ResponseWriter, r *http.Request) {
|
||||
session, err := h.idp.SessionsStore.Get(r, "parcoursmob_session")
|
||||
if err == nil {
|
||||
session.Options.MaxAge = -1
|
||||
session.Save(r, w)
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/", http.StatusOK)
|
||||
}
|
||||
58
servers/web/auth/groups.go
Normal file
58
servers/web/auth/groups.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func (h *Handler) Groups(w http.ResponseWriter, r *http.Request) {
|
||||
session, _ := h.idp.SessionsStore.Get(r, "parcoursmob_session")
|
||||
|
||||
if r.Method == "POST" {
|
||||
r.ParseForm()
|
||||
|
||||
groupid := r.FormValue("group")
|
||||
|
||||
session.Values["organization"] = groupid
|
||||
session.Save(r, w)
|
||||
|
||||
http.Redirect(w, r, "/app/", http.StatusFound)
|
||||
return
|
||||
}
|
||||
|
||||
tokenstring, ok := session.Values["idtoken"]
|
||||
if !ok {
|
||||
http.Redirect(w, r, "/app/", http.StatusFound)
|
||||
return
|
||||
}
|
||||
|
||||
idtoken, err := h.idp.TokenVerifier.Verify(context.Background(), tokenstring.(string))
|
||||
if err != nil {
|
||||
delete(session.Values, "idtoken")
|
||||
http.Redirect(w, r, "/app/", http.StatusFound)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := h.applicationHandler.GetUserGroups(idtoken)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to get user groups")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
var groupsresponse = []any{}
|
||||
for _, group := range result.Groups {
|
||||
groupsresponse = append(groupsresponse, group)
|
||||
}
|
||||
|
||||
h.renderer.AuthGroups(w, r, groupsresponse)
|
||||
}
|
||||
|
||||
func (h *Handler) GroupSwitch(w http.ResponseWriter, r *http.Request) {
|
||||
session, _ := h.idp.SessionsStore.Get(r, "parcoursmob_session")
|
||||
delete(session.Values, "organization")
|
||||
session.Save(r, w)
|
||||
http.Redirect(w, r, "/app/", http.StatusFound)
|
||||
}
|
||||
30
servers/web/auth/handler.go
Normal file
30
servers/web/auth/handler.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/core/application"
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/core/utils/identification"
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/renderer"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
config *viper.Viper
|
||||
applicationHandler *application.ApplicationHandler
|
||||
idp *identification.IdentificationProvider
|
||||
renderer *renderer.Renderer
|
||||
}
|
||||
|
||||
func NewHandler(cfg *viper.Viper, applicationHandler *application.ApplicationHandler, idp *identification.IdentificationProvider, renderer *renderer.Renderer) *Handler {
|
||||
return &Handler{
|
||||
config: cfg,
|
||||
applicationHandler: applicationHandler,
|
||||
idp: idp,
|
||||
renderer: renderer,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) NotFound(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
52
servers/web/auth/lost_password.go
Normal file
52
servers/web/auth/lost_password.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func (h *Handler) LostPasswordInit(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "POST" {
|
||||
r.ParseForm()
|
||||
email := r.FormValue("email")
|
||||
if email != "" {
|
||||
_, err := h.applicationHandler.InitiateLostPassword(email)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to initiate password reset")
|
||||
http.Redirect(w, r, "/app/", http.StatusFound)
|
||||
return
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/app/", http.StatusFound)
|
||||
}
|
||||
}
|
||||
h.renderer.LostPasswordInit(w, r)
|
||||
}
|
||||
|
||||
func (h *Handler) LostPasswordRecover(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
|
||||
key := r.FormValue("key")
|
||||
recover, err := h.applicationHandler.GetPasswordRecoveryData(key)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to get password recovery data")
|
||||
h.renderer.LostPasswordRecoverKO(w, r, key)
|
||||
return
|
||||
}
|
||||
|
||||
if r.Method == "POST" {
|
||||
newpassword := r.FormValue("password")
|
||||
_, err := h.applicationHandler.RecoverLostPassword(key, newpassword)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to recover password")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/app/", http.StatusFound)
|
||||
return
|
||||
}
|
||||
|
||||
h.renderer.LostPasswordRecover(w, r, recover)
|
||||
}
|
||||
37
servers/web/auth/onboarding.go
Normal file
37
servers/web/auth/onboarding.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func (h *Handler) Onboarding(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
|
||||
key := r.FormValue("key")
|
||||
onboarding, err := h.applicationHandler.GetOnboardingData(key)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to get onboarding data")
|
||||
h.renderer.AuthOnboardingKO(w, r, key)
|
||||
return
|
||||
}
|
||||
|
||||
if r.Method == "POST" {
|
||||
firstName := r.FormValue("first_name")
|
||||
lastName := r.FormValue("last_name")
|
||||
password := r.FormValue("password")
|
||||
|
||||
_, err := h.applicationHandler.CompleteOnboarding(key, password, firstName, lastName)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to complete onboarding")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/app/", http.StatusFound)
|
||||
return
|
||||
}
|
||||
|
||||
h.renderer.AuthOnboarding(w, r, key, onboarding)
|
||||
}
|
||||
Reference in New Issue
Block a user