-
-
Notifications
You must be signed in to change notification settings - Fork 6
Ng/feat/auth/last login method badge #460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ViktorSvertoka
merged 5 commits into
DevLoversTeam:develop
from
nazar-gavrylyk:ng/feat/auth/last-login-method-badge
Aug 16, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b263bb1
feat(messages): Add lastUsed translations
nazar-gavrylyk 13c5f46
feat(auth): Add last login badge
nazar-gavrylyk b0668c7
feat(auth): store last login method
nazar-gavrylyk cd75e86
refactor(auth): Export last login methods
nazar-gavrylyk d1c5247
fix(auth): harden redirect and badge accessibility
ViktorSvertoka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,25 @@ | ||
| 'use client'; | ||
|
|
||
| import { useSearchParams } from 'next/navigation'; | ||
| import { useLocale } from 'next-intl'; | ||
|
|
||
| import { LoginForm } from '@/components/auth/LoginForm'; | ||
| import { getSafeRedirect } from '@/lib/auth/safe-redirect'; | ||
| import { getLastLoginMethod } from '@/lib/auth-last-login'; | ||
|
|
||
| export default function LoginPage() { | ||
| const locale = useLocale(); | ||
| const searchParams = useSearchParams(); | ||
| export default async function LoginPage({ | ||
| params, | ||
| searchParams, | ||
| }: { | ||
| params: Promise<{ locale: string }>; | ||
| searchParams: Promise<{ returnTo?: string | string[] }>; | ||
| }) { | ||
| const { locale } = await params; | ||
| const { returnTo: returnToParam } = await searchParams; | ||
|
|
||
| const returnTo = getSafeRedirect(searchParams.get('returnTo')); | ||
| const returnTo = getSafeRedirect(returnToParam); | ||
| const lastLoginMethod = await getLastLoginMethod(); | ||
|
|
||
| return <LoginForm locale={locale} returnTo={returnTo} />; | ||
| return ( | ||
| <LoginForm | ||
| locale={locale} | ||
| returnTo={returnTo} | ||
| lastLoginMethod={lastLoginMethod} | ||
| /> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { useTranslations } from 'next-intl'; | ||
|
|
||
| import { Badge } from '@/components/ui/badge'; | ||
|
|
||
| export function LastLoginBadge({ id }: { id?: string }) { | ||
| const t = useTranslations('auth.login'); | ||
|
|
||
| return ( | ||
| <Badge | ||
| id={id} | ||
| variant="success" | ||
| className="pointer-events-none absolute -top-1.5 -right-1.5 z-10 dark:bg-green-900" | ||
| > | ||
| {t('lastUsed')} | ||
| </Badge> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /** | ||
| * @vitest-environment jsdom | ||
| */ | ||
| import { render, screen } from '@testing-library/react'; | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { LastLoginBadge } from '@/components/auth/LastLoginBadge'; | ||
| import { ProviderButton } from '@/components/auth/ProviderButton'; | ||
|
|
||
| vi.mock('next-intl', () => ({ | ||
| useTranslations: () => (key: string) => key, | ||
| })); | ||
|
|
||
| describe('LastLoginBadge', () => { | ||
| it('renders the "last used" label', () => { | ||
| render(<LastLoginBadge />); | ||
| expect(screen.getByText('lastUsed')).toBeDefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('ProviderButton', () => { | ||
| it('shows the badge when isLastUsed is true', () => { | ||
| render( | ||
| <ProviderButton | ||
| provider="google" | ||
| label="Continue with Google" | ||
| icon={null} | ||
| isLastUsed | ||
| /> | ||
| ); | ||
| expect(screen.getByText('lastUsed')).toBeDefined(); | ||
| expect( | ||
| screen.getByRole('button', { name: 'Continue with Google' }) | ||
| ).toHaveAccessibleDescription('lastUsed'); | ||
| }); | ||
|
|
||
| it('hides the badge when isLastUsed is false', () => { | ||
| render( | ||
| <ProviderButton | ||
| provider="github" | ||
| label="Continue with GitHub" | ||
| icon={null} | ||
| /> | ||
| ); | ||
| expect(screen.queryByText('lastUsed')).toBeNull(); | ||
| expect( | ||
| screen.getByRole('button', { name: 'Continue with GitHub' }) | ||
| ).not.toHaveAttribute('aria-describedby'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import 'server-only'; | ||
|
|
||
| import { cookies } from 'next/headers'; | ||
|
|
||
| const LAST_LOGIN_COOKIE = 'last_login_method'; | ||
| const LAST_LOGIN_MAX_AGE = 60 * 60 * 24 * 365; | ||
|
|
||
| export const LAST_LOGIN_METHODS = ['email', 'google', 'github'] as const; | ||
| export type LastLoginMethod = (typeof LAST_LOGIN_METHODS)[number]; | ||
|
|
||
| export async function setLastLoginMethodCookie(method: LastLoginMethod) { | ||
| const cookieStore = await cookies(); | ||
|
|
||
| cookieStore.set(LAST_LOGIN_COOKIE, method, { | ||
| httpOnly: true, | ||
| sameSite: 'lax', | ||
| secure: process.env.NODE_ENV === 'production', | ||
| path: '/', | ||
| maxAge: LAST_LOGIN_MAX_AGE, | ||
| }); | ||
| } | ||
|
|
||
| export async function getLastLoginMethod(): Promise<LastLoginMethod | null> { | ||
| const cookieStore = await cookies(); | ||
|
|
||
| const value = cookieStore.get(LAST_LOGIN_COOKIE)?.value; | ||
|
|
||
| return LAST_LOGIN_METHODS.includes(value as LastLoginMethod) | ||
| ? (value as LastLoginMethod) | ||
| : null; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { getSafeRedirect } from '@/lib/auth/safe-redirect'; | ||
|
|
||
| describe('getSafeRedirect', () => { | ||
| it('allows a safe internal path', () => { | ||
| expect(getSafeRedirect('/dashboard')).toBe('/dashboard'); | ||
| }); | ||
|
|
||
| it('rejects duplicate returnTo query values', () => { | ||
| expect(getSafeRedirect(['/dashboard', '/shop'])).toBe(''); | ||
| }); | ||
|
|
||
| it('rejects an external redirect', () => { | ||
| expect(getSafeRedirect('https://example.com')).toBe(''); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔒 Security & Privacy | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 25311
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 1201
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 673
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 13655
Validate
returnTowith the URL parser.'/\n//evil.example'passes the current checks.LoginFormassigns it towindow.location.href, which resolves it tohttps://evil.example/. Parse against the configured application origin and reject a different origin. Keep rejectingraw.startsWith('//'); the proposedhttps://internal.invalidbase accepts//internal.invalid/pathand redirects away fromhttps://devlovers.net. Add regression tests for both payloads.🤖 Prompt for AI Agents