Skip to content

Commit c2e508f

Browse files
committed
🪳
1 parent ac9e8cf commit c2e508f

File tree

7 files changed

+33
-122
lines changed

7 files changed

+33
-122
lines changed

app1/ui/Home.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default function Home() {
2929
session={sessions[0]}
3030
sessions={sessions}
3131
signOut={() => {
32-
window.location.href = `https://auth.example.local/auth/signout?id_token_hint=${sessions[0].idToken}&return_url=https://app.example.local/app1/hello`;
32+
window.location.href = `https://auth.example.local/auth/signout?id_token_hint=${sessions[0]?.idToken}&return_url=https://app.example.local/app1`;
3333
}}
3434
/>
3535
</div>

app1/ui/components/ProfileImage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export default function ProfileImage(props: {
9191
return_url: "https://app.example.local/app1",
9292
});
9393

94-
window.location.href = `https://auth.example.local/auth/signin?${params.toString()}`;
94+
window.location.href = `https://auth.example.local/auth/signin?${params}`;
9595
}}
9696
>
9797
+ Add other account

app2/ui/Home.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export default function Home() {
3737
return_url: "https://app.example.local/app2",
3838
});
3939

40-
window.location.href = `https://auth.example.local/auth/signin?${params.toString()}`;
40+
window.location.href = `https://auth.example.local/auth/signin?${params}`;
4141
}}
4242
>
4343
Login
@@ -48,7 +48,7 @@ export default function Home() {
4848
return_url: "https://app.example.local/app2",
4949
});
5050

51-
window.location.href = `https://auth.example.local/auth/signout?${params.toString()}`;
51+
window.location.href = `https://auth.example.local/auth/signout?${params}`;
5252
}}
5353
>
5454
Logout

auth/app/api/auth/signin/route.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,38 +52,25 @@ export async function POST(request: NextRequest) {
5252
const codeChallenge = generateCodeChallenge(codeVerifier);
5353
const state = generateState();
5454

55-
const params: {
56-
code_challenge: string;
57-
code_challenge_method: string;
58-
client_id: string;
59-
redirect_uri: string;
60-
response_type: string;
61-
scope: string;
62-
state: string;
63-
prompt?: string;
64-
login_hint?: string;
65-
} = {
55+
const params = new URLSearchParams({
6656
code_challenge: codeChallenge,
6757
code_challenge_method: "S256",
6858
client_id: configuration.portal.clientId,
6959
redirect_uri: configuration.portal.redirectUrl,
7060
response_type: "code",
7161
scope,
7262
state,
73-
};
74-
75-
if (prompt) params.prompt = prompt;
76-
if (loginHint) params.login_hint = loginHint;
63+
});
7764

78-
const authorizeUrl = `${
79-
wellKnown.authorization_endpoint
80-
}?${new URLSearchParams(params).toString()}`;
65+
if (prompt) params.set("prompt", prompt);
66+
if (loginHint) params.set("login_hint", loginHint);
8167

8268
if (returnUrl) setShortLiveCookie(returnUrlCookieName, returnUrl);
8369
setShortLiveCookie(stateCookieName, state);
8470
setShortLiveCookie(redirectUrlCookieName, configuration.portal.redirectUrl);
8571
setShortLiveCookie(codeVerifierCookieName, codeVerifier);
8672
deleteCookie(csrfTokenCookieName);
8773

74+
const authorizeUrl = `${wellKnown.authorization_endpoint}?${params}`;
8875
return NextResponse.json({ authorizeUrl });
8976
}

auth/app/api/auth/signout/route.ts

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import configuration from "@/configuration";
2-
import { returnUrlCookieName } from "@/lib/constant";
2+
import { authSessionCookieName, returnUrlCookieName } from "@/lib/constant";
33
import { setShortLiveCookie } from "@/lib/cookie";
44
import { prisma } from "@/lib/prisma";
5+
import { Prisma } from "@prisma/client";
6+
import { cookies } from "next/headers";
57
import { NextRequest, NextResponse } from "next/server";
68

79
export async function POST(request: NextRequest) {
@@ -15,6 +17,10 @@ export async function POST(request: NextRequest) {
1517
};
1618
const { returnUrl, idTokenHint, state } = body;
1719

20+
const requestCookie = cookies();
21+
const authSessionCookie = requestCookie.get(authSessionCookieName);
22+
const authSession = authSessionCookie?.value;
23+
1824
const wellKnownResponse = await fetch(
1925
`${configuration.portal.issuer}/.well-known/openid-configuration`
2026
);
@@ -31,36 +37,33 @@ export async function POST(request: NextRequest) {
3137
throw { code: wellKnownResponse.status, details: wellKnown };
3238
}
3339

34-
const params: {
35-
client_id?: string;
36-
post_logout_redirect_uri?: string;
37-
id_token_hint?: string;
38-
state?: string;
39-
} = {
40+
const params = new URLSearchParams({
4041
client_id: configuration.portal.clientId,
4142
post_logout_redirect_uri: configuration.portal.postLogoutRedirectUri,
42-
};
43+
});
4344

44-
if (idTokenHint) params.id_token_hint = idTokenHint;
45-
if (state) params.state = state;
45+
if (state) params.set("state", state);
46+
47+
const sessionWhereInput: Prisma.SessionWhereInput = {
48+
authSession: authSessionCookie?.value,
49+
};
4650

4751
if (idTokenHint) {
48-
await prisma.session.updateMany({
49-
where: {
50-
idToken: idTokenHint,
51-
},
52-
data: {
53-
deletedAt: new Date(),
54-
},
55-
});
52+
params.set("id_token_hint", idTokenHint);
53+
sessionWhereInput.idToken = idTokenHint;
5654
}
5755

58-
const endSessionUrl = `${
59-
wellKnown.end_session_endpoint
60-
}?${new URLSearchParams(params).toString()}`;
56+
await prisma.session.updateMany({
57+
where: sessionWhereInput,
58+
data: {
59+
deletedAt: new Date(),
60+
},
61+
});
6162

6263
if (returnUrl) setShortLiveCookie(returnUrlCookieName, returnUrl);
6364

65+
const endSessionUrl = `${wellKnown.end_session_endpoint}?${params}`;
66+
6467
return NextResponse.json({ endSessionUrl });
6568
} catch (error: any) {
6669
return NextResponse.json(error.details || { message: error.message }, {

auth/app/api/v1/signout/route.ts

Lines changed: 0 additions & 79 deletions
This file was deleted.

auth/prisma/dev.db

4 KB
Binary file not shown.

0 commit comments

Comments
 (0)