|
1 | | -<script context="module"> |
| 1 | +<script context="module" lang="ts"> |
2 | 2 | import { UserManager } from 'oidc-client-ts'; |
3 | 3 | import { onDestroy, onMount, setContext } from 'svelte'; |
4 | 4 | import { writable } from 'svelte/store'; |
|
31 | 31 | * @return bool indicated whether the token was refreshed, if false error will be set |
32 | 32 | * in the authError store. |
33 | 33 | */ |
34 | | - export async function refreshToken(oidcPromise) { |
| 34 | + export async function refreshToken(oidcPromise: Promise<UserManager>): Promise<boolean> { |
35 | 35 | try { |
36 | 36 | const oidc = await oidcPromise |
37 | 37 | await oidc.signinSilent(); |
|
47 | 47 | /** |
48 | 48 | * Initiate Register/Login flow. |
49 | 49 | * |
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. |
53 | 53 | */ |
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> { |
55 | 55 | const oidc = await oidcPromise; |
56 | 56 | const redirect_uri = callback_url || window.location.href; |
57 | 57 |
|
58 | 58 | // try to keep the user on the same page from which they triggered login. If set to false should typically |
59 | 59 | // cause redirect to /. |
60 | | - const appState = preserveRoute |
| 60 | + const state = preserveRoute |
61 | 61 | ? { |
62 | 62 | pathname: window.location.pathname, |
63 | 63 | search: window.location.search, |
64 | 64 | } |
65 | 65 | : {}; |
66 | | - await oidc.signinRedirect({ redirect_uri, appState }); |
| 66 | + await oidc.signinRedirect({ redirect_uri, state }); |
67 | 67 | } |
68 | 68 |
|
69 | 69 | /** |
70 | 70 | * Log out the current user. |
71 | 71 | * |
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. |
74 | 74 | */ |
75 | | - export async function logout(oidcPromise, logout_url = null) { |
| 75 | + export async function logout(oidcPromise: Promise<UserManager>, logout_url?: string): Promise<void> { |
76 | 76 | const oidc = await oidcPromise; |
77 | | - const returnTo = logout_url || window.location.href; |
| 77 | + const post_logout_redirect_uri = logout_url || window.location.href; |
78 | 78 | try { |
79 | | - await oidc.signoutRedirect({ returnTo }); |
| 79 | + await oidc.signoutRedirect({ post_logout_redirect_uri }); |
80 | 80 | } catch (err) { |
81 | | - if (err.message !== 'no end session endpoint') throw err; |
| 81 | + if (!err.message?.toLowerCase().includes('no end session endpoint')) throw err; |
82 | 82 | // this is most likely auth0, so let's try their logout endpoint. |
83 | 83 | // @see: https://auth0.com/docs/api/authentication#logout |
84 | 84 | // this is dirty and hack and reaches into guts of the oidc client |
85 | 85 | // in ways I'd prefer not to.. but auth0 has this annoying non-conforming |
86 | 86 | // session termination. |
87 | | - const authority = oidc._settings._authority; |
| 87 | + const authority = oidc.settings.authority; |
88 | 88 | if (authority.endsWith('auth0.com')) { |
89 | | - const clientId = oidc._settings._client_id; |
| 89 | + const clientId = oidc.settings.client_id; |
90 | 90 | const url = `${authority}/v2/logout?client_id=${clientId}&returnTo=${encodeURIComponent( |
91 | | - returnTo |
| 91 | + post_logout_redirect_uri |
92 | 92 | )}`; |
93 | | - window.location = url; |
94 | | - } else throw err |
| 93 | + window.location.assign(url); |
| 94 | + } else throw err; |
95 | 95 | } |
96 | 96 | } |
97 | 97 | </script> |
98 | 98 |
|
99 | | -<script> |
| 99 | +<script lang="ts"> |
100 | 100 | // 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> = {}; |
106 | 106 |
|
107 | | - export let scope = 'openid profile email'; |
| 107 | + export let scope: string = 'openid profile email'; |
108 | 108 |
|
109 | 109 | setContext(OIDC_CONTEXT_REDIRECT_URI, redirect_uri); |
110 | 110 | setContext(OIDC_CONTEXT_POST_LOGOUT_REDIRECT_URI, post_logout_redirect_uri); |
|
168 | 168 | if (params.has('code')) { |
169 | 169 | // handle the callback |
170 | 170 | const response = await oidc.signinCallback(); |
171 | | - let state = (response && response.state) || {}; |
| 171 | + let state: { targetUrl?: string; isRedirectCallback?: boolean } = (response && response.state) || {}; |
172 | 172 | // Can be smart here and redirect to original path instead of root |
173 | 173 | const url = state && state.targetUrl ? state.targetUrl : window.location.pathname; |
174 | 174 | state = { ...state, isRedirectCallback: true }; |
|
0 commit comments