Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

feat: Display last used auth method

Blue Mouse293477d4b00d
diff --git a/web/app/components/AuthLastUsedBadge.tsx b/web/app/components/AuthLastUsedBadge.tsx
new file mode 100644
index 000000000..816f9b07d
--- /dev/null
+++ b/web/app/components/AuthLastUsedBadge.tsx
@@ -0,0 +1,25 @@
+import { memo } from 'react'
+import { useTranslation } from 'react-i18next'
+
+import { Badge } from '~/ui/Badge'
+import { cn } from '~/utils/generic'
+
+interface AuthLastUsedBadgeProps {
+ className?: string
+}
+
+const AuthLastUsedBadge = ({ className }: AuthLastUsedBadgeProps) => {
+ const { t } = useTranslation('common')
+
+ return (
+ <Badge
+ label={t('auth.common.lastUsed')}
+ colour='slate'
+ size='sm'
+ // Using custom background colour to avoid ugly transparency effect with auth buttons
+ className={cn('shrink-0 leading-4 dark:bg-slate-950', className)}
+ />
+ )
+}
+
+export default memo(AuthLastUsedBadge)
diff --git a/web/app/components/GithubAuth.tsx b/web/app/components/GithubAuth.tsx
index 8164f0ac7..d815af160 100644
--- a/web/app/components/GithubAuth.tsx
+++ b/web/app/components/GithubAuth.tsx
@@ -1,25 +1,32 @@
import cx from 'clsx'
import { useTranslation } from 'react-i18next'
+import AuthLastUsedBadge from '~/components/AuthLastUsedBadge'
import { useTheme } from '~/providers/ThemeProvider'
import Button from '~/ui/Button'
import GithubDarkSVG from '~/ui/icons/GithubDark'
import GithubLightSVG from '~/ui/icons/GithubLight'
-interface GoogleAuthProps {
+interface GithubAuthProps {
className?: string
onClick: () => void
disabled?: boolean
+ lastUsed?: boolean
}
-const GithubAuth = ({ className, onClick, disabled }: GoogleAuthProps) => {
+const GithubAuth = ({
+ className,
+ onClick,
+ disabled,
+ lastUsed,
+}: GithubAuthProps) => {
const { t } = useTranslation()
const { theme } = useTheme()
return (
<Button
variant='secondary'
- className={cx(className, 'flex items-center justify-center')}
+ className={cx(className, 'relative flex items-center justify-center')}
onClick={onClick}
disabled={disabled}
>
@@ -30,6 +37,9 @@ const GithubAuth = ({ className, onClick, disabled }: GoogleAuthProps) => {
<GithubDarkSVG className='mr-2 size-5' />
)}
{t('auth.common.github')}
+ {lastUsed ? (
+ <AuthLastUsedBadge className='pointer-events-none absolute -top-2 right-2' />
+ ) : null}
</>
</Button>
)
diff --git a/web/app/components/GoogleAuth.tsx b/web/app/components/GoogleAuth.tsx
index 22340bb61..5d7271644 100644
--- a/web/app/components/GoogleAuth.tsx
+++ b/web/app/components/GoogleAuth.tsx
@@ -1,6 +1,7 @@
import cx from 'clsx'
import { useTranslation } from 'react-i18next'
+import AuthLastUsedBadge from '~/components/AuthLastUsedBadge'
import Button from '~/ui/Button'
import GoogleGSVG from '~/ui/icons/GoogleG'
@@ -8,21 +9,30 @@ interface GoogleAuthProps {
onClick: () => void
className?: string
disabled?: boolean
+ lastUsed?: boolean
}
-const GoogleAuth = ({ onClick, className, disabled }: GoogleAuthProps) => {
+const GoogleAuth = ({
+ onClick,
+ className,
+ disabled,
+ lastUsed,
+}: GoogleAuthProps) => {
const { t } = useTranslation()
return (
<Button
variant='secondary'
- className={cx(className, 'flex items-center justify-center')}
+ className={cx(className, 'relative flex items-center justify-center')}
onClick={onClick}
disabled={disabled}
>
<>
<GoogleGSVG className='mr-2 size-5' />
{t('auth.common.google')}
+ {lastUsed ? (
+ <AuthLastUsedBadge className='pointer-events-none absolute -top-2 right-2' />
+ ) : null}
</>
</Button>
)
diff --git a/web/app/components/OIDCAuth.tsx b/web/app/components/OIDCAuth.tsx
index 6e1e9ba52..0b3c5f1fb 100644
--- a/web/app/components/OIDCAuth.tsx
+++ b/web/app/components/OIDCAuth.tsx
@@ -2,26 +2,36 @@ import cx from 'clsx'
import { KeyIcon } from '@phosphor-icons/react'
import { useTranslation } from 'react-i18next'
+import AuthLastUsedBadge from '~/components/AuthLastUsedBadge'
import Button from '~/ui/Button'
interface OIDCAuthProps {
onClick: () => void
className?: string
disabled?: boolean
+ lastUsed?: boolean
}
-const OIDCAuth = ({ onClick, className, disabled }: OIDCAuthProps) => {
+const OIDCAuth = ({
+ onClick,
+ className,
+ disabled,
+ lastUsed,
+}: OIDCAuthProps) => {
const { t } = useTranslation()
return (
<Button
variant='secondary'
- className={cx(className, 'flex items-center justify-center')}
+ className={cx(className, 'relative flex items-center justify-center')}
onClick={onClick}
disabled={disabled}
>
<KeyIcon className='mr-2 size-5' />
{t('auth.common.oidc')}
+ {lastUsed ? (
+ <AuthLastUsedBadge className='pointer-events-none absolute -top-2 right-2' />
+ ) : null}
</Button>
)
}
diff --git a/web/app/hooks/useLastAuthMethod.ts b/web/app/hooks/useLastAuthMethod.ts
new file mode 100644
index 000000000..3d319d4a2
--- /dev/null
+++ b/web/app/hooks/useLastAuthMethod.ts
@@ -0,0 +1,21 @@
+import { useEffect, useState } from 'react'
+
+import {
+ isLastAuthMethod,
+ LAST_AUTH_METHOD_COOKIE,
+ type LastAuthMethod,
+} from '~/utils/authMethod'
+import { getCookie } from '~/utils/cookie'
+
+export const useLastAuthMethod = () => {
+ const [lastAuthMethod, setLastAuthMethod] = useState<LastAuthMethod | null>(
+ null,
+ )
+
+ useEffect(() => {
+ const authMethod = getCookie(LAST_AUTH_METHOD_COOKIE)
+ setLastAuthMethod(isLastAuthMethod(authMethod) ? authMethod : null)
+ }, [])
+
+ return lastAuthMethod
+}
diff --git a/web/app/pages/Auth/Signin/Signin.tsx b/web/app/pages/Auth/Signin/Signin.tsx
index 6f20f414b..205f85bc1 100644
--- a/web/app/pages/Auth/Signin/Signin.tsx
+++ b/web/app/pages/Auth/Signin/Signin.tsx
@@ -11,16 +11,19 @@ import {
} from 'react-router'
import { toast } from 'sonner'
+import AuthLastUsedBadge from '~/components/AuthLastUsedBadge'
import GithubAuth from '~/components/GithubAuth'
import GoogleAuth from '~/components/GoogleAuth'
import OIDCAuth from '~/components/OIDCAuth'
import { useAuthProxy } from '~/hooks/useAuthProxy'
+import { useLastAuthMethod } from '~/hooks/useLastAuthMethod'
import { isSelfhosted, TRIAL_DAYS } from '~/lib/constants'
-import { decidePostAuthRedirect } from '~/utils/auth'
import { SSOProvider, SSOHashSuccessResponse } from '~/lib/models/Auth'
import { useAuth } from '~/providers/AuthProvider'
import { useTheme } from '~/providers/ThemeProvider'
import type { LoginActionData } from '~/routes/login'
+import { decidePostAuthRedirect } from '~/utils/auth'
+import { isLastAuthMethod, type LastAuthMethod } from '~/utils/authMethod'
import Button from '~/ui/Button'
import Checkbox from '~/ui/Checkbox'
import Input from '~/ui/Input'
@@ -48,8 +51,11 @@ const Signin = () => {
const [searchParams] = useSearchParams()
const { t, i18n } = useTranslation('common')
const { theme } = useTheme()
+ const lastAuthMethod = useLastAuthMethod()
const [dontRemember, setDontRemember] = useState(false)
+ const [pendingAuthMethod, setPendingAuthMethod] =
+ useState<LastAuthMethod | null>(null)
const [isSsoLoading, setIsSsoLoading] = useState(false)
@@ -80,6 +86,14 @@ const Signin = () => {
searchParams.get('show_2fa_screen') === 'true' ||
actionData?.requires2FA === true ||
sso2FARequired
+ const authMethodParam = searchParams.get('auth_method')
+ const authMethodFromParams = isLastAuthMethod(authMethodParam)
+ ? authMethodParam
+ : null
+ const twoFAAuthMethod =
+ authMethodFromParams ||
+ pendingAuthMethod ||
+ (actionData?.requires2FA ? 'email' : null)
// Check if we're in the linking flow
const isLinkingRequired = linkingData !== null
@@ -117,6 +131,7 @@ const Signin = () => {
const handleFormSubmit = () => {
setClearedErrors(new Set())
+ setPendingAuthMethod('email')
}
const handle2FAInput = (event: React.ChangeEvent<HTMLInputElement>) => {
@@ -218,6 +233,7 @@ const Signin = () => {
if (user.isTwoFactorAuthenticationEnabled) {
setUser(user)
+ setPendingAuthMethod(provider)
setSso2FARequired(true)
setIsSsoLoading(false)
return
@@ -262,6 +278,9 @@ const Signin = () => {
formData.set('intent', 'submit-2fa')
formData.set('twoFACode', twoFACode)
formData.set('dontRemember', dontRemember.toString())
+ if (twoFAAuthMethod) {
+ formData.set('authMethod', twoFAAuthMethod)
+ }
twoFAFetcher.submit(formData, { method: 'post' })
}
@@ -483,16 +502,19 @@ const Signin = () => {
onClick={() => onSsoLogin('openid-connect')}
disabled={isLoading}
className='w-full'
+ lastUsed={lastAuthMethod === 'openid-connect'}
/>
) : (
<>
<GoogleAuth
onClick={() => onSsoLogin('google')}
disabled={isLoading}
+ lastUsed={lastAuthMethod === 'google'}
/>
<GithubAuth
onClick={() => onSsoLogin('github')}
disabled={isLoading}
+ lastUsed={lastAuthMethod === 'github'}
/>
</>
)}
@@ -510,9 +532,10 @@ const Signin = () => {
as='span'
colour='muted'
size='sm'
- className='bg-gray-50 px-4 dark:bg-slate-950'
+ className='inline-flex items-center gap-2 bg-gray-50 px-4 dark:bg-slate-950'
>
{t('auth.common.orContinueWith')} email
+ {lastAuthMethod === 'email' ? <AuthLastUsedBadge /> : null}
</Text>
</div>
</div>
diff --git a/web/app/pages/Auth/Signup/Signup.tsx b/web/app/pages/Auth/Signup/Signup.tsx
index 9e56ea2a0..db3adfc06 100644
--- a/web/app/pages/Auth/Signup/Signup.tsx
+++ b/web/app/pages/Auth/Signup/Signup.tsx
@@ -11,10 +11,12 @@ import { Link } from '~/ui/Link'
import { useNavigate, Form, useActionData, useNavigation } from 'react-router'
import { toast } from 'sonner'
+import AuthLastUsedBadge from '~/components/AuthLastUsedBadge'
import GithubAuth from '~/components/GithubAuth'
import GoogleAuth from '~/components/GoogleAuth'
import OIDCAuth from '~/components/OIDCAuth'
import { useAuthProxy } from '~/hooks/useAuthProxy'
+import { useLastAuthMethod } from '~/hooks/useLastAuthMethod'
import {
HAVE_I_BEEN_PWNED_URL,
isSelfhosted,
@@ -57,6 +59,7 @@ const Signup = () => {
const navigate = useNavigate()
const navigation = useNavigation()
const actionData = useActionData<SignupActionData>()
+ const lastAuthMethod = useLastAuthMethod()
const [tos, setTos] = useState(false)
const [checkIfLeaked, setCheckIfLeaked] = useState(true)
@@ -191,7 +194,7 @@ const Signup = () => {
setUser(user)
navigate(
localiseTo(
- `${routes.signin}?show_2fa_screen=true`,
+ `${routes.signin}?show_2fa_screen=true&auth_method=${provider}`,
i18n.language,
),
)
@@ -259,16 +262,19 @@ const Signup = () => {
onClick={() => onSsoLogin('openid-connect')}
disabled={isLoading}
className='w-full'
+ lastUsed={lastAuthMethod === 'openid-connect'}
/>
) : (
<>
<GoogleAuth
onClick={() => onSsoLogin('google')}
disabled={isLoading}
+ lastUsed={lastAuthMethod === 'google'}
/>
<GithubAuth
onClick={() => onSsoLogin('github')}
disabled={isLoading}
+ lastUsed={lastAuthMethod === 'github'}
/>
</>
)}
@@ -286,9 +292,10 @@ const Signup = () => {
as='span'
colour='muted'
size='sm'
- className='bg-gray-50 px-4 dark:bg-slate-950'
+ className='inline-flex items-center gap-2 bg-gray-50 px-4 dark:bg-slate-950'
>
{t('auth.common.orContinueWith')} email
+ {lastAuthMethod === 'email' ? <AuthLastUsedBadge /> : null}
</Text>
</div>
</div>
diff --git a/web/app/routes/api.auth.ts b/web/app/routes/api.auth.ts
index c5a1b1987..507c8b3e5 100644
--- a/web/app/routes/api.auth.ts
+++ b/web/app/routes/api.auth.ts
@@ -21,7 +21,10 @@ import {
type AuthMeResponse,
type LinkSSOWithPasswordParams,
} from '~/api/api.server'
-import { createAuthCookies } from '~/utils/session.server'
+import {
+ createAuthCookies,
+ createLastAuthMethodCookie,
+} from '~/utils/session.server'
interface ProxyRequest {
action:
@@ -97,11 +100,16 @@ export async function action({ request }: ActionFunctionArgs) {
// Only set cookies if this is a success response (not linking required)
if ('accessToken' in result.data) {
const { accessToken, refreshToken } = result.data
+ const remember = body.remember ?? false
const cookies = createAuthCookies(
{ accessToken, refreshToken },
- body.remember ?? false,
+ remember,
)
+ if (!result.data.user.isTwoFactorAuthenticationEnabled) {
+ cookies.push(createLastAuthMethodCookie(body.provider, remember))
+ }
+
return data<ProxyResponse<SSOHashResponse>>(
{
data: result.data,
@@ -278,10 +286,11 @@ export async function action({ request }: ActionFunctionArgs) {
if (result.data && 'accessToken' in result.data) {
const { accessToken, refreshToken } =
result.data as SSOHashSuccessResponse
- const cookies = createAuthCookies(
- { accessToken, refreshToken },
- body.remember ?? false,
- )
+ const remember = body.remember ?? false
+ const cookies = [
+ ...createAuthCookies({ accessToken, refreshToken }, remember),
+ createLastAuthMethodCookie(body.provider, remember),
+ ]
return data<ProxyResponse<SSOHashResponse>>(
{
diff --git a/web/app/routes/login.tsx b/web/app/routes/login.tsx
index 6e0c18e6e..5effdb01c 100644
--- a/web/app/routes/login.tsx
+++ b/web/app/routes/login.tsx
@@ -11,10 +11,12 @@ import { getAuthenticatedUser, loginUser, serverFetch } from '~/api/api.server'
import { getOgImageUrl } from '~/lib/constants'
import Signin from '~/pages/Auth/Signin'
import { decidePostAuthRedirect } from '~/utils/auth'
+import { isLastAuthMethod } from '~/utils/authMethod'
import { getDescription, getPreviewImage, getTitle } from '~/utils/seo'
import {
createHeadersWithCookies,
createAuthCookies,
+ createLastAuthMethodCookie,
} from '~/utils/session.server'
export const meta: MetaFunction = () => {
@@ -66,6 +68,7 @@ export async function action({ request }: ActionFunctionArgs) {
const twoFactorAuthenticationCode =
formData.get('twoFACode')?.toString() || ''
const dontRemember = formData.get('dontRemember') === 'true'
+ const authMethod = formData.get('authMethod')?.toString()
if (
!twoFactorAuthenticationCode ||
@@ -111,6 +114,10 @@ export async function action({ request }: ActionFunctionArgs) {
!dontRemember,
)
+ if (isLastAuthMethod(authMethod)) {
+ cookies.push(createLastAuthMethodCookie(authMethod, !dontRemember))
+ }
+
return redirect(user ? decidePostAuthRedirect(user) : '/dashboard', {
headers: createHeadersWithCookies(cookies),
})
@@ -153,7 +160,12 @@ export async function action({ request }: ActionFunctionArgs) {
return redirect(
result.data?.user ? decidePostAuthRedirect(result.data.user) : '/dashboard',
- { headers: createHeadersWithCookies(result.cookies) },
+ {
+ headers: createHeadersWithCookies([
+ ...result.cookies,
+ createLastAuthMethodCookie('email', !dontRemember),
+ ]),
+ },
)
}
diff --git a/web/app/routes/signup.tsx b/web/app/routes/signup.tsx
index 571f365bc..6093d036d 100644
--- a/web/app/routes/signup.tsx
+++ b/web/app/routes/signup.tsx
@@ -12,7 +12,10 @@ import { getAuthenticatedUser, registerUser } from '~/api/api.server'
import { getOgImageUrl, isSelfhosted } from '~/lib/constants'
import Signup from '~/pages/Auth/Signup'
import { getDescription, getPreviewImage, getTitle } from '~/utils/seo'
-import { createHeadersWithCookies } from '~/utils/session.server'
+import {
+ createHeadersWithCookies,
+ createLastAuthMethodCookie,
+} from '~/utils/session.server'
import { MAX_PASSWORD_CHARS } from '~/utils/validator'
export const meta: MetaFunction = () => {
@@ -104,7 +107,10 @@ export async function action({ request }: ActionFunctionArgs) {
}
return redirect('/onboarding', {
- headers: createHeadersWithCookies(result.cookies),
+ headers: createHeadersWithCookies([
+ ...result.cookies,
+ createLastAuthMethodCookie('email'),
+ ]),
})
}
diff --git a/web/app/routes/signup_.invitation.$id.tsx b/web/app/routes/signup_.invitation.$id.tsx
index a60baca96..2acfdb6a7 100644
--- a/web/app/routes/signup_.invitation.$id.tsx
+++ b/web/app/routes/signup_.invitation.$id.tsx
@@ -16,7 +16,10 @@ import {
import { getOgImageUrl, isSelfhosted } from '~/lib/constants'
import InvitationSignup from '~/pages/Auth/Signup/InvitationSignup'
import { getDescription, getPreviewImage, getTitle } from '~/utils/seo'
-import { createHeadersWithCookies } from '~/utils/session.server'
+import {
+ createHeadersWithCookies,
+ createLastAuthMethodCookie,
+} from '~/utils/session.server'
import { MAX_PASSWORD_CHARS } from '~/utils/validator'
export const meta: MetaFunction = () => {
@@ -149,7 +152,10 @@ export async function action({ request, params }: ActionFunctionArgs) {
}
return redirect('/dashboard', {
- headers: createHeadersWithCookies(result.cookies),
+ headers: createHeadersWithCookies([
+ ...result.cookies,
+ createLastAuthMethodCookie('email'),
+ ]),
})
}
diff --git a/web/app/utils/authMethod.ts b/web/app/utils/authMethod.ts
new file mode 100644
index 000000000..09a311ff4
--- /dev/null
+++ b/web/app/utils/authMethod.ts
@@ -0,0 +1,16 @@
+export const LAST_AUTH_METHOD_COOKIE = 'swx_last_auth_method'
+export const LAST_AUTH_METHOD_MAX_AGE = 31536000
+
+export const LAST_AUTH_METHODS = [
+ 'email',
+ 'google',
+ 'github',
+ 'openid-connect',
+] as const
+
+export type LastAuthMethod = (typeof LAST_AUTH_METHODS)[number]
+
+export const isLastAuthMethod = (
+ method: string | null | undefined,
+): method is LastAuthMethod =>
+ LAST_AUTH_METHODS.some((authMethod) => authMethod === method)
diff --git a/web/app/utils/session.server.ts b/web/app/utils/session.server.ts
index 6398ceb51..a0d5bbeab 100644
--- a/web/app/utils/session.server.ts
+++ b/web/app/utils/session.server.ts
@@ -1,5 +1,11 @@
import { redirect } from 'react-router'
+import {
+ LAST_AUTH_METHOD_COOKIE,
+ LAST_AUTH_METHOD_MAX_AGE,
+ type LastAuthMethod,
+} from '~/utils/authMethod'
+
const ACCESS_TOKEN_COOKIE = 'access_token'
const REFRESH_TOKEN_COOKIE = 'refresh_token'
@@ -148,6 +154,17 @@ export function createAuthCookies(
return cookies
}
+export function createLastAuthMethodCookie(
+ method: LastAuthMethod,
+ remember = true,
+): string {
+ return buildCookieHeader(LAST_AUTH_METHOD_COOKIE, method, {
+ maxAge: remember ? LAST_AUTH_METHOD_MAX_AGE : undefined,
+ sameSite: 'Lax',
+ httpOnly: false,
+ })
+}
+
/**
* @returns Array of Set-Cookie header values that expire the cookies
*/
diff --git a/web/public/locales/en.json b/web/public/locales/en.json
index bd78ebafb..5252730d0 100644
--- a/web/public/locales/en.json
+++ b/web/public/locales/en.json
@@ -828,6 +828,7 @@
"google": "Google",
"github": "Github",
"oidc": "OpenID Connect",
+ "lastUsed": "Last used",
"orContinueWith": "Or continue with"
},
"passwordStrength": {