Middleware redirect

I am currently having issues redirecting users to the login screen. User story: a user is not logged in and they click the 'dashboard' button, the button should redirect them to the nextauth login screen.
5 Replies
mac
macOP3y ago
// middleware.ts
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import { signIn } from "next-auth/react";

// This function can be marked `async` if using `await` inside
export async function middleware(request: NextRequest) {
return NextResponse.redirect(new URL(await signIn("discord") as unknown as string, request.url))
}

// See "Matching Paths" below to learn more
export const config = {
matcher: '/dashboard',
}
// middleware.ts
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import { signIn } from "next-auth/react";

// This function can be marked `async` if using `await` inside
export async function middleware(request: NextRequest) {
return NextResponse.redirect(new URL(await signIn("discord") as unknown as string, request.url))
}

// See "Matching Paths" below to learn more
export const config = {
matcher: '/dashboard',
}
error - (middleware)/node_modules/next-auth/react/index.js (249:73) @ _callee5$
error - window is not defined
null
error - (middleware)/node_modules/next-auth/react/index.js (249:73) @ _callee5$
error - window is not defined
null
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
mac
macOP3y ago
appreciate it wait idk if im dumb but if its clientside how do I (!session) if we cant get session
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
esponges
esponges3y ago
there's a withAuth wrapper to redirect out users to the login page https://next-auth.js.org/configuration/nextjs#advanced-usage
Next.js | NextAuth.js
unstable_getServerSession
Want results from more Discord servers?
Add your server