Skip to content
This repository was archived by the owner on Apr 26, 2025. It is now read-only.

Commit 24330d3

Browse files
committed
Add parameter to Next.js current user to force a refresh
1 parent f80b020 commit 24330d3

File tree

2 files changed

+43
-17
lines changed

2 files changed

+43
-17
lines changed

src/nextjs/index.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -449,17 +449,20 @@ class FiefAuth {
449449
userinfo: FiefUserInfo | null,
450450
access_token_info: FiefSafeAccessTokenInfo | null,
451451
}> {
452-
return this.authenticated(
453-
async (req, res) => {
454-
let safeAccessTokenInfo: FiefSafeAccessTokenInfo | null = null;
455-
if (req.accessTokenInfo) {
456-
const { access_token: _, ...rest } = req.accessTokenInfo;
457-
safeAccessTokenInfo = rest;
458-
}
459-
res.status(200).json({ userinfo: req.user, access_token_info: safeAccessTokenInfo });
460-
},
461-
{ optional: true },
462-
);
452+
return async (req, res) => {
453+
const refresh = req.query.refresh === 'true';
454+
return this.authenticated(
455+
async (_req, _res) => {
456+
let safeAccessTokenInfo: FiefSafeAccessTokenInfo | null = null;
457+
if (_req.accessTokenInfo) {
458+
const { access_token: _, ...rest } = _req.accessTokenInfo;
459+
safeAccessTokenInfo = rest;
460+
}
461+
_res.status(200).json({ userinfo: _req.user, access_token_info: safeAccessTokenInfo });
462+
},
463+
{ optional: true, refresh },
464+
)(req, res as NextApiResponse);
465+
};
463466
}
464467
}
465468

src/nextjs/react.tsx

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,18 @@ const stub = (): never => {
6666
throw new Error('You forgot to wrap your component in <FiefAuthProvider>.');
6767
};
6868

69+
/**
70+
* Function to refresh the user information from the API.
71+
*
72+
* @param useCache - If `true`, the data will be read from your server cache (much faster).
73+
* If `false`, the data will be retrieved from the Fief API (fresher data).
74+
* Defaults to `true`.
75+
*/
76+
export type RefreshFunction = (useCache?: boolean) => Promise<void>;
77+
6978
interface FiefAuthContextType {
7079
state: FiefAuthState;
71-
refresh: () => void;
80+
refresh: RefreshFunction;
7281
}
7382

7483
// @ts-ignore
@@ -114,8 +123,9 @@ export interface FiefAuthProviderProps {
114123
*/
115124
export const FiefAuthProvider: React.FunctionComponent<FiefAuthProviderProps> = (props) => {
116125
const [state, dispatch] = useAuthStorageReducer();
117-
const refresh = useCallback(async () => {
118-
const response = await window.fetch(props.currentUserPath);
126+
const refresh = useCallback(async (useCache?: boolean) => {
127+
const refreshParam = useCache === undefined ? false : !useCache;
128+
const response = await window.fetch(`${props.currentUserPath}?refresh=${refreshParam}`);
119129
if (response.status === 200) {
120130
const data = await response.json();
121131
dispatch({ type: 'setAccessTokenInfo', value: data.access_token_info });
@@ -182,9 +192,9 @@ export const useFiefIsAuthenticated = (): boolean => {
182192
/**
183193
* Return a function to refresh the user information from the API.
184194
*
185-
* @returns A refresh function.
195+
* @returns A {@link RefreshFunction}.
186196
*
187-
* @example
197+
* @example Basic
188198
* ```tsx
189199
* const userinfo = useFiefUserinfo();
190200
* const refresh = useFiefRefresh();
@@ -196,8 +206,21 @@ export const useFiefIsAuthenticated = (): boolean => {
196206
* </>
197207
* );
198208
* ```
209+
*
210+
* @example Refresh from Fief server
211+
* ```tsx
212+
* const userinfo = useFiefUserinfo();
213+
* const refresh = useFiefRefresh();
214+
*
215+
* return (
216+
* <>
217+
* <p>User: {userinfo.email}</p>
218+
* <button type="button" onClick={() => refresh(false)}>Refresh user</button>
219+
* </>
220+
* );
221+
* ```
199222
*/
200-
export const useFiefRefresh = (): () => void => {
223+
export const useFiefRefresh = (): RefreshFunction => {
201224
const { refresh } = useContext(FiefAuthContext);
202225
return refresh;
203226
};

0 commit comments

Comments
 (0)