1
0
Fork 0

fix(nextauth): check if originLinkAccount is not undefined

This commit is contained in:
Vojtěch Mareš 2023-09-07 11:43:05 +02:00
parent 94cd9e37d1
commit 57dc162ba1
Signed by: vojtech.mares
GPG key ID: C6827B976F17240D

View file

@ -37,7 +37,7 @@ declare module "next-auth" {
* *
* @see https://stackoverflow.com/a/75526977 * @see https://stackoverflow.com/a/75526977
*/ */
declare module 'next-auth/jwt' { declare module "next-auth/jwt" {
interface JWT { interface JWT {
id_token?: string; id_token?: string;
provider?: string; provider?: string;
@ -56,10 +56,15 @@ const originLinkAccount = adapter.linkAccount;
*/ */
adapter.linkAccount = (account: AdapterAccount) => { adapter.linkAccount = (account: AdapterAccount) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
const { 'not-before-policy': _, refresh_expires_in, ...data } = account; const { "not-before-policy": _, refresh_expires_in, ...data } = account;
return originLinkAccount(data);
if (!originLinkAccount) {
return;
} }
return originLinkAccount(data);
};
/** /**
* Options for NextAuth.js used to configure adapters, providers, callbacks, etc. * Options for NextAuth.js used to configure adapters, providers, callbacks, etc.
* *
@ -83,10 +88,10 @@ export const authOptions: NextAuthOptions = {
*/ */
jwt({ token, account }) { jwt({ token, account }) {
if (account) { if (account) {
token.id_token = account?.id_token token.id_token = account?.id_token;
token.provider = account?.provider token.provider = account?.provider;
} }
return token return token;
}, },
}, },
adapter: adapter, adapter: adapter,
@ -119,12 +124,14 @@ export const authOptions: NextAuthOptions = {
*/ */
async signOut({ token }: { token: JWT }) { async signOut({ token }: { token: JWT }) {
if (token.provider === "keycloak") { if (token.provider === "keycloak") {
const logOutURL = new URL(`${env.KEYCLOAK_ISSUER}/protocol/openid-connect/logout`) const logOutURL = new URL(
logOutURL.searchParams.set("id_token_hint", token.id_token ?? '') `${env.KEYCLOAK_ISSUER}/protocol/openid-connect/logout`
);
logOutURL.searchParams.set("id_token_hint", token.id_token ?? "");
await fetch(logOutURL); await fetch(logOutURL);
} }
}, },
} },
}; };
/** /**