summaryrefslogtreecommitdiff
path: root/app/controllers/sessions
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2025-05-11 22:11:09 -0600
committermo khan <mo@mokhan.ca>2025-05-11 22:11:09 -0600
commit7517e20bcce760c896e6b349c98cf04f88d6e00f (patch)
treebc894004cb6ac3e14789c44a92f53987b44003b1 /app/controllers/sessions
parent086300ea0dbd0ff4af320a1b4402a93f5bd87ac0 (diff)
fix: redirect to dashboard when already logged in at callback url
Diffstat (limited to 'app/controllers/sessions')
-rw-r--r--app/controllers/sessions/controller.go5
-rw-r--r--app/controllers/sessions/controller_test.go16
2 files changed, 21 insertions, 0 deletions
diff --git a/app/controllers/sessions/controller.go b/app/controllers/sessions/controller.go
index 2853358..898244c 100644
--- a/app/controllers/sessions/controller.go
+++ b/app/controllers/sessions/controller.go
@@ -93,6 +93,11 @@ When it is decoded it has the following form:
```
*/
func (c *Controller) Create(w http.ResponseWriter, r *http.Request) {
+ if middleware.IsLoggedIn(r) {
+ http.Redirect(w, r, "/dashboard", http.StatusFound)
+ return
+ }
+
tokens, err := c.svc.Exchange(r)
if err != nil {
pls.LogError(r.Context(), err)
diff --git a/app/controllers/sessions/controller_test.go b/app/controllers/sessions/controller_test.go
index 00e3f4e..b3e1d56 100644
--- a/app/controllers/sessions/controller_test.go
+++ b/app/controllers/sessions/controller_test.go
@@ -110,6 +110,22 @@ func TestSessions(t *testing.T) {
assert.Equal(t, http.StatusBadRequest, w.Code)
})
+ t.Run("when already logged in", func(t *testing.T) {
+ t.Run("redirects to the dashboard", func(t *testing.T) {
+ user := &domain.User{}
+ r, w := test.RequestResponse(
+ "GET",
+ "/session/callback?code=valid",
+ test.WithContextKeyValue(t.Context(), xcfg.CurrentUser, user),
+ )
+
+ mux.ServeHTTP(w, r)
+
+ require.Equal(t, http.StatusFound, w.Code)
+ assert.Equal(t, "/dashboard", w.Header().Get("Location"))
+ })
+ })
+
t.Run("with a valid authorization code grant", func(t *testing.T) {
user := mockoidc.DefaultUser()
code := srv.CreateAuthorizationCodeFor(user)