Skip to content

Instantly share code, notes, and snippets.

@solace
Created November 12, 2023 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save solace/0414f12616f3fb362787d35a7b499d31 to your computer and use it in GitHub Desktop.
Save solace/0414f12616f3fb362787d35a7b499d31 to your computer and use it in GitHub Desktop.
NextJS middleware to fix "Invalid Refresh Token: Already Used" from supabase
// Requested fix for https://github.com/supabase/gotrue/issues/1290
export async function middleware(req: NextRequest) {
const res = NextResponse.next();
const supabase = createMiddlewareClient({req, res});
const {error} = await supabase.auth.getSession();
// Trying to fix "Invalid Refresh Token: Already Used"
// https://github.com/vercel/next.js/issues/40146#issuecomment-1236392823
if (error) {
const prefix = (
process.env.NEXT_PUBLIC_SUPABASE_URL.split('//').pop() as string
)
.split('.')
.shift();
deleteCookie(req, res, `sb-${prefix}-auth-token`);
}
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment