Skip to content

Commit f08c50f

Browse files
fix(auth): clear stale session before login/signup to stop refresh token loop
When users land on /login or /sign-up with expired auth cookies, the Supabase client singleton starts an auto-refresh loop with the stale token (400 → 429 endlessly). The middleware fix clears cookies for the next page load, but can't stop a JS loop already running in the browser. Fix: call signOut({ scope: 'local' }) before signInWithPassword, signUp, and signInWithOAuth to clear cached stale tokens from the Supabase client's memory and cookies before attempting authentication. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 05d50ec commit f08c50f

2 files changed

Lines changed: 9 additions & 0 deletions

File tree

components/login-form.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ export function LoginForm({ className, tenantId, ...props }: LoginFormProps) {
4545
setError(null)
4646

4747
try {
48+
// Clear any stale session before login. When a user lands on /login with
49+
// expired cookies, the Supabase client's auto-refresh ticker may already be
50+
// retrying with the old refresh token. Signing out locally clears the cached
51+
// tokens and stops the loop, giving signInWithPassword a clean slate.
52+
await supabase.auth.signOut({ scope: 'local' })
53+
4854
const { error } = await supabase.auth.signInWithPassword({
4955
email,
5056
password,
@@ -87,6 +93,7 @@ export function LoginForm({ className, tenantId, ...props }: LoginFormProps) {
8793
setError(null)
8894

8995
try {
96+
await supabase.auth.signOut({ scope: 'local' })
9097
const { error } = await supabase.auth.signInWithOAuth({
9198
provider: 'google',
9299
options: {

components/sign-up-form.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export function SignUpForm({ className, tenantId, ...props }: SignUpFormProps) {
5353
}
5454

5555
try {
56+
await supabase.auth.signOut({ scope: 'local' })
5657
const { error } = await supabase.auth.signUp({
5758
email,
5859
password,
@@ -78,6 +79,7 @@ export function SignUpForm({ className, tenantId, ...props }: SignUpFormProps) {
7879
setError(null)
7980

8081
try {
82+
await supabase.auth.signOut({ scope: 'local' })
8183
const { error } = await supabase.auth.signInWithOAuth({
8284
provider: 'google',
8385
options: {

0 commit comments

Comments
 (0)