An Oidc Client Component for Svelte.
Setup an OIDC Sever
Get the authority and client_id
npm install @dopry/svelte-oidc
App.svelte
# App.svelte
import {
OidcContext,
LoginButton,
LogoutButton,
RefreshTokenButton,
authError,
accessToken,
idToken,
isAuthenticated,
isLoading,
login,
logout,
userInfo,
} from '@dopry/svelte-oidc';
const metadata = {
// added to overcome missing value in auth0 .well-known/openid-configuration
// see: https://github.com/IdentityModel/oidc-client-js/issues/1067
// see: https://github.com/IdentityModel/oidc-client-js/pull/1068
end_session_endpoint: `process.env.OIDC_ISSUER/v2/logout?client_id=process.env.OIDC_CLIENT_ID`,
};
</script>
<OidcContext
issuer="https://dev-hvw40i79.auth0.com"
client_id="aOijZt2ug6Ovgzp0HXdF23B6zxwA6PaP"
redirect_uri="https://darrelopry.com/svelte-oidc/"
post_logout_redirect_uri="https://darrelopry.com/svelte-oidc/"
metadata={metadata}
extraOptions={{
mergeClaims: true,
resource: "some_identifier",
}}
>
<LoginButton>Login</LoginButton>
<LogoutButton>Logout</LogoutButton>
<RefreshTokenButton>RefreshToken</RefreshTokenButton><br />
<pre>isLoading: {$isLoading}</pre>
<pre>isAuthenticated: {$isAuthenticated}</pre>
<pre>authToken: {$accessToken}</pre>
<pre>idToken: {$idToken}</pre>
<pre>userInfo: {JSON.stringify($userInfo, null, 2)}</pre>
<pre>authError: {$authError}</pre>
</OidcContext>
This component does not natively support SSR nor can it be used for authentication in server side rendered contexts. It can be used within SSR applications as long as it is acceptable for all authentication to be client side. In order to use for client side auth in an SSR application you will need to ensure it is not rendered server side as follows.
{#if process.browser} <OidcContext> ..... </OidcContext> {/if}
Same as what is needed for Sapper (see above section). To do this, we need to import in the script
section:
import { browser } from '$app/env';
And in the main
:
{#if browser} <OidcContext> ..... </OidcContext> {/if}
Contributors are Welcome. There is a lot of value in a vendor neutral OIDC component for use by the Svelte and Sapper community. As a developer and product manager, I have needed to switch between Okta, Auth0, KeyCloak, IdentityServer, and Ory on multiple occasions. Vendor specific libraries are usually riddled with vendor specific assumptions that make the migration hard.
How to Help!
PRs Welcome!
OidcContext - component to initiate the OIDC client. You only need one instance in your DOM tree at the root.
Attributes:
LoginButton - log out the current context
Attributes:
LogoutButton - log in the current context
Attributes:
RefreshTokenButton - refresh the current token
npm run showcase:dev
use semver