Skip to content

Commit 44a3595

Browse files
committed
Support org login/register, add script build when running the test script, update the README and .env_example file
1 parent 9fbd183 commit 44a3595

File tree

6 files changed

+59
-35
lines changed

6 files changed

+59
-35
lines changed

.env_example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ KINDE_POST_LOGOUT_REDIRECT_URL=http://localhost:4173
77
KINDE_REDIRECT_URL=http://localhost:4173/api/auth/kinde_callback
88
KINDE_SCOPE=profile email offline openid
99
KINDE_USER_EMAIL_TEST= // An user has existed in your organization
10-
KINDE_USER_PASSWORD_TEST=
10+
KINDE_USER_PASSWORD_TEST=
11+
KINDE_AUTH_WITH_PKCE=

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ KINDE_REDIRECT_URL=http://localhost:4173/api/auth/kinde_callback
8282
KINDE_SCOPE=profile email offline openid
8383
KINDE_USER_EMAIL_TEST= // An user has existed in your organization
8484
KINDE_USER_PASSWORD_TEST=
85+
KINDE_AUTH_WITH_PKCE= // Set `true` if you want to use Authentication Code Flow with PKCE
8586
```
8687
8788
Finally, you can simply run the command

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"preview": "vite preview",
99
"package": "svelte-kit sync && svelte-package && publint",
1010
"prepublishOnly": "npm run package",
11-
"test": "npm run test:integration",
11+
"test": "vite build && npm run test:integration",
1212
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
1313
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
1414
"lint": "prettier --plugin-search-dir . --check . && eslint .",

src/lib/handleAuth/handleAuth.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
import {kindeAuthClient} from '$lib/KindeSDK.js';
22
import {kindeConfiguration} from '$lib/index.js';
3+
import {parseSearchParamsToObject} from '$lib/utils/index.js';
34
import type {SessionManager} from '@kinde-oss/kinde-typescript-sdk';
45
import {error, redirect, type RequestEvent} from '@sveltejs/kit';
56

6-
export async function handleAuth({request, params}: RequestEvent): Promise<Response> {
7+
export async function handleAuth({
8+
request,
9+
params,
10+
url: originURL
11+
}: RequestEvent): Promise<Response> {
12+
const options = parseSearchParamsToObject(originURL.search);
713
let url: URL | null = null;
814
switch (params.kindeAuth) {
915
case 'login':
10-
url = await kindeAuthClient.login(request as unknown as SessionManager);
16+
url = await kindeAuthClient.login(request as unknown as SessionManager, options);
1117
break;
1218
case 'register':
13-
url = await kindeAuthClient.register(request as unknown as SessionManager);
19+
url = await kindeAuthClient.register(request as unknown as SessionManager, options);
1420
break;
1521
case 'create_org':
16-
url = await kindeAuthClient.createOrg(request as unknown as SessionManager);
22+
url = await kindeAuthClient.createOrg(request as unknown as SessionManager, options);
1723
break;
1824
case 'kinde_callback':
1925
await kindeAuthClient.handleRedirectToApp(

src/lib/utils/index.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,24 @@ export const pick = (target: Record<string, unknown>, path: string[]) => {
88
};
99

1010
export const omit = (target: Record<string, unknown>, path: string[]) => {
11-
return Object.keys(target).reduce((prev, curr) => {
12-
if (path.includes(curr)) {
13-
delete prev[curr];
14-
}
15-
return prev;
16-
}, {...target});
11+
return Object.keys(target).reduce(
12+
(prev, curr) => {
13+
if (path.includes(curr)) {
14+
delete prev[curr];
15+
}
16+
return prev;
17+
},
18+
{...target}
19+
);
20+
};
21+
22+
export const parseSearchParamsToObject = (search: string) => {
23+
const searchParams = new URLSearchParams(search);
24+
const paramsObject: Record<string, string | number> = {};
25+
26+
for (const param of searchParams.entries()) {
27+
paramsObject[param[0]] = param[1];
28+
}
29+
30+
return paramsObject;
1731
};

tests/test.ts

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,60 @@
1-
import { expect, test } from '@playwright/test';
1+
import {expect, test} from '@playwright/test';
22
import dotenv from 'dotenv';
33

44
dotenv.config();
55

6-
const HOST = process.env.KINDE_ISSUER_URL || ""
7-
const LOGIN_REDIRECT_URL = process.env.KINDE_POST_LOGIN_REDIRECT_URL || ""
8-
const LOGOUT_REDIRECT_URL = process.env.KINDE_POST_LOGOUT_REDIRECT_URL || ""
9-
const USER_EMAIL = process.env.KINDE_USER_EMAIL_TEST || ""
10-
const USER_PASSWORD = process.env.KINDE_USER_PASSWORD_TEST || ""
6+
const HOST = process.env.KINDE_ISSUER_URL || '';
7+
const LOGIN_REDIRECT_URL = process.env.KINDE_POST_LOGIN_REDIRECT_URL || '';
8+
const LOGOUT_REDIRECT_URL = process.env.KINDE_POST_LOGOUT_REDIRECT_URL || '';
9+
const USER_EMAIL = process.env.KINDE_USER_EMAIL_TEST || '';
10+
const USER_PASSWORD = process.env.KINDE_USER_PASSWORD_TEST || '';
1111

12-
test('Authentication Login', async ({ page }) => {
12+
test('Authentication Login', async ({page}) => {
1313
await page.goto('api/auth/login');
1414

15-
await page.waitForURL(new RegExp(`${HOST}/auth/cx/_:nav&m:login`))
15+
await page.waitForURL(new RegExp(`${HOST}/auth/cx/_:nav&m:login`));
1616

1717
await page.getByLabel('Email').fill(USER_EMAIL);
1818
await page.getByText('Log in').click();
1919
await page.waitForTimeout(3000);
2020
await page.locator('#input_field_p_password_password').fill(USER_PASSWORD);
21-
await page.locator('[type="submit"]', {
22-
hasText: 'Continue'
23-
}).click();
24-
21+
await page
22+
.locator('[type="submit"]', {
23+
hasText: 'Continue'
24+
})
25+
.click();
26+
2527
await expect(async () => {
2628
expect(page.url()).toContain(LOGIN_REDIRECT_URL);
2729
}).toPass();
2830
});
2931

30-
test('Should redirect to registration page', async ({ page }) => {
32+
test('Should redirect to registration page', async ({page}) => {
3133
await page.goto('api/auth/register');
3234

33-
await page.waitForURL(new RegExp(`${HOST}/auth/cx/_:nav&m:register`))
35+
await page.waitForURL(new RegExp(`${HOST}/auth/cx/_:nav&m:register`));
3436

35-
await expect(page.locator('[name=p_first_name]')).toBeVisible()
37+
await expect(page.locator('[name=p_first_name]')).toBeVisible();
3638

37-
await expect(page.locator('[name=p_last_name]')).toBeVisible()
39+
await expect(page.locator('[name=p_last_name]')).toBeVisible();
3840

39-
await expect(page.locator('[name=p_email]')).toBeVisible()
41+
await expect(page.locator('[name=p_email]')).toBeVisible();
4042
});
4143

42-
test('Should redirect to create org page', async ({ page }) => {
44+
test('Should redirect to create org page', async ({page}) => {
4345
await page.goto('api/auth/create_org');
4446

45-
await expect(page.locator('[name=p_first_name]')).toBeVisible()
47+
await expect(page.locator('[name=p_first_name]')).toBeVisible();
4648

47-
await expect(page.locator('[name=p_last_name]')).toBeVisible()
49+
await expect(page.locator('[name=p_last_name]')).toBeVisible();
4850

49-
await expect(page.locator('[name=p_email]')).toBeVisible()
51+
await expect(page.locator('[name=p_email]')).toBeVisible();
5052
});
5153

52-
test('Should redirect to logout page', async ({ page }) => {
54+
test('Should redirect to logout page', async ({page}) => {
5355
await page.goto('api/auth/logout');
5456

5557
await expect(async () => {
5658
expect(page.url()).toContain(LOGOUT_REDIRECT_URL);
5759
}).toPass();
58-
});
60+
});

0 commit comments

Comments
 (0)