Skip to content

Commit 57e45a5

Browse files
committed
Simplify useCustomClaims to useTokenClaims
match the APIs proposed for authkit-react
1 parent 4daa96f commit 57e45a5

File tree

4 files changed

+38
-33
lines changed

4 files changed

+38
-33
lines changed

src/components/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Impersonation } from './impersonation.js';
22
import { AuthKitProvider, useAuth } from './authkit-provider.js';
33
import { useAccessToken } from './useAccessToken.js';
4-
import { useCustomClaims } from './useCustomClaims.js';
4+
import { useTokenClaims } from './useTokenClaims.js';
55

6-
export { Impersonation, AuthKitProvider, useAuth, useAccessToken, useCustomClaims };
6+
export { Impersonation, AuthKitProvider, useAuth, useAccessToken, useTokenClaims };

src/components/useCustomClaims.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/components/useTokenClaims.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { useMemo } from 'react';
2+
import { useAccessToken } from './useAccessToken.js';
3+
import { decodeJwt, type JWTPayload } from 'jose';
4+
5+
type TokenClaims<T> = Partial<JWTPayload & T>;
6+
7+
/**
8+
* A hook that retrieves the claims from the access token.
9+
*
10+
* @example
11+
* ```ts
12+
* const {customClaim, iat } = useTokenClaims<{ customClaim: string }>();
13+
* ```
14+
* @returns The claims from the access token, or an empty object if the token is not available or cannot be parsed.
15+
*/
16+
export function useTokenClaims<T = Record<string, unknown>>(): TokenClaims<T> {
17+
const { accessToken } = useAccessToken();
18+
19+
return useMemo(() => {
20+
if (!accessToken) {
21+
return {};
22+
}
23+
24+
try {
25+
return decodeJwt<T>(accessToken);
26+
} catch {
27+
return {};
28+
}
29+
}, [accessToken]);
30+
}

src/session.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use server';
22

33
import { sealData, unsealData } from 'iron-session';
4-
import { createRemoteJWKSet, decodeJwt, jwtVerify } from 'jose';
4+
import { JWTPayload, createRemoteJWKSet, decodeJwt, jwtVerify } from 'jose';
55
import { cookies, headers } from 'next/headers';
66
import { redirect } from 'next/navigation';
77
import { NextRequest, NextResponse } from 'next/server';
@@ -355,16 +355,15 @@ async function redirectToSignIn() {
355355
redirect(await getAuthorizationUrl({ returnPathname, screenHint }));
356356
}
357357

358-
export async function getCustomClaims<T = Record<string, unknown>>(accessToken?: string) {
358+
export async function getTokenClaims<T = Record<string, unknown>>(
359+
accessToken?: string,
360+
): Promise<Partial<JWTPayload & T>> {
359361
const token = accessToken ?? (await withAuth()).accessToken;
360362
if (!token) {
361-
return null;
363+
return {};
362364
}
363365

364-
const decoded = decodeJwt(token);
365-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
366-
const { aud, exp, iat, iss, sub, sid, org_id, role, permissions, entitlements, jti, nbf, ...custom } = decoded;
367-
return custom as T;
366+
return decodeJwt<T>(token);
368367
}
369368

370369
async function withAuth(options: { ensureSignedIn: true }): Promise<UserInfo>;

0 commit comments

Comments
 (0)