Created
February 15, 2024 16:11
-
-
Save jerrywonderr/9ae371dfb7bc2b754f1098e1cd101be0 to your computer and use it in GitHub Desktop.
Next Auth Implementation using Middlewares
This file contains 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
import { NextResponse } from 'next/server' | |
import type { NextRequest } from 'next/server' | |
import { supabaseClient } from '../supabase' | |
export const config = { | |
matcher: ['/auth/routes/:function*'], | |
} | |
const loginUrl = new URL('/login', request.url) | |
// Add ?from=/incoming-url to the /login URL | |
loginUrl.searchParams.set('from', request.nextUrl.pathname) | |
// And redirect to the new URL | |
return NextResponse.redirect(loginUrl) | |
export function middleware(request: NextRequest) { | |
const {data, error} = supabaseClient.auth.getSession() | |
if (error || !data) { | |
// Redirect user to login page | |
} | |
return NextResponse.next() // Allow the request to continue | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment