Skip to content

Commit 0cdd038

Browse files
authored
Merge pull request #81 from dopry/fix/auth0-logout
fix: auth0 logout
2 parents 8ae9b1a + 61938fb commit 0cdd038

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

src/lib/OidcContext.svelte

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<script context="module">
1+
<script context="module" lang="ts">
22
import { UserManager } from 'oidc-client-ts';
33
import { onDestroy, onMount, setContext } from 'svelte';
44
import { writable } from 'svelte/store';
@@ -31,7 +31,7 @@
3131
* @return bool indicated whether the token was refreshed, if false error will be set
3232
* in the authError store.
3333
*/
34-
export async function refreshToken(oidcPromise) {
34+
export async function refreshToken(oidcPromise: Promise<UserManager>): Promise<boolean> {
3535
try {
3636
const oidc = await oidcPromise
3737
await oidc.signinSilent();
@@ -47,64 +47,64 @@
4747
/**
4848
* Initiate Register/Login flow.
4949
*
50-
* @param {Promise<UserManager>} oidcPromise
51-
* @param {boolean} preserveRoute - store current location so callback handler will navigate back to it.
52-
* @param {string} callback_url - explicit path to use for the callback.
50+
* @param oidcPromise
51+
* @param preserveRoute - store current location so callback handler will navigate back to it.
52+
* @param callback_url - explicit path to use for the callback.
5353
*/
54-
export async function login(oidcPromise, preserveRoute = true, callback_url = null) {
54+
export async function login(oidcPromise: Promise<UserManager>, preserveRoute = true, callback_url?: string): Promise<void> {
5555
const oidc = await oidcPromise;
5656
const redirect_uri = callback_url || window.location.href;
5757
5858
// try to keep the user on the same page from which they triggered login. If set to false should typically
5959
// cause redirect to /.
60-
const appState = preserveRoute
60+
const state = preserveRoute
6161
? {
6262
pathname: window.location.pathname,
6363
search: window.location.search,
6464
}
6565
: {};
66-
await oidc.signinRedirect({ redirect_uri, appState });
66+
await oidc.signinRedirect({ redirect_uri, state });
6767
}
6868
6969
/**
7070
* Log out the current user.
7171
*
72-
* @param {Promise<UserManager>} oidcPromise
73-
* @param {string} logout_url - specify the url to return to after login.
72+
* @param oidcPromise
73+
* @param logout_url - specify the url to return to after login.
7474
*/
75-
export async function logout(oidcPromise, logout_url = null) {
75+
export async function logout(oidcPromise: Promise<UserManager>, logout_url?: string): Promise<void> {
7676
const oidc = await oidcPromise;
77-
const returnTo = logout_url || window.location.href;
77+
const post_logout_redirect_uri = logout_url || window.location.href;
7878
try {
79-
await oidc.signoutRedirect({ returnTo });
79+
await oidc.signoutRedirect({ post_logout_redirect_uri });
8080
} catch (err) {
81-
if (err.message !== 'no end session endpoint') throw err;
81+
if (!err.message?.toLowerCase().includes('no end session endpoint')) throw err;
8282
// this is most likely auth0, so let's try their logout endpoint.
8383
// @see: https://auth0.com/docs/api/authentication#logout
8484
// this is dirty and hack and reaches into guts of the oidc client
8585
// in ways I'd prefer not to.. but auth0 has this annoying non-conforming
8686
// session termination.
87-
const authority = oidc._settings._authority;
87+
const authority = oidc.settings.authority;
8888
if (authority.endsWith('auth0.com')) {
89-
const clientId = oidc._settings._client_id;
89+
const clientId = oidc.settings.client_id;
9090
const url = `${authority}/v2/logout?client_id=${clientId}&returnTo=${encodeURIComponent(
91-
returnTo
91+
post_logout_redirect_uri
9292
)}`;
93-
window.location = url;
94-
} else throw err
93+
window.location.assign(url);
94+
} else throw err;
9595
}
9696
}
9797
</script>
9898

99-
<script>
99+
<script lang="ts">
100100
// props.
101-
export let issuer;
102-
export let client_id;
103-
export let redirect_uri;
104-
export let post_logout_redirect_uri;
105-
export let extraOptions = {};
101+
export let issuer: string;
102+
export let client_id: string;
103+
export let redirect_uri: string;
104+
export let post_logout_redirect_uri: string;
105+
export let extraOptions: Record<string, unknown> = {};
106106
107-
export let scope = 'openid profile email';
107+
export let scope: string = 'openid profile email';
108108
109109
setContext(OIDC_CONTEXT_REDIRECT_URI, redirect_uri);
110110
setContext(OIDC_CONTEXT_POST_LOGOUT_REDIRECT_URI, post_logout_redirect_uri);
@@ -168,7 +168,7 @@
168168
if (params.has('code')) {
169169
// handle the callback
170170
const response = await oidc.signinCallback();
171-
let state = (response && response.state) || {};
171+
let state: { targetUrl?: string; isRedirectCallback?: boolean } = (response && response.state) || {};
172172
// Can be smart here and redirect to original path instead of root
173173
const url = state && state.targetUrl ? state.targetUrl : window.location.pathname;
174174
state = { ...state, isRedirectCallback: true };

0 commit comments

Comments
 (0)