Yōten: A social tracker for your language learning journey built on the atproto.

feat: redirect user back to where they were after login redirect

Signed-off-by: brookjeynes <me@brookjeynes.dev>

authored by brookjeynes.dev and committed by

Tangled c85d9bbd 6f84a644

+24 -5
+13 -3
internal/server/middleware/middleware.go
··· 2 2 3 3 import ( 4 4 "context" 5 + "fmt" 5 6 "log" 6 7 "net/http" 8 + "net/url" 7 9 "slices" 8 10 "strings" 9 11 ··· 38 40 func AuthMiddleware(a *oauth.OAuth) middlewareFunc { 39 41 return func(next http.Handler) http.Handler { 40 42 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 41 - redirectFunc := func(w http.ResponseWriter, r *http.Request) { 42 - http.Redirect(w, r, "/login", http.StatusTemporaryRedirect) 43 + returnURL := "/" 44 + if u, err := url.Parse(r.Header.Get("Referer")); err == nil { 45 + returnURL = u.RequestURI() 43 46 } 44 47 48 + loginURL := fmt.Sprintf("/login?return_url=%s", url.QueryEscape(returnURL)) 49 + 50 + redirectFunc := func(w http.ResponseWriter, r *http.Request) { 51 + http.Redirect(w, r, loginURL, http.StatusTemporaryRedirect) 52 + } 45 53 if r.Header.Get("HX-Request") == "true" { 46 54 redirectFunc = func(w http.ResponseWriter, _ *http.Request) { 47 - w.Header().Set("HX-Redirect", "/login") 55 + w.Header().Set("HX-Redirect", loginURL) 48 56 w.WriteHeader(http.StatusOK) 49 57 } 50 58 } ··· 75 83 next.ServeHTTP(w, r) 76 84 return 77 85 } 86 + 87 + didOrHandle = strings.TrimPrefix(didOrHandle, "@") 78 88 79 89 id, err := mw.idResolver.ResolveIdent(r.Context(), didOrHandle) 80 90 if err != nil {
+9 -1
internal/server/views/login.templ
··· 28 28 </div> 29 29 <div class="flex flex-col gap-2"> 30 30 <label for="handle">Handle or DID</label> 31 - <input type="text" id="handle" name="handle" placeholder="username.bsky.social" class="input" autocomplete="username"/> 31 + <input 32 + type="text" 33 + id="handle" 34 + name="handle" 35 + placeholder="username.bsky.social" 36 + class="input" 37 + autocomplete="username" 38 + /> 32 39 <p class="text-xs text-text-muted">Enter your Bluesky handle (e.g., alice.bsky.social) or full DID</p> 33 40 </div> 41 + <input type="hidden" name="return_url" value={ params.ReturnUrl }/> 34 42 <button class="btn btn-primary" type="submit" id="login-button"> 35 43 <span>Log in with AT Protocol</span> 36 44 <i class="w-4 h-4" data-lucide="arrow-right"></i>
+2 -1
internal/server/views/views.go
··· 18 18 19 19 type LoginPageParams struct { 20 20 // The current logged in user. 21 - User *types.User 21 + User *types.User 22 + ReturnUrl string 22 23 } 23 24 24 25 type ProfilePageParams struct {