Updated auth
This commit is contained in:
@@ -3,12 +3,14 @@ import { drizzleAdapter } from "better-auth/adapters/drizzle"
|
|||||||
import { genericOAuth } from "better-auth/plugins"
|
import { genericOAuth } from "better-auth/plugins"
|
||||||
import { db } from "../drizzle/db"
|
import { db } from "../drizzle/db"
|
||||||
import { env } from "../env/server"
|
import { env } from "../env/server"
|
||||||
|
import { nextCookies } from "better-auth/next-js"
|
||||||
|
|
||||||
export const auth = betterAuth({
|
export const auth = betterAuth({
|
||||||
database: drizzleAdapter(db, {
|
database: drizzleAdapter(db, {
|
||||||
provider: "pg"
|
provider: "pg"
|
||||||
}),
|
}),
|
||||||
plugins: [
|
plugins: [
|
||||||
|
nextCookies(),
|
||||||
genericOAuth({
|
genericOAuth({
|
||||||
config: [{
|
config: [{
|
||||||
providerId: "authentik",
|
providerId: "authentik",
|
||||||
@@ -16,6 +18,6 @@ export const auth = betterAuth({
|
|||||||
clientSecret: env.AUTHENTIK_CLIENT_SECRET,
|
clientSecret: env.AUTHENTIK_CLIENT_SECRET,
|
||||||
discoveryUrl: env.AUTHENTIK_DISCOVERY_URL
|
discoveryUrl: env.AUTHENTIK_DISCOVERY_URL
|
||||||
}]
|
}]
|
||||||
})
|
}),
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,20 +1,13 @@
|
|||||||
import { NextRequest, NextResponse } from "next/server"
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
import { auth } from "./lib/auth/auth"
|
import { getSessionCookie } from "better-auth/cookies";
|
||||||
|
|
||||||
export async function middleware(request: NextRequest) {
|
export async function middleware(request: NextRequest) {
|
||||||
const { nextUrl, headers, url } = request
|
const sessionCookie = getSessionCookie(request);
|
||||||
|
if (!sessionCookie) {
|
||||||
if (nextUrl.pathname.startsWith('/dashboard')) {
|
return NextResponse.redirect(new URL("/", request.url));
|
||||||
const session = await auth.api.getSession({
|
|
||||||
headers: headers
|
|
||||||
})
|
|
||||||
|
|
||||||
if (!session) {
|
|
||||||
return NextResponse.redirect(new URL('/sign-in', url))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return NextResponse.next()
|
return NextResponse.next();
|
||||||
}
|
}
|
||||||
|
|
||||||
export const config = { matcher: ['/dashboard/:path*'] }
|
export const config = { matcher: ["/dashboard"] };
|
||||||
Reference in New Issue
Block a user