This repository has been archived by the owner on Dec 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b3c3eaa
commit 1f42d36
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import NextAuth from "next-auth"; | ||
import { JWT } from "next-auth/jwt"; | ||
import ZitadelProvider from "next-auth/providers/zitadel"; | ||
import { custom } from 'openid-client'; | ||
|
||
var client_id = process.env.ZITADEL_CLIENT_ID; | ||
var client_secret = process.env.ZITADEL_CLIENT_SECRET; | ||
var zitadel_url = process.env.ZITADEL_URL; | ||
|
||
if (!client_id || !client_secret) { | ||
throw new Error("ZITADEL_CLIENT_ID and ZITADEL_CLIENT_SECRET must be set"); | ||
} | ||
|
||
custom.setHttpOptionsDefaults({ | ||
timeout: 10000, | ||
}); | ||
|
||
const authOptions = { | ||
providers: [ | ||
ZitadelProvider({ | ||
clientId: client_id, | ||
clientSecret: client_secret, | ||
issuer: zitadel_url, | ||
}), | ||
], | ||
callbacks: { | ||
jwt: async ({ token, user, account, profile, isNewUser }: { token: JWT, user?: any, account?: any, profile?: any, isNewUser?: boolean }) => { | ||
if (user) { | ||
token.user = user; | ||
const u = user as any | ||
token.role = u.role; | ||
} | ||
if (account) { | ||
token.accessToken = account.access_token | ||
} | ||
return token; | ||
}, | ||
session: ({ session, token }: { token: JWT, session?: any }) => { | ||
token.accessToken | ||
return { | ||
...session, | ||
user: { | ||
...session.user, | ||
role: token.role, | ||
}, | ||
}; | ||
}, | ||
}, | ||
}; | ||
|
||
const handler = NextAuth(authOptions); | ||
|
||
export { handler as GET, handler as POST }; |