@@ -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+
6978interface FiefAuthContextType {
7079 state : FiefAuthState ;
71- refresh : ( ) => void ;
80+ refresh : RefreshFunction ;
7281}
7382
7483// @ts -ignore
@@ -114,8 +123,9 @@ export interface FiefAuthProviderProps {
114123 */
115124export 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