fix(auth): revoke JWT on logout and harden Google sign-in
Logout now increments sessionVersion so existing JWTs are rejected server-side, deletes orphaned DB sessions, and uses redirectTo for signOut. Google OAuth requests account selection each time; optional AUTH_GOOGLE_PROMPT=login forces Google re-authentication on shared devices. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -24,6 +24,7 @@ NEXTAUTH_URL="http://localhost:3000"
|
|||||||
# Authorized redirect URI: {NEXTAUTH_URL}/api/auth/callback/google
|
# Authorized redirect URI: {NEXTAUTH_URL}/api/auth/callback/google
|
||||||
# AUTH_GOOGLE_ID="....apps.googleusercontent.com"
|
# AUTH_GOOGLE_ID="....apps.googleusercontent.com"
|
||||||
# AUTH_GOOGLE_SECRET="GOCSPX-..."
|
# AUTH_GOOGLE_SECRET="GOCSPX-..."
|
||||||
|
# AUTH_GOOGLE_PROMPT="select_account" # or "login" to force Google password every time
|
||||||
|
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
# AI Providers
|
# AI Providers
|
||||||
|
|||||||
@@ -2,14 +2,20 @@ import { LoginForm } from '@/components/login-form';
|
|||||||
import { getSystemConfig } from '@/lib/config';
|
import { getSystemConfig } from '@/lib/config';
|
||||||
import { isGoogleAuthEnabled } from '@/lib/auth-providers';
|
import { isGoogleAuthEnabled } from '@/lib/auth-providers';
|
||||||
|
|
||||||
export default async function LoginPage() {
|
export default async function LoginPage({
|
||||||
|
searchParams,
|
||||||
|
}: {
|
||||||
|
searchParams: Promise<{ error?: string }>;
|
||||||
|
}) {
|
||||||
const config = await getSystemConfig();
|
const config = await getSystemConfig();
|
||||||
const allowRegister = config.ALLOW_REGISTRATION !== 'false' && process.env.ALLOW_REGISTRATION !== 'false';
|
const allowRegister = config.ALLOW_REGISTRATION !== 'false' && process.env.ALLOW_REGISTRATION !== 'false';
|
||||||
|
const { error: authError } = await searchParams;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<LoginForm
|
<LoginForm
|
||||||
allowRegister={allowRegister}
|
allowRegister={allowRegister}
|
||||||
googleAuthEnabled={isGoogleAuthEnabled()}
|
googleAuthEnabled={isGoogleAuthEnabled()}
|
||||||
|
authError={authError}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { useState } from 'react'
|
|||||||
import { Input } from '@/components/ui/input'
|
import { Input } from '@/components/ui/input'
|
||||||
import { updateProfile, changePassword } from '@/app/actions/profile'
|
import { updateProfile, changePassword } from '@/app/actions/profile'
|
||||||
import { updateUserSettings } from '@/app/actions/user-settings'
|
import { updateUserSettings } from '@/app/actions/user-settings'
|
||||||
import { signOut } from 'next-auth/react'
|
import { performSignOut } from '@/lib/auth-client'
|
||||||
import { toast } from 'sonner'
|
import { toast } from 'sonner'
|
||||||
import { useLanguage } from '@/lib/i18n'
|
import { useLanguage } from '@/lib/i18n'
|
||||||
import { User, Mail, Shield, LogOut, Camera, Bell } from 'lucide-react'
|
import { User, Mail, Shield, LogOut, Camera, Bell } from 'lucide-react'
|
||||||
@@ -143,7 +143,7 @@ export function ProfileForm({ user }: { user: { name: string | null; email: stri
|
|||||||
{/* Logout */}
|
{/* Logout */}
|
||||||
<div className="pt-8 border-t border-border/40">
|
<div className="pt-8 border-t border-border/40">
|
||||||
<button
|
<button
|
||||||
onClick={() => signOut({ callbackUrl: '/login' })}
|
onClick={() => performSignOut('/login')}
|
||||||
className="flex items-center gap-3 px-6 py-3 bg-rose-50 dark:bg-rose-500/10 text-rose-600 rounded-xl font-bold uppercase tracking-widest text-[10px] hover:bg-rose-100 transition-colors"
|
className="flex items-center gap-3 px-6 py-3 bg-rose-50 dark:bg-rose-500/10 text-rose-600 rounded-xl font-bold uppercase tracking-widest text-[10px] hover:bg-rose-100 transition-colors"
|
||||||
>
|
>
|
||||||
<LogOut size={16} />
|
<LogOut size={16} />
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ export const authConfig = {
|
|||||||
trustHost: true,
|
trustHost: true,
|
||||||
session: {
|
session: {
|
||||||
strategy: 'jwt',
|
strategy: 'jwt',
|
||||||
|
maxAge: 60 * 60 * 24 * 7,
|
||||||
|
updateAge: 60 * 60 * 12,
|
||||||
},
|
},
|
||||||
callbacks: {
|
callbacks: {
|
||||||
authorized({ auth, request: { nextUrl } }) {
|
authorized({ auth, request: { nextUrl } }) {
|
||||||
|
|||||||
@@ -19,6 +19,24 @@ export const { auth, signIn, signOut, handlers } = NextAuth({
|
|||||||
data: { role: 'ADMIN', emailVerified: new Date() },
|
data: { role: 'ADMIN', emailVerified: new Date() },
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
async signOut(message) {
|
||||||
|
const userId =
|
||||||
|
'token' in message && message.token?.sub
|
||||||
|
? message.token.sub
|
||||||
|
: 'session' in message && message.session?.userId
|
||||||
|
? message.session.userId
|
||||||
|
: null;
|
||||||
|
|
||||||
|
if (!userId) return;
|
||||||
|
|
||||||
|
await prisma.$transaction([
|
||||||
|
prisma.user.update({
|
||||||
|
where: { id: userId },
|
||||||
|
data: { sessionVersion: { increment: 1 } },
|
||||||
|
}),
|
||||||
|
prisma.session.deleteMany({ where: { userId } }),
|
||||||
|
]);
|
||||||
|
},
|
||||||
},
|
},
|
||||||
callbacks: {
|
callbacks: {
|
||||||
...authConfig.callbacks,
|
...authConfig.callbacks,
|
||||||
@@ -41,18 +59,26 @@ export const { auth, signIn, signOut, handlers } = NextAuth({
|
|||||||
token.id = user.id;
|
token.id = user.id;
|
||||||
const dbUser = await prisma.user.findUnique({
|
const dbUser = await prisma.user.findUnique({
|
||||||
where: { id: user.id },
|
where: { id: user.id },
|
||||||
select: { role: true },
|
select: { role: true, sessionVersion: true },
|
||||||
});
|
});
|
||||||
token.role = dbUser?.role ?? 'USER';
|
if (!dbUser) return null;
|
||||||
|
token.role = dbUser.role;
|
||||||
|
token.sessionVersion = dbUser.sessionVersion;
|
||||||
} else if (token.sub) {
|
} else if (token.sub) {
|
||||||
const dbUser = await prisma.user.findUnique({
|
const dbUser = await prisma.user.findUnique({
|
||||||
where: { id: token.sub },
|
where: { id: token.sub },
|
||||||
select: { role: true },
|
select: { role: true, sessionVersion: true },
|
||||||
});
|
});
|
||||||
if (dbUser) {
|
if (!dbUser) return null;
|
||||||
token.id = token.sub;
|
if (
|
||||||
token.role = dbUser.role;
|
typeof token.sessionVersion === 'number' &&
|
||||||
|
token.sessionVersion !== dbUser.sessionVersion
|
||||||
|
) {
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
token.id = token.sub;
|
||||||
|
token.role = dbUser.role;
|
||||||
|
token.sessionVersion = dbUser.sessionVersion;
|
||||||
}
|
}
|
||||||
return token;
|
return token;
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import { usePathname } from 'next/navigation'
|
import { usePathname } from 'next/navigation'
|
||||||
import { signOut, useSession } from 'next-auth/react'
|
import { useSession } from 'next-auth/react'
|
||||||
|
import { performSignOut } from '@/lib/auth-client'
|
||||||
import {
|
import {
|
||||||
LayoutDashboard,
|
LayoutDashboard,
|
||||||
Users,
|
Users,
|
||||||
@@ -113,7 +114,7 @@ export function AdminSidebar({ className }: { className?: string }) {
|
|||||||
<DropdownMenuSeparator />
|
<DropdownMenuSeparator />
|
||||||
<DropdownMenuItem
|
<DropdownMenuItem
|
||||||
className="cursor-pointer text-destructive focus:text-destructive"
|
className="cursor-pointer text-destructive focus:text-destructive"
|
||||||
onClick={() => signOut({ callbackUrl: '/login' })}
|
onClick={() => performSignOut('/login')}
|
||||||
>
|
>
|
||||||
<LogOut className="mr-2 h-4 w-4" />
|
<LogOut className="mr-2 h-4 w-4" />
|
||||||
{t('auth.signOut')}
|
{t('auth.signOut')}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import { useState } from 'react'
|
import { useState } from 'react'
|
||||||
import { signOut } from 'next-auth/react'
|
import { performSignOut } from '@/lib/auth-client'
|
||||||
import { Loader2, ShieldAlert } from 'lucide-react'
|
import { Loader2, ShieldAlert } from 'lucide-react'
|
||||||
import { toast } from 'sonner'
|
import { toast } from 'sonner'
|
||||||
import { useLanguage } from '@/lib/i18n'
|
import { useLanguage } from '@/lib/i18n'
|
||||||
@@ -42,7 +42,7 @@ export function DeleteAccountDialog({ userEmail, open, onOpenChange }: DeleteAcc
|
|||||||
}
|
}
|
||||||
|
|
||||||
toast.success(t('account.deleteAccount.successRedirect'))
|
toast.success(t('account.deleteAccount.successRedirect'))
|
||||||
await signOut({ callbackUrl: '/login?deleted=true' })
|
await performSignOut('/login?deleted=true')
|
||||||
} catch {
|
} catch {
|
||||||
toast.error(t('account.deleteAccount.errorFailed'))
|
toast.error(t('account.deleteAccount.errorFailed'))
|
||||||
setIsDeleting(false)
|
setIsDeleting(false)
|
||||||
|
|||||||
@@ -44,12 +44,20 @@ function LoginButton() {
|
|||||||
export function LoginForm({
|
export function LoginForm({
|
||||||
allowRegister = true,
|
allowRegister = true,
|
||||||
googleAuthEnabled = false,
|
googleAuthEnabled = false,
|
||||||
|
authError,
|
||||||
}: {
|
}: {
|
||||||
allowRegister?: boolean;
|
allowRegister?: boolean;
|
||||||
googleAuthEnabled?: boolean;
|
googleAuthEnabled?: boolean;
|
||||||
|
authError?: string;
|
||||||
}) {
|
}) {
|
||||||
const [errorMessage, dispatch] = useActionState(authenticate, undefined);
|
const [errorMessage, dispatch] = useActionState(authenticate, undefined);
|
||||||
const { t } = useLanguage();
|
const { t } = useLanguage();
|
||||||
|
const oauthError =
|
||||||
|
authError === 'SessionRequired'
|
||||||
|
? t('auth.sessionExpired')
|
||||||
|
: authError === 'OAuthAccountNotLinked'
|
||||||
|
? t('auth.oauthAccountNotLinked')
|
||||||
|
: authError ?? null;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="bg-white dark:bg-[var(--background)]/50 border border-[var(--border)] p-8 md:p-10 rounded-[48px] shadow-2xl">
|
<div className="bg-white dark:bg-[var(--background)]/50 border border-[var(--border)] p-8 md:p-10 rounded-[48px] shadow-2xl">
|
||||||
@@ -63,6 +71,12 @@ export function LoginForm({
|
|||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{oauthError && (
|
||||||
|
<p className="text-sm text-red-500 text-center px-2" role="alert">
|
||||||
|
{oauthError}
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
|
||||||
{googleAuthEnabled && (
|
{googleAuthEnabled && (
|
||||||
<>
|
<>
|
||||||
<GoogleSignInButton label={t('auth.continueWithGoogle')} />
|
<GoogleSignInButton label={t('auth.continueWithGoogle')} />
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ import {
|
|||||||
DropdownMenuSeparator,
|
DropdownMenuSeparator,
|
||||||
DropdownMenuTrigger,
|
DropdownMenuTrigger,
|
||||||
} from '@/components/ui/dropdown-menu'
|
} from '@/components/ui/dropdown-menu'
|
||||||
import { signOut } from 'next-auth/react'
|
import { performSignOut } from '@/lib/auth-client'
|
||||||
import { useNoteRefresh } from '@/context/NoteRefreshContext'
|
import { useNoteRefresh } from '@/context/NoteRefreshContext'
|
||||||
import { useBrainstormSessions, useDeleteBrainstorm } from '@/hooks/use-brainstorm'
|
import { useBrainstormSessions, useDeleteBrainstorm } from '@/hooks/use-brainstorm'
|
||||||
import { UsageMeter } from './usage-meter'
|
import { UsageMeter } from './usage-meter'
|
||||||
@@ -849,7 +849,7 @@ export function Sidebar({ className, user }: { className?: string; user?: any })
|
|||||||
<DropdownMenuSeparator />
|
<DropdownMenuSeparator />
|
||||||
<DropdownMenuItem
|
<DropdownMenuItem
|
||||||
className="text-destructive focus:text-destructive"
|
className="text-destructive focus:text-destructive"
|
||||||
onClick={() => signOut({ callbackUrl: '/login' })}
|
onClick={() => performSignOut('/login')}
|
||||||
>
|
>
|
||||||
<LogOut className="h-4 w-4 me-2" />
|
<LogOut className="h-4 w-4 me-2" />
|
||||||
{t('sidebar.signOut')}
|
{t('sidebar.signOut')}
|
||||||
|
|||||||
8
memento-note/lib/auth-client.ts
Normal file
8
memento-note/lib/auth-client.ts
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { signOut } from 'next-auth/react';
|
||||||
|
|
||||||
|
/** Ends the Momento session server-side (JWT revoked) and redirects to login. */
|
||||||
|
export async function performSignOut(callbackUrl = '/login') {
|
||||||
|
await signOut({ redirectTo: callbackUrl });
|
||||||
|
}
|
||||||
@@ -9,11 +9,19 @@ export function buildAuthProviders() {
|
|||||||
const providers = [];
|
const providers = [];
|
||||||
|
|
||||||
if (process.env.AUTH_GOOGLE_ID && process.env.AUTH_GOOGLE_SECRET) {
|
if (process.env.AUTH_GOOGLE_ID && process.env.AUTH_GOOGLE_SECRET) {
|
||||||
|
const googlePrompt =
|
||||||
|
process.env.AUTH_GOOGLE_PROMPT === 'login' ? 'login' : 'select_account';
|
||||||
|
|
||||||
providers.push(
|
providers.push(
|
||||||
Google({
|
Google({
|
||||||
clientId: process.env.AUTH_GOOGLE_ID,
|
clientId: process.env.AUTH_GOOGLE_ID,
|
||||||
clientSecret: process.env.AUTH_GOOGLE_SECRET,
|
clientSecret: process.env.AUTH_GOOGLE_SECRET,
|
||||||
allowDangerousEmailAccountLinking: true,
|
authorization: {
|
||||||
|
params: {
|
||||||
|
prompt: googlePrompt,
|
||||||
|
access_type: 'online',
|
||||||
|
},
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,8 @@
|
|||||||
"rememberMe": "Remember me",
|
"rememberMe": "Remember me",
|
||||||
"orContinueWith": "Or continue with",
|
"orContinueWith": "Or continue with",
|
||||||
"continueWithGoogle": "Continue with Google",
|
"continueWithGoogle": "Continue with Google",
|
||||||
|
"sessionExpired": "Your session has expired. Please sign in again.",
|
||||||
|
"oauthAccountNotLinked": "This Google account does not match your existing account. Use the same email or sign in with your password.",
|
||||||
"checkYourEmail": "Check your email",
|
"checkYourEmail": "Check your email",
|
||||||
"resetEmailSent": "We have sent a password reset link to your email address if it exists in our system.",
|
"resetEmailSent": "We have sent a password reset link to your email address if it exists in our system.",
|
||||||
"returnToLogin": "Return to Login",
|
"returnToLogin": "Return to Login",
|
||||||
|
|||||||
@@ -19,6 +19,8 @@
|
|||||||
"rememberMe": "Se souvenir de moi",
|
"rememberMe": "Se souvenir de moi",
|
||||||
"orContinueWith": "Ou continuer avec",
|
"orContinueWith": "Ou continuer avec",
|
||||||
"continueWithGoogle": "Continuer avec Google",
|
"continueWithGoogle": "Continuer avec Google",
|
||||||
|
"sessionExpired": "Votre session a expiré. Connectez-vous à nouveau.",
|
||||||
|
"oauthAccountNotLinked": "Ce compte Google ne correspond pas à votre compte existant. Utilisez le même e-mail ou connectez-vous par mot de passe.",
|
||||||
"checkYourEmail": "Vérifiez votre email",
|
"checkYourEmail": "Vérifiez votre email",
|
||||||
"resetEmailSent": "Nous avons envoyé un lien de réinitialisation à votre adresse email si elle existe dans notre système.",
|
"resetEmailSent": "Nous avons envoyé un lien de réinitialisation à votre adresse email si elle existe dans notre système.",
|
||||||
"returnToLogin": "Retour à la connexion",
|
"returnToLogin": "Retour à la connexion",
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
-- Invalidate all JWT sessions on logout by bumping sessionVersion (additive, no data loss)
|
||||||
|
ALTER TABLE "User" ADD COLUMN IF NOT EXISTS "sessionVersion" INTEGER NOT NULL DEFAULT 0;
|
||||||
@@ -15,6 +15,7 @@ model User {
|
|||||||
emailVerified DateTime?
|
emailVerified DateTime?
|
||||||
password String?
|
password String?
|
||||||
role String @default("USER")
|
role String @default("USER")
|
||||||
|
sessionVersion Int @default(0)
|
||||||
image String?
|
image String?
|
||||||
theme String @default("light")
|
theme String @default("light")
|
||||||
resetToken String? @unique
|
resetToken String? @unique
|
||||||
|
|||||||
Reference in New Issue
Block a user