@affinity-lab/sk-mik-id-sso-client
v1.0.1
Published
MIK ID SSO for sveltekit projects
Downloads
9
Keywords
Readme
@affinity-lab/sk-mik-id-sso-client
MIK ID SSO for sveltekit projects
Installation
npm i -s @affinity-lab/sk-mik-id-sso-client
Usage
Set up the service
$lib/services/sso-client.ts
import {type NeptunUser, SSOClient} from "@affinity-lab/sk-mik-id-sso-client";
import type {RequestEvent} from "@sveltejs/kit";
import {PRIVATE_MIKID_AUTH_REQUEST_URL, PRIVATE_MIKID_LOGOUT_URL, PRIVATE_APP_ID, PRIVATE_APP_KEY} from "$env/static/private";
export let ssoClient = new SSOClient(
PRIVATE_MIKID_AUTH_REQUEST_URL,
PRIVATE_MIKID_LOGOUT_URL,
PRIVATE_APP_ID,
PRIVATE_APP_KEY,
(event: RequestEvent, user: NeptunUser) => {
// ... do whatever you want, e.g: store the user in session
event.locals.session.user = user
},
(event: RequestEvent) =>{
// ... do whatever you want, e.g: remove the user from the session
event.locals.session.user = undefined
}
)
Use it in your routes
routes/sso/[action]/+server.ts
import type {RequestEvent} from '@sveltejs/kit';
import {error} from "@sveltejs/kit";
import {ssoClient} from "$lib/services/sso-client";
export async function GET(event: RequestEvent) {
switch (event.params["action"]) {
case "sign-in":
return ssoClient.signIn(event, event.url.origin + "/sso/authenticated");
case "authenticated":
return ssoClient.authenticated(event, event.url.origin, event.url.origin);
case "sign-out":
return ssoClient.signOut(event, event.url.origin);
}
throw error(404, {message: 'Not found'});
}
Sign in and sign-out
In your pages do some real relocation using navto
from @affinity-lab/sk-util
or on an anchor
tag with data-sveltekit-reload
attribute to the urls defined before. (/sso/sign-in
, /sso/sing-out
)
<script lang="ts">
import {navto} from "@affinity-lab/sk-util";
const singIn = ()=>navto("/sso/sign-in");
</script>
<button on:click={signIn}>Sign In</button>
<a href="/sso/sign-out" data-sveltekit-reload>Sign Out</a>