From 215a0a443f489bbe7e40242e4518cffed09b555b Mon Sep 17 00:00:00 2001 From: Darrel O'Pry Date: Wed, 12 Aug 2020 19:58:59 -0400 Subject: [PATCH] chore: consolidate into single component file. --- src/components/OidcContext.svelte | 185 ++++++++++++++++++---------- src/components/components.module.js | 14 ++- src/components/oidc.js | 61 --------- 3 files changed, 131 insertions(+), 129 deletions(-) delete mode 100644 src/components/oidc.js diff --git a/src/components/OidcContext.svelte b/src/components/OidcContext.svelte index 8b2f259..8a05fc5 100644 --- a/src/components/OidcContext.svelte +++ b/src/components/OidcContext.svelte @@ -1,55 +1,108 @@ - + + diff --git a/src/components/components.module.js b/src/components/components.module.js index 22d61c8..bcf4147 100644 --- a/src/components/components.module.js +++ b/src/components/components.module.js @@ -1,3 +1,13 @@ -export * from './oidc' -export { default as OidcContext } from './OidcContext.svelte'; +export { + default as OidcContext, + authError, + idToken, + accessToken, + isAuthenticated, + isLoading, + login, + logout, + refreshToken, + userInfo, +} from './OidcContext.svelte'; diff --git a/src/components/oidc.js b/src/components/oidc.js deleted file mode 100644 index e0a5c4e..0000000 --- a/src/components/oidc.js +++ /dev/null @@ -1,61 +0,0 @@ -import { writable } from 'svelte/store'; -import { getContext } from 'svelte'; - -/** - * Stores - */ -export const isLoading = writable(true); -export const isAuthenticated = writable(false); -export const accessToken = writable(''); -export const idToken = writable(''); -export const userInfo = writable({}); -export const authError = writable(null); - -/** - * Context Keys - * - * using an object literal means the keys are guaranteed not to conflict in any circumstance (since an object only has - * referential equality to itself, i.e. {} !== {} whereas "x" === "x"), even when you have multiple different contexts - * operating across many component layers. - */ -export const OIDC_CONTEXT_CLIENT_PROMISE = {}; -export const OIDC_CONTEXT_REDIRECT_URI = {}; -export const OIDC_CONTEXT_POST_LOGOUT_REDIRECT_URI = {}; - -/** - * Refresh the accessToken store. - */ -export async function refreshToken() { - const oidc = await getContext(OIDC_CONTEXT_CLIENT_PROMISE) - const token = await oidc.signinSilent(); - accessToken.set(token.accessToken); - idToken.set(token.idToken); -} - -/** - * Initiate Register/Login flow. - * - * @param {boolean} preserveRoute - store current location so callback handler will navigate back to it. - * @param {string} callback_url - explicit path to use for the callback. - */ -export async function login(preserveRoute = true, callback_url = null) { - const oidc = await getContext(OIDC_CONTEXT_CLIENT_PROMISE) - const redirect_uri = callback_url || getContext(OIDC_CONTEXT_REDIRECT_URI) || window.location.href; - - // try to keep the user on the same page from which they triggered login. If set to false should typically - // cause redirect to /. - const appState = (preserveRoute) ? { pathname: window.location.pathname, search: window.location.search } : {} - await oidc.signinRedirect({ redirect_uri, appState }); -} - -/** - * Log out the current user. - * - * @param {string} logout_url - specify the url to return to after login. - */ -export async function logout(logout_url = null) { - const oidc = await getContext(OIDC_CONTEXT_CLIENT_PROMISE) - const returnTo = logout_url || getContext(OIDC_CONTEXT_POST_LOGOUT_REDIRECT_URI) || window.location.href; - accessToken.set(''); - oidc.signoutRedirect({ returnTo }); -} \ No newline at end of file