Skip to content

An open-source, faster Devin alternative

TypeScript

dashboard: skip login screen and fix WorkOS sign-out

Igor Zalutski3b596e6607a0
diff --git a/internal/auth/oauth_handlers.go b/internal/auth/oauth_handlers.go
index 750434ad..0e05f396 100644
--- a/internal/auth/oauth_handlers.go
+++ b/internal/auth/oauth_handlers.go
@@ -7,6 +7,7 @@ import (
"net/http"
"time"
+ "github.com/golang-jwt/jwt/v5"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/workos/workos-go/v4/pkg/usermanagement"
@@ -168,26 +169,66 @@ func (h *OAuthHandlers) HandleCallback(c echo.Context) error {
return c.Redirect(http.StatusFound, "/")
}
-// HandleLogout clears the session cookie and invalidates the server-side session.
+// HandleLogout ends the session via WorkOS's hosted logout. Clearing our own
+// cookies is NOT enough — the WorkOS SSO session would survive, so the next hit
+// to /auth/login silently re-authenticates the user (the "logout logs me back
+// in" loop). Handing the browser WorkOS's logout URL tears down the WorkOS
+// session cookie itself, then redirects to the Sign-out redirect configured in
+// the WorkOS dashboard.
+//
+// We intentionally do NOT pass return_to: the post-logout destination is
+// WorkOS-managed dashboard config (per environment), not app code. That config
+// is required — without a configured Sign-out redirect WorkOS returns
+// app-homepage-url-not-found.
func (h *OAuthHandlers) HandleLogout(c echo.Context) error {
- // Invalidate server-side session if we can identify the user
+ var sessionID string
if cookie, err := c.Cookie("workos_session"); err == nil && cookie.Value != "" {
- store := h.workos.Store()
- if store != nil {
+ // The WorkOS session id is the `sid` claim of the access token; it's
+ // required to build the hosted logout URL.
+ sessionID = sessionIDFromAccessToken(cookie.Value)
+
+ // Invalidate the server-side session if we can identify the user.
+ if store := h.workos.Store(); store != nil {
ctx := c.Request().Context()
- user, err := store.GetUserByAccessToken(ctx, cookie.Value)
- if err == nil {
+ if user, err := store.GetUserByAccessToken(ctx, cookie.Value); err == nil {
_ = store.DeleteAccessTokensForUser(ctx, user.ID)
}
}
}
- // Clear all auth cookies
+ // Clear all auth cookies.
ClearAllCookies(c)
- return c.JSON(http.StatusOK, map[string]string{
- "message": "logged out",
- })
+ // Hand back WorkOS's hosted logout URL for the browser to follow. GetLogoutURL
+ // is an offline URL builder; the real teardown + redirect happen when the
+ // browser visits it.
+ if mgr := h.workos.UserMgr(); mgr != nil && sessionID != "" {
+ if logoutURL, err := mgr.GetLogoutURL(usermanagement.GetLogoutURLOpts{SessionID: sessionID}); err == nil {
+ return c.JSON(http.StatusOK, map[string]string{
+ "message": "logged out",
+ "logoutUrl": logoutURL.String(),
+ })
+ }
+ }
+
+ // Fallback: no WorkOS session to end — the client falls back to /auth/login.
+ return c.JSON(http.StatusOK, map[string]string{"message": "logged out"})
+}
+
+// sessionIDFromAccessToken extracts the WorkOS `sid` claim from an access-token
+// JWT without verifying its signature. The token was already validated for this
+// request by the auth middleware; here we only need the session id to build the
+// logout URL, so an unverified parse is sufficient (and avoids needing WorkOS's
+// JWKS at logout time).
+func sessionIDFromAccessToken(token string) string {
+ claims := jwt.MapClaims{}
+ if _, _, err := jwt.NewParser().ParseUnverified(token, claims); err != nil {
+ return ""
+ }
+ if sid, ok := claims["sid"].(string); ok {
+ return sid
+ }
+ return ""
}
// HandleMe returns the current user info from the authenticated context.
diff --git a/web/src/App.tsx b/web/src/App.tsx
index f9737438..5db25ff7 100644
--- a/web/src/App.tsx
+++ b/web/src/App.tsx
@@ -2,7 +2,6 @@ import { Routes, Route, Navigate } from 'react-router-dom'
import { AuthProvider } from './hooks/useAuth'
import ProtectedRoute from './components/ProtectedRoute'
import Layout from './components/Layout'
-import Login from './pages/Login'
import Dashboard from './pages/Dashboard'
import Sessions from './pages/Sessions'
import APIKeys from './pages/APIKeys'
@@ -16,7 +15,6 @@ export default function App() {
return (
<AuthProvider>
<Routes>
- <Route path="/login" element={<Login />} />
<Route element={<ProtectedRoute />}>
<Route element={<Layout />}>
<Route index element={<Dashboard />} />
diff --git a/web/src/api/client.ts b/web/src/api/client.ts
index 5e39be6b..13357b23 100644
--- a/web/src/api/client.ts
+++ b/web/src/api/client.ts
@@ -38,12 +38,23 @@ export async function apiFetch<T>(path: string, options: RequestInit = {}): Prom
return res.json()
}
-// Logout: clears server session, then navigates to login
+// Logout: the backend clears cookies and returns WorkOS's hosted logout URL,
+// which ends the WorkOS session and redirects to the dashboard-configured
+// Sign-out redirect (so we're not instantly re-logged-in). Navigate there.
+// Fall back to /auth/login only when there was no WorkOS session to end.
export async function logout(): Promise<void> {
- await fetch('/auth/logout', { method: 'POST', credentials: 'include' }).catch(() => {})
+ let dest = '/auth/login'
+ try {
+ const res = await fetch('/auth/logout', { method: 'POST', credentials: 'include' })
+ const body = await res.json().catch(() => ({}))
+ if (body && typeof body.logoutUrl === 'string' && body.logoutUrl) {
+ dest = body.logoutUrl
+ }
+ } catch {
+ // ignore — fall back to /auth/login
+ }
posthog.reset()
- // Navigate to login page — use replace to prevent back-button loop
- window.location.replace('/login')
+ window.location.replace(dest)
}
// API functions
diff --git a/web/src/components/ProtectedRoute.tsx b/web/src/components/ProtectedRoute.tsx
index 0a85c64d..2810ef5b 100644
--- a/web/src/components/ProtectedRoute.tsx
+++ b/web/src/components/ProtectedRoute.tsx
@@ -1,10 +1,21 @@
-import { Navigate, Outlet } from 'react-router-dom'
+import { useEffect } from 'react'
+import { Outlet } from 'react-router-dom'
import { useAuth } from '../hooks/useAuth'
export default function ProtectedRoute() {
const { user, loading } = useAuth()
- if (loading) {
+ // Not authenticated → go straight to WorkOS. /auth/login is a server route
+ // (proxied by Vite) that 302s to the WorkOS hosted login, so we do a
+ // full-page navigation rather than an in-app route. No intermediate
+ // "Sign in" screen — the spinner below shows until the browser leaves.
+ useEffect(() => {
+ if (!loading && !user) {
+ window.location.replace('/auth/login')
+ }
+ }, [loading, user])
+
+ if (loading || !user) {
return (
<div style={{
display: 'flex',
@@ -28,9 +39,5 @@ export default function ProtectedRoute() {
)
}
- if (!user) {
- return <Navigate to="/login" replace />
- }
-
return <Outlet />
}
diff --git a/web/src/pages/Login.tsx b/web/src/pages/Login.tsx
deleted file mode 100644
index 4fca657b..00000000
--- a/web/src/pages/Login.tsx
+++ /dev/null
@@ -1,87 +0,0 @@
-export default function Login() {
- return (
- <div style={{
- display: 'flex',
- flexDirection: 'column',
- alignItems: 'center',
- justifyContent: 'center',
- height: '100vh',
- background: 'var(--bg-void)',
- position: 'relative',
- overflow: 'hidden',
- }}>
- {/* Ambient glow */}
- <div style={{
- position: 'absolute',
- width: 600, height: 600,
- borderRadius: '50%',
- background: 'radial-gradient(circle, rgba(99,102,241,0.08) 0%, transparent 70%)',
- top: '50%', left: '50%',
- transform: 'translate(-50%, -55%)',
- pointerEvents: 'none',
- }} />
-
- {/* Content */}
- <div style={{ position: 'relative', textAlign: 'center' }}>
- {/* Logo */}
- <div style={{
- width: 56, height: 56,
- borderRadius: 16,
- background: 'var(--gradient-primary)',
- display: 'flex', alignItems: 'center', justifyContent: 'center',
- fontSize: 20, fontWeight: 800, color: '#fff',
- fontFamily: 'var(--font-display)',
- margin: '0 auto 24px',
- boxShadow: '0 0 40px rgba(99,102,241,0.3)',
- }}>
- OS
- </div>
-
- <h1 style={{
- fontSize: 34,
- fontWeight: 700,
- fontFamily: 'var(--font-display)',
- letterSpacing: '-0.04em',
- color: 'var(--text-primary)',
- marginBottom: 8,
- }}>
- OpenComputer
- </h1>
-
- <p style={{
- fontSize: 14,
- color: 'var(--text-tertiary)',
- marginBottom: 40,
- maxWidth: 280,
- lineHeight: 1.5,
- }}>
- Sign in to manage your sandbox infrastructure
- </p>
-
- <a
- href="/auth/login"
- className="btn-primary"
- style={{
- display: 'inline-flex',
- padding: '14px 40px',
- fontSize: 15,
- borderRadius: 10,
- textDecoration: 'none',
- }}
- >
- Sign in
- </a>
-
- <div style={{
- marginTop: 48,
- fontSize: 11,
- color: 'var(--text-tertiary)',
- fontFamily: 'var(--font-mono)',
- letterSpacing: '0.04em',
- }}>
- SECURE &middot; GLOBAL &middot; FAST
- </div>
- </div>
- </div>
- )
-}