Skip to content

Commit 6f4d684

Browse files
committed
Redirect after identity select
1 parent b8d8908 commit 6f4d684

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

app/handlers/identity.handlers.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,21 @@ func handleIdentitySet(logger *slog.Logger, queries *db.Queries) http.HandlerFun
9090
Type: session.FlashType_Info,
9191
Content: fmt.Sprintf("%s, welcome to the space %s!", member.Name, space.Name),
9292
})
93+
94+
redirectURL := fmt.Sprintf("/s/%s", access.Token)
95+
if storedRedirectURL, ok := sess.Values[session.RedirectKey].(string); ok && storedRedirectURL != "" {
96+
redirectURL = storedRedirectURL
97+
delete(sess.Values, session.RedirectKey)
98+
}
99+
93100
err = sess.Save(r, w)
94101
if err != nil {
95102
logger.Error("failed to save session", slog.Any("error", err))
96103
http.Error(w, "internal server error", http.StatusInternalServerError)
97104
return
98105
}
99106

100-
http.Redirect(w, r, fmt.Sprintf("/s/%s", access.Token), http.StatusSeeOther)
107+
http.Redirect(w, r, redirectURL, http.StatusSeeOther)
101108
}
102109
}
103110

app/rctx/identity.context.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"log/slog"
77
"net/http"
88

9+
"github.com/gorilla/sessions"
910
"github.com/nicolashery/simply-shared-notes/app/db"
1011
"github.com/nicolashery/simply-shared-notes/app/identity"
1112
"github.com/nicolashery/simply-shared-notes/app/session"
@@ -26,6 +27,7 @@ func IdentityCtxMiddleware(logger *slog.Logger, queries *db.Queries) func(http.H
2627
memberID, ok := sess.Values[session.IdentityKey].(int64)
2728
if !ok {
2829
delete(sess.Values, session.IdentityKey)
30+
storeRedirectURL(r, sess)
2931
err := sess.Save(r, w)
3032
if err != nil {
3133
logger.Error("failed to save session", slog.Any("error", err))
@@ -42,6 +44,7 @@ func IdentityCtxMiddleware(logger *slog.Logger, queries *db.Queries) func(http.H
4244
member, err := queries.GetMemberByID(r.Context(), memberID)
4345
if err != nil || member.SpaceID != space.ID {
4446
delete(sess.Values, session.IdentityKey)
47+
storeRedirectURL(r, sess)
4548
err := sess.Save(r, w)
4649
if err != nil {
4750
logger.Error("failed to save session", slog.Any("error", err))
@@ -61,6 +64,14 @@ func IdentityCtxMiddleware(logger *slog.Logger, queries *db.Queries) func(http.H
6164
}
6265
}
6366

67+
func storeRedirectURL(r *http.Request, sess *sessions.Session) {
68+
redirectURL := r.URL.Path
69+
if r.URL.RawQuery != "" {
70+
redirectURL += "?" + r.URL.RawQuery
71+
}
72+
sess.Values[session.RedirectKey] = redirectURL
73+
}
74+
6475
func GetIdentity(ctx context.Context) *identity.Identity {
6576
identity, ok := ctx.Value(identityContextKey).(*identity.Identity)
6677
if !ok {

app/session/session.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import (
77
)
88

99
const (
10-
CookieName = "simplysharednotes_session"
11-
IdentityKey = "identity"
10+
CookieName = "simplysharednotes_session"
11+
IdentityKey = "identity"
12+
RedirectKey = "redirect_url"
1213
)
1314

1415
func InitStore(secret string, isDev bool) *sessions.CookieStore {

docs/access.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@
4141
- The cookie is encrypted and cannot be tampered with (the only way a user can identity as another member is through the UI)
4242
- The cookie value contains the selected Member internal ID (among other session values)
4343
- The cookie expires after 3 months (90 days)
44-
- The Member selection page is at the URI `/identity`
45-
- All Space pages `/s/{token}/*` redirect to `/identity` if no valid Identity is found in the cookie
44+
- The Member selection page is at the URI `/s/{token}/identity`
45+
- All Space pages `/s/{token}/*` redirect to `/s/{token}/identity` if no valid Identity is found in the cookie
4646
- Note that a stored Identity can become invalid if a Member is deleted from the Space
47+
48+
**Redirect after Identity selection**:
49+
- When a user visits a protected URI without a valid session (e.g., `/s/{token}/members/{memberID}`), the system stores the original URI and redirects them to the identity selection page. After selecting their identity, they are automatically redirected back to the original URI they tried to visit instead of the space home page.
50+
- The original URI is stored securely in the encrypted session cookie using the `redirect_url` key
51+
- The redirect URI is cleared from the session after a single use to prevent replay
52+
- If no valid redirect URI is stored, users are redirected to the space home page

0 commit comments

Comments
 (0)