Files
web/auth.ts
2024-03-14 11:02:36 +01:00

117 lines
2.9 KiB
TypeScript

import NextAuth from "next-auth"
import { env } from "@/env/server"
import type { NextAuthConfig } from "next-auth"
export const config = {
providers: [
{
id: "curity",
type: "oidc",
name: "Curity",
// FIXME: This is incorrect. We should not hard code this.
// It should be ${env.CURITY_ISSUER_USER}.
// This change requires sync between Curity deploy and CurrentWeb and NewWeb.
issuer: "https://scandichotels.com",
token: {
url: `${env.CURITY_ISSUER_USER}/oauth/v2/token`,
},
userinfo: {
url: `${env.CURITY_ISSUER_USER}/oauth/v2/userinfo`,
},
authorization: {
url: `${env.CURITY_ISSUER_USER}/oauth/v2/authorize`,
params: {
scope: ["openid"],
},
},
clientId: env.CURITY_CLIENT_ID_USER,
clientSecret: env.CURITY_CLIENT_SECRET_USER,
profile(profile: { id: string; sub: string; given_name: string }) {
console.log({ profile })
return {
id: profile.id,
sub: profile.sub,
given_name: profile.given_name,
}
},
},
],
trustHost: true,
// pages: {
// signIn: "/auth/login",
// },
// basePath: "/api/auth",
session: {
strategy: "jwt",
},
callbacks: {
async signIn(...args) {
console.log("****** SIGN IN *******")
console.log(args)
return true
},
async session(...args) {
console.log(args)
return args[0].session
},
async redirect({ baseUrl, url }) {
console.log("****** REDIRECT *******")
console.log({ url })
console.log({ baseUrl })
// Allows relative callback URLs
if (url.startsWith("/")) {
return `${baseUrl}${url}`
} else if (new URL(url).origin === baseUrl) {
// Allows callback URLs on the same origin
return url
}
return baseUrl
},
authorized({ auth, request }) {
console.log("****** AUTHORIZED *******")
console.log({ request, auth })
// const { pathname } = request.nextUrl
// if (pathname === "/middleware-example") return !!auth
return true
},
jwt({ session, token, trigger }) {
console.log("****** JWT *******")
// if (trigger === "update") token.name = session.user.name
console.log({ token, trigger, session })
return token
},
},
events: {
async signIn(...args) {
console.log({ args })
},
async session(...args) {
console.log({ args })
},
},
logger: {
error(code, ...message) {
console.info("ERROR LOGGER")
console.error(code, message)
},
warn(code, ...message) {
console.info("WARN LOGGER")
console.warn(code, message)
},
debug(code, ...message) {
console.info("DEBUG LOGGER")
console.debug(code, message)
},
},
} satisfies NextAuthConfig
export const {
handlers: { GET, POST },
auth,
signIn,
signOut,
} = NextAuth(config)