Skip to content

Commit e1f1a83

Browse files
committed
🐢
1 parent c7b4f25 commit e1f1a83

File tree

11 files changed

+57
-98
lines changed

11 files changed

+57
-98
lines changed

app1/ui/components/ProfileImage.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ export default function ProfileImage(props: {
105105
return_url: "https://app.example.local/app1",
106106
});
107107

108-
if (session?.idToken) {
109-
params.set("id_token_hint", session.idToken);
108+
if (session?.id) {
109+
params.set("session_id", session.id);
110110
}
111111

112-
window.location.href = `https://auth.example.local/auth/signout/zitadel?${params}`;
112+
window.location.href = `https://auth.example.local/auth/signout?${params}`;
113113
}}
114114
>
115115
Logout

app2/ui/Home.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ export default function Home() {
4848
return_url: "https://app.example.local/app2",
4949
});
5050

51-
if (sessions[0]?.idToken) {
52-
params.set("id_token_hint", sessions[0].idToken);
51+
if (sessions[0]?.id) {
52+
params.set("session_id", sessions[0].id);
5353
}
5454

55-
window.location.href = `https://auth.example.local/auth/signout/portal?${params}`;
55+
window.location.href = `https://auth.example.local/auth/signout/?${params}`;
5656
}}
5757
>
5858
Logout

auth/app/api/auth/callback/[provider]/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ async function handler(
9292
await prisma.session.create({
9393
data: {
9494
authSession,
95-
issuer: wellKnown.issuer,
95+
providerId: provider,
9696
accessToken: result.access_token,
9797
tokenType: result.token_type,
9898
expiresIn: result.expires_in,

auth/app/api/auth/signout/[provider]/route.ts renamed to auth/app/api/auth/signout/route.ts

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,28 @@ import { authSessionCookieName, returnUrlCookieName } from "@/lib/constant";
33
import { setShortLiveCookie } from "@/lib/cookie";
44
import { prisma } from "@/lib/prisma";
55
import { getWellKnown } from "@/lib/zitadel";
6-
import { Prisma } from "@prisma/client";
76
import { cookies } from "next/headers";
87
import { NextRequest, NextResponse } from "next/server";
98

10-
export async function POST(
11-
request: NextRequest,
12-
{ params }: { params: { provider: "portal" | "zitadel" } }
13-
) {
9+
export async function POST(request: NextRequest) {
1410
try {
1511
const body = (await request.json()) as {
1612
returnUrl?: string;
17-
idTokenHint?: string;
18-
clientId?: string;
19-
postLogoutRedirectUri?: string;
20-
state?: string;
13+
sessionId?: string;
2114
};
22-
const { returnUrl, idTokenHint, state } = body;
15+
const { sessionId, returnUrl } = body;
2316

24-
const provider = params.provider;
25-
if (!provider) throw new Error("provider not found");
17+
const requestCookie = cookies();
18+
const authSessionCookie = requestCookie.get(authSessionCookieName);
19+
20+
const session = await prisma.session.findFirst({
21+
where: {
22+
id: sessionId,
23+
authSession: authSessionCookie?.value,
24+
},
25+
});
26+
if (!session) throw new Error("session not found");
27+
const provider = session.providerId as "portal" | "zitadel";
2628

2729
const wellKnown = await getWellKnown(configuration[provider].issuer);
2830

@@ -31,33 +33,22 @@ export async function POST(
3133
post_logout_redirect_uri: configuration.postLogoutRedirectUri,
3234
});
3335

34-
if (state) requestParams.set("state", state);
35-
if (idTokenHint) requestParams.set("id_token_hint", idTokenHint);
36-
37-
const requestCookie = cookies();
38-
const authSessionCookie = requestCookie.get(authSessionCookieName);
39-
40-
if (authSessionCookie?.value) {
41-
const sessionWhereInput: Prisma.SessionWhereInput = {
42-
authSession: authSessionCookie.value,
43-
};
44-
45-
if (idTokenHint) {
46-
sessionWhereInput.idToken = idTokenHint;
47-
}
48-
49-
await prisma.session.updateMany({
50-
where: sessionWhereInput,
51-
data: {
52-
deletedAt: new Date(),
53-
},
54-
});
36+
if (session.idToken) {
37+
requestParams.set("id_token_hint", session.idToken);
5538
}
5639

40+
await prisma.session.updateMany({
41+
where: {
42+
id: sessionId,
43+
},
44+
data: {
45+
deletedAt: new Date(),
46+
},
47+
});
48+
5749
if (returnUrl) setShortLiveCookie(returnUrlCookieName, returnUrl);
5850

5951
const endSessionUrl = `${wellKnown.end_session_endpoint}?${requestParams}`;
60-
6152
return NextResponse.json({ endSessionUrl });
6253
} catch (error: any) {
6354
return NextResponse.json(error.details || { message: error.message }, {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export async function GET(request: NextRequest) {
4343
sessions: sessions.map((session) => ({
4444
id: session.id,
4545
authSession: session.authSession,
46-
issuer: session.issuer,
46+
providerId: session.providerId,
4747
tokenType: session.tokenType,
4848
accessToken: session.accessToken,
4949
expiresIn: session.expiresIn,

auth/app/auth/signout/[provider]/page.tsx

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

auth/app/auth/signout/page.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import SignOut from "@/ui/SignOut";
2+
3+
export default async function Page({
4+
searchParams,
5+
}: {
6+
searchParams: {
7+
return_url?: string;
8+
session_id?: string;
9+
};
10+
}) {
11+
return (
12+
<SignOut
13+
returnUrl={searchParams.return_url}
14+
sessionId={searchParams.session_id}
15+
/>
16+
);
17+
}

auth/prisma/dev.db

-8 KB
Binary file not shown.

auth/prisma/migrations/20240811150650_init/migration.sql renamed to auth/prisma/migrations/20240815055109_init/migration.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
CREATE TABLE "Session" (
33
"id" TEXT NOT NULL PRIMARY KEY,
44
"authSession" TEXT NOT NULL,
5-
"issuer" TEXT NOT NULL,
5+
"providerId" TEXT NOT NULL,
66
"accessToken" TEXT NOT NULL,
77
"tokenType" TEXT NOT NULL,
88
"expiresIn" INTEGER NOT NULL,

auth/prisma/schema.prisma

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ generator client {
1010
model Session {
1111
id String @id @default(uuid())
1212
authSession String
13-
issuer String
13+
providerId String
1414
accessToken String
1515
tokenType String
1616
expiresIn Int //miliseconds

auth/ui/SignOut.tsx

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,25 @@ import { useEffect } from "react";
33
import { useRouter } from "next/navigation";
44

55
export default function SignOut(props: {
6-
provider: string;
76
returnUrl?: string;
8-
idTokenHint?: string;
9-
clientId?: string;
10-
postLogoutRedirectUri?: string;
11-
state?: string;
7+
sessionId?: string;
128
}) {
13-
const {
14-
provider,
15-
returnUrl,
16-
idTokenHint,
17-
clientId,
18-
postLogoutRedirectUri,
19-
state,
20-
} = props;
9+
const { returnUrl, sessionId } = props;
2110
const router = useRouter();
2211

2312
useEffect(() => {
24-
fetch(`https://auth.example.local/api/auth/signout/${provider}`, {
13+
fetch(`https://auth.example.local/api/auth/signout`, {
2514
method: "POST",
2615
body: JSON.stringify({
2716
returnUrl,
28-
idTokenHint,
29-
clientId,
30-
postLogoutRedirectUri,
31-
state,
17+
sessionId,
3218
}),
3319
})
3420
.then((response) => response.json())
3521
.then(({ endSessionUrl }) => {
3622
if (endSessionUrl) router.replace(endSessionUrl);
3723
});
38-
}, [
39-
clientId,
40-
idTokenHint,
41-
postLogoutRedirectUri,
42-
provider,
43-
returnUrl,
44-
router,
45-
state,
46-
]);
24+
}, [returnUrl, router, sessionId]);
4725

4826
return <div>Loading...</div>;
4927
}

0 commit comments

Comments
 (0)