chore: consolidate into single component file.

This commit is contained in:
Darrel O'Pry 2020-08-12 19:58:59 -04:00
parent 701668b662
commit 215a0a443f
3 changed files with 131 additions and 129 deletions

View file

@ -1,19 +1,73 @@
<script>
<script context="module">
import { writable } from 'svelte/store';
import { getContext } from 'svelte';
import oidcClient from 'oidc-client';
const { UserManager } = oidcClient;
import { onMount, onDestroy, setContext, getContext } from 'svelte';
import {
OIDC_CONTEXT_REDIRECT_URI,
OIDC_CONTEXT_CLIENT_PROMISE,
OIDC_CONTEXT_POST_LOGOUT_REDIRECT_URI,
idToken,
accessToken,
isAuthenticated,
isLoading,
authError,
userInfo,
} from './oidc';
import { onMount, onDestroy, setContext } 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);
await oidc.signinSilent();
}
/**
* 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;
oidc.signoutRedirect({ returnTo });
}
</script>
<script>
// props.
export let issuer;
export let client_id;
@ -24,11 +78,10 @@
setContext(OIDC_CONTEXT_REDIRECT_URI, redirect_uri);
setContext(OIDC_CONTEXT_POST_LOGOUT_REDIRECT_URI, post_logout_redirect_uri);
// getContext doesn't seem to return a value in OnMount, so we'll pass the oidcPromise around by reference.
const settings = {
authority: issuer,
client_id,
response_type: 'id_token token',
// response_type: 'id_token token',
redirect_uri,
post_logout_redirect_uri,
response_type: 'code',

View file

@ -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';

View file

@ -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 });
}