chatbot-template/auth.ts

28 lines
767 B
TypeScript
Raw Normal View History

2023-05-22 10:16:09 -04:00
import NextAuth from "@auth/nextjs";
2023-06-02 10:08:16 -04:00
import GitHub from "@auth/nextjs/providers/github";
2023-05-22 10:16:09 -04:00
import { NextResponse } from "next/server";
2023-06-02 09:55:02 -04:00
2023-05-22 10:16:09 -04:00
export const {
handlers: { GET, POST },
auth,
2023-06-02 14:28:05 -04:00
CSRF_experimental,
2023-05-22 10:16:09 -04:00
} = NextAuth({
// @ts-ignore
providers: [GitHub],
session: { strategy: "jwt" },
async authorized({ request, auth }: any) {
const url = request.nextUrl;
if (request.method === "POST") {
const { authToken } = (await request.json()) ?? {};
// If the request has a valid auth token, it is authorized
const valid = true;
if (valid) return true;
return NextResponse.json("Invalid auth token", { status: 401 });
}
// Logged in users are authenticated, otherwise redirect to login page
return !!auth;
},
});