Skip to content
This repository has been archived by the owner on Jul 11, 2024. It is now read-only.

Commit

Permalink
Allow to keep last auth login ID after logout
Browse files Browse the repository at this point in the history
  • Loading branch information
orius123 committed Jul 3, 2024
1 parent a2defdf commit 73298ed
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ Notes:

### Last User Persistence

Descope stores the last user information in local storage. If you wish to disable this feature, you can pass `storeLastAuthenticatedUser={false}` to the `AuthProvider` component. Please note that some features related to the last authenticated user may not function as expected if this behavior is disabled.
Descope stores the last user information in local storage. If you wish to disable this feature, you can pass `storeLastAuthenticatedUser={false}` to the `AuthProvider` component. Please note that some features related to the last authenticated user may not function as expected if this behavior is disabled. Local storage is being cleared when the user logs out, if you want the avoid clearing the local storage, you can pass `keepLastAuthenticatedUserAfterLogout={true}` to the `AuthProvider` component.

### Widgets

Expand Down
84 changes: 55 additions & 29 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"@descope/role-management-widget": "0.1.85",
"@descope/user-management-widget": "0.4.88",
"@descope/user-profile-widget": "0.0.59",
"@descope/web-component": "3.16.2"
"@descope/web-component": "3.18.0"
},
"devDependencies": {
"@babel/core": "7.23.0",
Expand Down
7 changes: 6 additions & 1 deletion src/components/AuthProvider/AuthProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ interface IAuthProviderProps {
sessionTokenViaCookie?: boolean;
// If true, last authenticated user will be stored on local storage and can accessed with getUser function
storeLastAuthenticatedUser?: boolean;
// If true, last authenticated user will not be removed after logout
keepLastAuthenticatedUserAfterLogout?: boolean;
children?: JSX.Element;
}

Expand All @@ -35,6 +37,7 @@ const AuthProvider: FC<IAuthProviderProps> = ({
sessionTokenViaCookie = false,
persistTokens = true,
storeLastAuthenticatedUser = true,
keepLastAuthenticatedUserAfterLogout = false,
children = undefined
}) => {
const [user, setUser] = useState<User>();
Expand All @@ -48,7 +51,8 @@ const AuthProvider: FC<IAuthProviderProps> = ({
baseUrl,
persistTokens,
sessionTokenViaCookie,
storeLastAuthenticatedUser
storeLastAuthenticatedUser,
keepLastAuthenticatedUserAfterLogout
});

useEffect(() => {
Expand Down Expand Up @@ -97,6 +101,7 @@ const AuthProvider: FC<IAuthProviderProps> = ({
baseUrl,
baseStaticUrl,
storeLastAuthenticatedUser,
keepLastAuthenticatedUserAfterLogout,
setUser,
setSession,
sdk
Expand Down
5 changes: 4 additions & 1 deletion src/components/AuthProvider/useSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ type Config = Pick<
| 'persistTokens'
| 'sessionTokenViaCookie'
| 'storeLastAuthenticatedUser'
| 'keepLastAuthenticatedUserAfterLogout'
>;

export default ({
projectId,
baseUrl,
persistTokens,
sessionTokenViaCookie,
storeLastAuthenticatedUser
storeLastAuthenticatedUser,
keepLastAuthenticatedUserAfterLogout
}: Config): ReturnType<typeof createSdk> =>
useMemo(() => {
if (!projectId) {
Expand All @@ -29,6 +31,7 @@ export default ({
baseHeaders,
persistTokens,
storeLastAuthenticatedUser,
keepLastAuthenticatedUserAfterLogout,
autoRefresh: true
});
}, [projectId, baseUrl, sessionTokenViaCookie]);
4 changes: 4 additions & 0 deletions src/components/Descope.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ const Descope = React.forwardRef<HTMLElement, DescopeProps>(
baseUrl,
baseStaticUrl,
storeLastAuthenticatedUser,
keepLastAuthenticatedUserAfterLogout,
sdk
} = React.useContext(Context);

Expand Down Expand Up @@ -211,6 +212,9 @@ const Descope = React.forwardRef<HTMLElement, DescopeProps>(
autoFocus={autoFocus}
validateOnBlur={validateOnBlur}
storeLastAuthenticatedUser={storeLastAuthenticatedUser}
keepLastAuthenticatedUserAfterLogout={
keepLastAuthenticatedUserAfterLogout
}
/>
</Suspense>
</form>
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export interface IContext {
baseUrl?: string;
baseStaticUrl?: string;
storeLastAuthenticatedUser?: boolean;
keepLastAuthenticatedUserAfterLogout?: boolean;
sdk?: Sdk;
setUser: React.Dispatch<React.SetStateAction<User>>;
setSession: React.Dispatch<React.SetStateAction<string>>;
Expand Down

0 comments on commit 73298ed

Please sign in to comment.