Skip to content

Instantly share code, notes, and snippets.

@jerrywonderr
Created February 15, 2024 16:11
Show Gist options
  • Save jerrywonderr/9ae371dfb7bc2b754f1098e1cd101be0 to your computer and use it in GitHub Desktop.
Save jerrywonderr/9ae371dfb7bc2b754f1098e1cd101be0 to your computer and use it in GitHub Desktop.
Next Auth Implementation using Middlewares
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