Skip to content

Commit ee5edbf

Browse files
authored
Merge pull request #429 from pesickaa/feat/invitation-codes
Feat: Support invitation codes
2 parents 17c98f7 + 164f0a4 commit ee5edbf

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@kinde-oss/kinde-auth-nextjs",
3-
"version": "2.11.0",
3+
"version": "2.11.1",
44
"description": "Kinde Auth SDK for NextJS",
55
"main": "dist/cjs/index.js",
66
"module": "dist/index.js",
@@ -61,7 +61,8 @@
6161
"release": "release-it",
6262
"lint": "prettier --check .",
6363
"lint:fix": "prettier --write .",
64-
"dev": "cd playground && pnpm run dev"
64+
"dev": "cd playground && pnpm run dev",
65+
"dev:prepare": "rm -rf playground && git clone --depth 1 https://github.com/kinde-starter-kits/kinde-nextjs-app-router-starter-kit playground && rm -rf playground/.git && mv playground/.env_sample playground/.env && cd playground && pnpm i && pnpm link .."
6566
},
6667
"author": {
6768
"name": "Kinde",

playground/next-env.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/// <reference types="next" />
22
/// <reference types="next/image-types/global" />
3+
/// <reference path="./.next/types/routes.d.ts" />
34

45
// NOTE: This file should not be edited
5-
// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information.
6+
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.

src/authMiddleware/authMiddleware.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,48 @@ import { getStandardCookieOptions } from "../utils/cookies/getStandardCookieOpti
1414
import { isPublicPathMatch } from "../utils/isPublicPathMatch";
1515
import { TWENTY_NINE_DAYS } from "src/utils/constants";
1616

17+
/**
18+
* Handles invitation code redirect logic.
19+
* Redirects to the register page with the invitation code, or to login on error.
20+
*/
21+
const handleInvitationCodeRedirect = (
22+
invitationCode: string,
23+
registerPage: string,
24+
loginRedirectUrl: string,
25+
redirectURLBase: string | undefined,
26+
): NextResponse => {
27+
try {
28+
const params = new URLSearchParams();
29+
params.set("invitation_code", invitationCode);
30+
params.set("is_invitation", "true");
31+
32+
const registerWithInviteRedirectUrl = `${registerPage}?${params.toString()}`;
33+
34+
return NextResponse.redirect(
35+
new URL(
36+
registerWithInviteRedirectUrl,
37+
redirectURLBase || config.redirectURL,
38+
),
39+
);
40+
} catch (error) {
41+
if (config.isDebugMode) {
42+
console.error(
43+
"authMiddleware: error redirecting to register with invitation code",
44+
error,
45+
);
46+
}
47+
return NextResponse.redirect(
48+
new URL(loginRedirectUrl, redirectURLBase || config.redirectURL),
49+
);
50+
}
51+
};
52+
1753
const handleMiddleware = async (req, options, onSuccess) => {
1854
const { pathname, search } = req.nextUrl;
1955

56+
const params = new URLSearchParams(search);
57+
const invitationCode = params.get("invitation_code");
58+
const hasInvitationCode = !!invitationCode?.trim();
2059
const isReturnToCurrentPage = options?.isReturnToCurrentPage;
2160
const orgCode: string | undefined = options?.orgCode;
2261
const loginPage = options?.loginPage || `${config.apiPath}/${routes.login}`;
@@ -39,7 +78,6 @@ const handleMiddleware = async (req, options, onSuccess) => {
3978
publicPaths = options.publicPaths;
4079
}
4180
}
42-
4381
const loginRedirectUrlParams = new URLSearchParams();
4482

4583
if (orgCode) {
@@ -55,6 +93,15 @@ const handleMiddleware = async (req, options, onSuccess) => {
5593
? `${loginPage}?${queryString}`
5694
: loginPage;
5795

96+
if (hasInvitationCode) {
97+
return handleInvitationCodeRedirect(
98+
invitationCode,
99+
registerPage,
100+
loginRedirectUrl,
101+
options?.redirectURLBase,
102+
);
103+
}
104+
58105
// Use extracted utility for public path matching
59106
// eslint-disable-next-line @typescript-eslint/no-var-requires
60107
const isPublicPath = isPublicPathMatch(

0 commit comments

Comments
 (0)