2023-12-10 17:12:20 +01:00
|
|
|
import { cookies } from "next/headers";
|
|
|
|
|
import { DrizzleAdapter } from "@auth/drizzle-adapter";
|
|
|
|
|
import NextAuth from "next-auth";
|
|
|
|
|
import Credentials from "next-auth/providers/credentials";
|
|
|
|
|
|
2024-01-02 14:18:37 +01:00
|
|
|
import { db } from "@homarr/db";
|
2023-12-10 17:12:20 +01:00
|
|
|
|
2024-02-10 19:00:08 +01:00
|
|
|
import { createSignInCallback, sessionCallback } from "./callbacks";
|
|
|
|
|
import { createCredentialsConfiguration } from "./providers/credentials";
|
2023-12-10 17:12:20 +01:00
|
|
|
import { EmptyNextAuthProvider } from "./providers/empty";
|
2024-02-10 19:00:08 +01:00
|
|
|
import { sessionMaxAgeInSeconds, sessionTokenCookieName } from "./session";
|
2023-12-10 17:12:20 +01:00
|
|
|
|
|
|
|
|
const adapter = DrizzleAdapter(db);
|
|
|
|
|
|
|
|
|
|
export const createConfiguration = (isCredentialsRequest: boolean) =>
|
|
|
|
|
NextAuth({
|
2024-02-23 17:15:24 +01:00
|
|
|
logger: {
|
|
|
|
|
error: (code, ...message) => {
|
|
|
|
|
// Remove the big error message for failed login attempts
|
|
|
|
|
// as it is not useful for the user.
|
|
|
|
|
if (code.name === "CredentialsSignin") {
|
|
|
|
|
console.warn("The login attempt of a user was not successful.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.error(code, ...message);
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
trustHost: true,
|
2023-12-10 17:12:20 +01:00
|
|
|
adapter,
|
2024-02-10 19:00:08 +01:00
|
|
|
providers: [
|
|
|
|
|
Credentials(createCredentialsConfiguration(db)),
|
|
|
|
|
EmptyNextAuthProvider(),
|
|
|
|
|
],
|
2023-12-10 17:12:20 +01:00
|
|
|
callbacks: {
|
2024-02-10 19:00:08 +01:00
|
|
|
session: sessionCallback,
|
|
|
|
|
signIn: createSignInCallback(adapter, isCredentialsRequest),
|
2023-12-10 17:12:20 +01:00
|
|
|
},
|
2024-04-07 11:32:29 +02:00
|
|
|
secret: "secret-is-not-defined-yet", // TODO: This should be added later
|
2023-12-10 17:12:20 +01:00
|
|
|
session: {
|
|
|
|
|
strategy: "database",
|
|
|
|
|
maxAge: sessionMaxAgeInSeconds,
|
|
|
|
|
},
|
|
|
|
|
pages: {
|
|
|
|
|
signIn: "/auth/login",
|
|
|
|
|
error: "/auth/login",
|
|
|
|
|
},
|
|
|
|
|
jwt: {
|
|
|
|
|
encode() {
|
2024-02-10 19:00:08 +01:00
|
|
|
const cookie = cookies().get(sessionTokenCookieName)?.value;
|
2023-12-10 17:12:20 +01:00
|
|
|
return cookie ?? "";
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
decode() {
|
|
|
|
|
return null;
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|