2023-06-28 09:09:45 +00:00
|
|
|
import type { NextAuthOptions } from "next-auth";
|
|
|
|
|
import Google from "next-auth/providers/google";
|
|
|
|
|
import Apple from "next-auth/providers/apple";
|
2023-08-25 07:11:33 +00:00
|
|
|
import Credentials from "next-auth/providers/credentials";
|
|
|
|
|
import { checkAuth } from "./opensearch";
|
2025-08-20 11:37:39 +02:00
|
|
|
import { createLogger } from "@link-stack/logger";
|
|
|
|
|
|
|
|
|
|
const logger = createLogger('leafcutter-auth');
|
2023-06-28 09:09:45 +00:00
|
|
|
|
|
|
|
|
export const authOptions: NextAuthOptions = {
|
2023-08-25 07:11:33 +00:00
|
|
|
pages: {
|
|
|
|
|
signIn: "/login",
|
|
|
|
|
error: "/login",
|
|
|
|
|
signOut: "/logout",
|
|
|
|
|
},
|
2023-06-28 09:09:45 +00:00
|
|
|
providers: [
|
|
|
|
|
Google({
|
|
|
|
|
clientId: process.env.GOOGLE_CLIENT_ID ?? "",
|
|
|
|
|
clientSecret: process.env.GOOGLE_CLIENT_SECRET ?? "",
|
|
|
|
|
}),
|
|
|
|
|
Apple({
|
|
|
|
|
clientId: process.env.APPLE_CLIENT_ID ?? "",
|
|
|
|
|
clientSecret: process.env.APPLE_CLIENT_SECRET ?? "",
|
|
|
|
|
}),
|
2023-08-25 07:11:33 +00:00
|
|
|
Credentials({
|
|
|
|
|
name: "Link",
|
|
|
|
|
credentials: {
|
2025-01-22 17:50:38 +01:00
|
|
|
authToken: { label: "AuthToken", type: "text" },
|
2023-08-25 07:11:33 +00:00
|
|
|
},
|
|
|
|
|
async authorize(credentials, req) {
|
|
|
|
|
const { headers } = req;
|
|
|
|
|
const leafcutterUser = headers?.["x-leafcutter-user"];
|
|
|
|
|
const authToken = credentials?.authToken;
|
|
|
|
|
|
|
|
|
|
if (!leafcutterUser || leafcutterUser.trim() === "") {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
/*
|
|
|
|
|
try {
|
|
|
|
|
// add role check
|
|
|
|
|
await checkAuth(username, password);
|
|
|
|
|
const user = {
|
|
|
|
|
id: leafcutterUser,
|
|
|
|
|
email: leafcutterUser
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return user;
|
|
|
|
|
} catch (e) {
|
2025-08-20 11:37:39 +02:00
|
|
|
logger.error({ e });
|
2023-08-25 07:11:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
*/
|
2025-01-22 17:50:38 +01:00
|
|
|
},
|
|
|
|
|
}),
|
2023-06-28 09:09:45 +00:00
|
|
|
],
|
|
|
|
|
secret: process.env.NEXTAUTH_SECRET,
|
2023-08-25 07:11:33 +00:00
|
|
|
/*
|
|
|
|
|
callbacks: {
|
|
|
|
|
signIn: async ({ user, account, profile }) => {
|
|
|
|
|
const roles: any = [];
|
|
|
|
|
return roles.includes("admin") || roles.includes("agent");
|
|
|
|
|
},
|
|
|
|
|
session: async ({ session, user, token }) => {
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
session.user.roles = token.roles;
|
|
|
|
|
return session;
|
|
|
|
|
},
|
|
|
|
|
jwt: async ({ token, user, account, profile, trigger }) => {
|
|
|
|
|
if (user) {
|
|
|
|
|
token.roles = [];
|
|
|
|
|
}
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
},*/
|
2023-06-28 09:09:45 +00:00
|
|
|
};
|