Skip to content

Commit

Permalink
fix: set user to authenticate
Browse files Browse the repository at this point in the history
  • Loading branch information
Björn Urban committed May 1, 2024
1 parent b31cef6 commit 50391d9
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions backend/internal/handlers/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@ type Handler struct {
BaseURL string
}

type TokenInfo struct {
authenticated bool
user string
}

var store = sessions.NewCookieStore([]byte("your-very-secret-key"))
var oneTimeStore = make(map[string]bool)
var oneTimeStore = make(map[string]TokenInfo)

func NewHandler(db *gorm.DB) *Handler {
jwtKey, err := util.GetEnvOrError("JWT_SECRET_KEY")
Expand Down Expand Up @@ -140,7 +145,7 @@ func (h *Handler) HandleLogin(w http.ResponseWriter, r *http.Request) {
if oneTimeToken == "" {
return
}
oneTimeStore[oneTimeToken] = true
oneTimeStore[oneTimeToken] = TokenInfo{true, inputUser.Email}
sendJSONResponse(w, response, http.StatusOK)
}

Expand Down Expand Up @@ -216,7 +221,7 @@ func (h *Handler) HandleAuthenticate(w http.ResponseWriter, r *http.Request) {
// Check if "authenticated" is set and true in the session
auth, ok := session.Values["authenticated"].(bool)
token, ok := session.Values["oneTimeToken"].(string)
tokenAuthenticated := oneTimeStore[token]
tokenAuthenticated := oneTimeStore[token].authenticated
slog.Info("token ?:", token)
slog.Info("tokenAuth ?:", tokenAuthenticated)
slog.Info("auth ?:", auth)
Expand Down Expand Up @@ -256,6 +261,7 @@ func (h *Handler) HandleAuthenticate(w http.ResponseWriter, r *http.Request) {
if tokenAuthenticated {
delete(oneTimeStore, token)
session.Values["authenticated"] = true
session.Values["user"] = oneTimeStore[token].user
session.Save(r, w)
}
slog.Info("Incoming session is authenticated")
Expand Down

0 comments on commit 50391d9

Please sign in to comment.