Last active
August 29, 2021 17:19
-
-
Save dcaponi/4369876f33653a8417ac8176a56e2d4b to your computer and use it in GitHub Desktop.
implementation of route protection component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script lang="ts"> | |
import Client from "@onelogin/sdk"; | |
import { Route } from 'svelte-routing'; | |
import { accessToken } from './stores'; | |
import Login from '../pages/Login.svelte'; | |
export let path: string; | |
export let component: any; | |
$: isAuthenticated = $accessToken; | |
const onelogin = new Client( { region: "us" } ); | |
let pkceClient = onelogin.pkce.Configure({ | |
redirectURL: `http://localhost:8080${path}`, | |
clientID: process.env.ONELOGIN_CLIENT_ID | |
}); | |
pkceClient.CreateAuthURL(); // stores auth URL in localStorage automatically - you can also await and use the url elsewhere | |
let urlParams = new URLSearchParams( window.location.search ); | |
if ( urlParams.has( 'code' ) && !isAuthenticated ) { | |
let code = urlParams.get( 'code' ); | |
pkceClient.GetAccessTokenAsync( code ).then( token => { | |
if( token.access_token ) { | |
localStorage.removeItem( "auth-url" ); | |
accessToken.set( token.access_token ); // set in svelte store | |
localStorage.setItem( "accessToken", token.access_token ); // set in localStorage to survive hard refresh | |
} | |
} ); | |
} | |
</script> | |
{#if isAuthenticated} | |
<Route path={path} component={component} /> | |
{:else} | |
<Route path={path} component={Login} /> | |
{/if} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment