Skip to content

Commit 6b92df5

Browse files
authored
Merge pull request #123 from kinde-oss/feat/logout-options
2 parents 8c352a5 + d367a1f commit 6b92df5

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

src/components/LogoutLink.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ export function LogoutLink({ children, ...props }: LogoutLinkProps) {
66
const auth = useKindeAuth();
77

88
const logout = useCallback(async () => {
9-
auth.logout(props.redirectUrl || window.location.origin);
10-
}, [auth, props.redirectUrl]);
9+
auth.logout({
10+
redirectUrl: props.redirectUrl || window.location.origin,
11+
allSessions: props.allSessions,
12+
});
13+
}, [auth, props.redirectUrl, props.allSessions]);
1114

1215
return (
1316
<button

src/state/KindeContext.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createContext } from "react";
22
import { initialState } from "./initialState";
3-
import { State } from "./types";
3+
import { LogoutOptions, State } from "./types";
44
import type {
55
getClaim,
66
getClaims,
@@ -22,7 +22,7 @@ export interface KindeContextProps extends State {
2222
register: (
2323
options?: LoginMethodParams & { state?: Record<string, string> },
2424
) => Promise<void>;
25-
logout: (redirectUri?: string) => Promise<void>;
25+
logout: (options?: string | LogoutOptions) => Promise<void>;
2626
getClaims: typeof getClaims;
2727
getIdToken: () => Promise<string | undefined>;
2828
getToken: () => Promise<string | undefined>;

src/state/KindeProvider.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import React, {
3737
import { KindeContext, KindeContextProps } from "./KindeContext";
3838
import { getRedirectUrl } from "../utils/getRedirectUrl";
3939
import packageJson from "../../package.json";
40-
import { ErrorProps } from "./types";
40+
import { ErrorProps, LogoutOptions } from "./types";
4141
import { RefreshTokenResult } from "@kinde/js-utils/dist/utils/token/refreshToken";
4242
// TODO: need to look for old token store and convert.
4343
storageSettings.keyPrefix = "";
@@ -243,15 +243,27 @@ export const KindeProvider = ({
243243
[redirectUri],
244244
);
245245

246-
const logout = useCallback(async (redirectUrl?: string) => {
246+
const logout = useCallback(async (options?: string | LogoutOptions) => {
247247
try {
248248
const domain = (await storeState.memoryStorage.getSessionItem(
249249
storeState.LocalKeys.domain,
250250
)) as string;
251251

252252
const params = new URLSearchParams();
253-
if (redirectUrl) {
254-
params.append("redirect", redirectUrl);
253+
254+
if (options) {
255+
if (options && typeof options === "string") {
256+
params.append("redirect", options);
257+
} else if (typeof options === "object") {
258+
if (options.redirectUrl || logoutUri) {
259+
params.append("redirect", options.redirectUrl || logoutUri || "");
260+
}
261+
if (options.allSessions) {
262+
params.append("all_sessions", String(options.allSessions));
263+
}
264+
}
265+
} else {
266+
params.append("redirect", logoutUri || "");
255267
}
256268

257269
setState((val) => {

src/state/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,15 @@ export interface LogoutLinkProps
2323
extends React.ButtonHTMLAttributes<HTMLButtonElement> {
2424
children: React.ReactNode;
2525
redirectUrl?: string;
26+
allSessions?: boolean;
2627
}
2728

2829
export type ErrorProps = {
2930
error: string;
3031
errorDescription: string;
3132
};
33+
34+
export type LogoutOptions = {
35+
allSessions?: boolean;
36+
redirectUrl?: string;
37+
};

0 commit comments

Comments
 (0)