Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add health endpoint #20

Merged
merged 2 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ KINDE_SCOPE=profile email offline openid
KINDE_USER_EMAIL_TEST= // An user has existed in your organization
KINDE_USER_PASSWORD_TEST=
KINDE_AUTH_WITH_PKCE= // Set `true` if you want to use Authentication Code Flow with PKCE
KINDE_DEBUG= // Set `true` if you want to enable the `api/auth/health` endpoint
```

Finally, you can simply run the command
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"svelte": "^4.0.0"
},
"dependencies": {
"@kinde-oss/kinde-typescript-sdk": "^2.4.0"
"@kinde-oss/kinde-typescript-sdk": "^2.6.2"
},
"devDependencies": {
"@playwright/test": "^1.28.1",
Expand Down
8 changes: 6 additions & 2 deletions src/lib/config/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
import {env} from '$env/dynamic/private';
import {version} from '$app/environment';

export const kindeConfiguration = {
authDomain: env.KINDE_ISSUER_URL,
Expand All @@ -11,11 +12,14 @@ export const kindeConfiguration = {
scope: env.KINDE_SCOPE,
clientSecret: env.KINDE_CLIENT_SECRET,
loginRedirectURL: env.KINDE_POST_LOGIN_REDIRECT_URL,
authUsePKCE: [true, 'true'].includes(env.KINDE_AUTH_WITH_PKCE)
authUsePKCE: [true, 'true'].includes(env.KINDE_AUTH_WITH_PKCE),
debug: env.KINDE_DEBUG || process.env.NODE_ENV !== 'production'
};

export const kindeAPIConfiguration = {
audience: `${env.KINDE_ISSUER_URL}/api`,
clientId: env.KINDE_CLIENT_ID,
clientSecret: env.KINDE_CLIENT_SECRET
clientSecret: env.KINDE_CLIENT_SECRET,
framework: 'Sveltekit',
frameworkVersion: version
};
27 changes: 27 additions & 0 deletions src/lib/handleAuth/handleAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {kindeConfiguration} from '$lib/index.js';
import {parseSearchParamsToObject} from '$lib/utils/index.js';
import type {SessionManager} from '@kinde-oss/kinde-typescript-sdk';
import {error, redirect, type RequestEvent} from '@sveltejs/kit';
import {version} from '$app/environment';

export async function handleAuth({
request,
Expand All @@ -15,6 +16,32 @@ export async function handleAuth({
case 'login':
url = await kindeAuthClient.login(request as unknown as SessionManager, options);
break;
case 'health':
if (!kindeConfiguration.debug) {
url = new URL(kindeConfiguration.loginRedirectURL);
break;
}
return new Response(
JSON.stringify({
authDomain: kindeConfiguration.authDomain || '',
clientId: kindeConfiguration.clientId || '',
logoutRedirectURL: kindeConfiguration.logoutRedirectURL || '',
redirectURL: kindeConfiguration.redirectURL || '',
audience: kindeConfiguration.audience || '',
scope: kindeConfiguration.scope || '',
clientSecret: kindeConfiguration.clientSecret.match('[a-z0-9]{32}')
? 'Set correctly'
: 'Not set correctly',
loginRedirectURL: kindeConfiguration.loginRedirectURL || '',
authUsePKCE: kindeConfiguration.authUsePKCE,
version: version,
framework: 'sveltekit'
}),
{
status: 200,
headers: {'Content-Type': 'application/json'}
}
);
case 'register':
url = await kindeAuthClient.register(request as unknown as SessionManager, options);
break;
Expand Down
7 changes: 5 additions & 2 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
<h1>Welcome to SvelteKit</h1>
<p>Visit <a href="https://kit.svelte.dev">kit.svelte.dev</a> to read the documentation</p>
<script>
import {version} from '$app/environment';
</script>

<h1>Welcome to Kinde Svelte SDK v{version}</h1>
11 changes: 10 additions & 1 deletion svelte.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import adapter from '@sveltejs/adapter-auto';
import { vitePreprocess } from '@sveltejs/kit/vite';
import { readFileSync } from 'fs';
import { fileURLToPath } from 'url';

const file = fileURLToPath(new URL('package.json', import.meta.url));
const json = readFileSync(file, 'utf8');
const pkg = JSON.parse(json);

/** @type {import('@sveltejs/kit').Config} */
const config = {
Expand All @@ -11,7 +17,10 @@ const config = {
// adapter-auto only supports some environments, see https://kit.svelte.dev/docs/adapter-auto for a list.
// If your environment is not supported or you settled on a specific environment, switch out the adapter.
// See https://kit.svelte.dev/docs/adapters for more information about adapters.
adapter: adapter()
adapter: adapter(),
version: {
name: pkg.version,
}
}
};

Expand Down