2020-05-28 11:29:21 -04:00
|
|
|
# svelte-oidc
|
|
|
|
|
|
|
|
|
|
An Oidc Client Component for Svelte.
|
|
|
|
|
|
|
|
|
|
[Try out the demo](https://darrelopry.com/svelte-oidc/)
|
|
|
|
|
|
|
|
|
|
## Getting Started
|
|
|
|
|
|
|
|
|
|
Setup an OIDC Sever
|
|
|
|
|
* https://www.ory.sh/
|
|
|
|
|
* https://www.keycloak.org/
|
|
|
|
|
* https://www.okta.com/
|
|
|
|
|
* http://auth0.com/
|
|
|
|
|
|
|
|
|
|
Get the authority and client_id
|
|
|
|
|
|
|
|
|
|
`npm install @dopry/svelte-oidc`
|
|
|
|
|
|
|
|
|
|
App.svelte
|
|
|
|
|
```
|
|
|
|
|
# App.svelte
|
|
|
|
|
import {
|
|
|
|
|
OidcContext,
|
|
|
|
|
authError,
|
2020-10-21 18:59:04 +11:00
|
|
|
accessToken,
|
2020-05-28 11:29:21 -04:00
|
|
|
idToken,
|
|
|
|
|
isAuthenticated,
|
|
|
|
|
isLoading,
|
|
|
|
|
login,
|
|
|
|
|
logout,
|
|
|
|
|
userInfo,
|
|
|
|
|
} from '@dopry/svelte-oidc';
|
2020-06-04 03:22:48 -04:00
|
|
|
|
|
|
|
|
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`,
|
|
|
|
|
};
|
2020-05-28 11:29:21 -04:00
|
|
|
</script>
|
|
|
|
|
|
2020-05-28 12:53:42 -04:00
|
|
|
<OidcContext
|
|
|
|
|
issuer="https://dev-hvw40i79.auth0.com"
|
|
|
|
|
client_id="aOijZt2ug6Ovgzp0HXdF23B6zxwA6PaP"
|
|
|
|
|
redirect_uri="https://darrelopry.com/svelte-oidc/"
|
2020-06-04 03:22:48 -04:00
|
|
|
post_logout_redirect_uri="https://darrelopry.com/svelte-oidc/"
|
2020-10-21 18:54:45 +11:00
|
|
|
metadata={metadata}
|
2020-06-04 03:22:48 -04:00
|
|
|
>
|
2020-05-28 12:53:42 -04:00
|
|
|
|
2020-05-28 11:29:21 -04:00
|
|
|
<button on:click|preventDefault='{() => login() }'>Login</button>
|
|
|
|
|
<button on:click|preventDefault='{() => logout() }'>Logout</button><br />
|
|
|
|
|
<pre>isLoading: {$isLoading}</pre>
|
|
|
|
|
<pre>isAuthenticated: {$isAuthenticated}</pre>
|
2020-06-04 03:22:48 -04:00
|
|
|
<pre>authToken: {$accessToken}</pre>
|
|
|
|
|
<pre>idToken: {$idToken}</pre>
|
2020-05-28 11:29:21 -04:00
|
|
|
<pre>userInfo: {JSON.stringify($userInfo, null, 2)}</pre>
|
|
|
|
|
<pre>authError: {$authError}</pre>
|
|
|
|
|
</OidcContext>
|
|
|
|
|
```
|
|
|
|
|
|
2020-08-20 11:54:55 -04:00
|
|
|
## Sapper/SSR
|
|
|
|
|
|
|
|
|
|
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}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Contributing
|
|
|
|
|
|
|
|
|
|
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!**
|
|
|
|
|
|
|
|
|
|
* Better Documentation
|
|
|
|
|
* Helping with the Issue Queue (support, good bug report, resolving bugs)
|
|
|
|
|
* SSR Support
|
|
|
|
|
* Automated Testing of all major identity providers
|
|
|
|
|
|
|
|
|
|
**PRs Welcome!**
|
|
|
|
|
|
2020-05-28 11:29:21 -04:00
|
|
|
## Docs
|
|
|
|
|
|
|
|
|
|
### Components
|
|
|
|
|
* OidcContext - component to initiate the OIDC client. You only need one instance in your DOM tree at the root.
|
|
|
|
|
|
|
|
|
|
Attributes:
|
2020-05-28 12:53:42 -04:00
|
|
|
* issuer - OIDC Authority/issuer/base url for .well-known/openid-configuration
|
2020-05-28 11:29:21 -04:00
|
|
|
* client_id - OAuth ClientId
|
|
|
|
|
* redirect_uri - default: window.location.href
|
|
|
|
|
* post_logout_redirect_uri - override the default url that OIDC will redirect to after logout. default: window.location.href
|
2020-06-04 03:22:48 -04:00
|
|
|
* metadata - set default metadata or metadata missing from authority.
|
2020-05-28 11:29:21 -04:00
|
|
|
|
|
|
|
|
### Functions
|
|
|
|
|
* login(preseveRoute = true, callback_url = null) - begin a user login.
|
|
|
|
|
* logout(logout_url = null) - logout a user.
|
|
|
|
|
* refreshToken - function to refresh a token.
|
|
|
|
|
|
|
|
|
|
### Stores
|
|
|
|
|
* isLoading - if true OIDC Context is still loading.
|
|
|
|
|
* isAuthenticated - true if user is currently authenticated
|
2020-06-04 03:22:48 -04:00
|
|
|
* accessToken - access token for connecting to apis.
|
|
|
|
|
* idToken - identity token for getting user info easily.
|
|
|
|
|
* userInfo - the currently logged in user's info from OIDC userInfo endpoint
|
2020-05-28 11:29:21 -04:00
|
|
|
* authError - the last authentication error.
|
|
|
|
|
|
|
|
|
|
### Constants
|
2020-06-04 03:22:48 -04:00
|
|
|
* OIDC_CONTEXT_CALLBACK_URL
|
2020-05-28 11:29:21 -04:00
|
|
|
* OIDC_CONTEXT_CLIENT_PROMISE - key for the OIDC client in setContext/getContext.
|
|
|
|
|
* OIDC_CONTEXT_LOGOUT_URL,
|
|
|
|
|
|
2020-08-20 11:54:55 -04:00
|
|
|
## Development
|
|
|
|
|
npm run showcase:dev
|
|
|
|
|
|
2020-05-28 11:29:21 -04:00
|
|
|
## Release
|
|
|
|
|
**use semver**
|
2020-08-20 11:54:55 -04:00
|
|
|
1. npm publish
|
|
|
|
|
2. npm showcase:build
|
2020-10-21 18:54:45 +11:00
|
|
|
3. npm showcase:publish
|