Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(auth): send already verified users on old verify links to login (#130349)

Rafael Audibert0df300d698ec
diff --git a/frontend/src/scenes/authentication/verify-email/verifyEmailLogic.test.ts b/frontend/src/scenes/authentication/verify-email/verifyEmailLogic.test.ts
index e1b7cb236496..716c28a64134 100644
--- a/frontend/src/scenes/authentication/verify-email/verifyEmailLogic.test.ts
+++ b/frontend/src/scenes/authentication/verify-email/verifyEmailLogic.test.ts
@@ -3,6 +3,9 @@ import { router } from 'kea-router'
import { expectLogic } from 'kea-test-utils'
import posthog from 'posthog-js'
+import { urls } from 'scenes/urls'
+
+import { useMocks } from '~/mocks/jest'
import { initKeaTests } from '~/test/init'
import { verifyEmailLogic } from './verifyEmailLogic'
@@ -55,4 +58,23 @@ describe('verifyEmailLogic', () => {
await expectLogic(logic).toMatchValues({ verificationEmailSent: false })
})
+
+ it('sends an already verified user from an old link to login, keeping next', async () => {
+ useMocks({
+ post: {
+ '/api/users/request_email_verification/': () => [
+ 400,
+ { type: 'validation_error', code: 'already_verified', detail: 'Email is already verified.' },
+ ],
+ },
+ })
+ router.actions.push('/verify_email/abc-123/old-token', { next: '/insights' })
+
+ await expectLogic(logic, () => logic.actions.requestVerificationCode('abc-123')).toDispatchActions([
+ 'requestVerificationCodeSuccess',
+ ])
+
+ expect(router.values.location.pathname).toEqual(urls.login())
+ expect(router.values.searchParams).toEqual({ next: '/insights' })
+ })
})
diff --git a/frontend/src/scenes/authentication/verify-email/verifyEmailLogic.ts b/frontend/src/scenes/authentication/verify-email/verifyEmailLogic.ts
index b7113b717b26..9f2585455127 100644
--- a/frontend/src/scenes/authentication/verify-email/verifyEmailLogic.ts
+++ b/frontend/src/scenes/authentication/verify-email/verifyEmailLogic.ts
@@ -37,6 +37,12 @@ const resolvePostVerifyDefault = (values: {
return hasCompletedSomething ? urls.default() : urls.onboarding()
}
+const redirectToLogin = (message: string): void => {
+ const nextUrl = getRelativeNextPath(router.values.searchParams.next, location)
+ lemonToast.success(message)
+ router.actions.push(urls.login(), nextUrl ? { next: nextUrl } : {})
+}
+
// A verify success without a session (2FA, blocked member, SSO-enforced domain) routes to
// login with a matching toast. Otherwise the user goes straight into the app.
const redirectAfterVerification = (
@@ -52,8 +58,7 @@ const redirectAfterVerification = (
? 'Email verified! Log in with SSO to continue.'
: null
if (loginMessage) {
- lemonToast.success(loginMessage)
- router.actions.push(urls.login(), nextUrl ? { next: nextUrl } : {})
+ redirectToLogin(loginMessage)
return
}
// this url is validated in getRelativeNextPath as either being relative or on the same origin
@@ -219,6 +224,12 @@ export const verifyEmailLogic = kea<verifyEmailLogicType>([
actions.setView('pending')
return true
} catch (e: any) {
+ // Old link emails reach this page for accounts that are already verified, and
+ // the backend won't send them a code, so login is the only way forward.
+ if (e.code === 'already_verified') {
+ redirectToLogin('Your email is already verified. Log in to continue.')
+ return false
+ }
if (e.code === 'throttled') {
lemonToast.error('You have requested a new code too many times. Please try again later.')
return false