2023-05-22 10:16:09 -04:00
|
|
|
import NextAuth from "@auth/nextjs";
|
|
|
|
|
import GitHub from "@auth/nextjs/providers/github";
|
|
|
|
|
import { NextResponse } from "next/server";
|
2023-05-22 12:14:37 -04:00
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
db,
|
|
|
|
|
users,
|
|
|
|
|
accounts,
|
|
|
|
|
sessions,
|
|
|
|
|
verificationTokens,
|
|
|
|
|
} from "./lib/db/schema";
|
|
|
|
|
import { DrizzleAdapterPg } from "./lib/db";
|
|
|
|
|
|
2023-05-22 10:16:09 -04:00
|
|
|
export const {
|
|
|
|
|
handlers: { GET, POST },
|
|
|
|
|
auth,
|
|
|
|
|
CSRF_experimental,
|
|
|
|
|
} = NextAuth({
|
2023-05-22 12:14:37 -04:00
|
|
|
adapter: DrizzleAdapterPg(db, {
|
|
|
|
|
accounts,
|
|
|
|
|
users,
|
|
|
|
|
sessions,
|
|
|
|
|
verificationTokens,
|
|
|
|
|
}),
|
2023-05-22 10:16:09 -04:00
|
|
|
// @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;
|
|
|
|
|
},
|
|
|
|
|
});
|