Skip to content

Commit ac9e8cf

Browse files
committed
🪲
1 parent 1a67e2e commit ac9e8cf

File tree

4 files changed

+57
-56
lines changed

4 files changed

+57
-56
lines changed

app1/ui/Home.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,8 @@ export default function Home() {
2828
onSelectAccount={(session) => console.log(session)}
2929
session={sessions[0]}
3030
sessions={sessions}
31-
signOut={async (sessionId) => {
32-
await fetch("https://auth.example.local/api/v1/signout", {
33-
method: "post",
34-
credentials: "include",
35-
body: JSON.stringify({ sessionId }),
36-
}).then((response) => response.json());
37-
38-
reloadSessions();
31+
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`;
3933
}}
4034
/>
4135
</div>

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

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,60 +5,66 @@ import { prisma } from "@/lib/prisma";
55
import { NextRequest, NextResponse } from "next/server";
66

77
export async function POST(request: NextRequest) {
8-
const body = (await request.json()) as {
9-
returnUrl?: string;
10-
idTokenHint?: string;
11-
clientId?: string;
12-
postLogoutRedirectUri?: string;
13-
state?: string;
14-
};
15-
const { returnUrl, idTokenHint, clientId, postLogoutRedirectUri, state } =
16-
body;
8+
try {
9+
const body = (await request.json()) as {
10+
returnUrl?: string;
11+
idTokenHint?: string;
12+
clientId?: string;
13+
postLogoutRedirectUri?: string;
14+
state?: string;
15+
};
16+
const { returnUrl, idTokenHint, state } = body;
1717

18-
const wellKnownResponse = await fetch(
19-
`${configuration.portal.issuer}/.well-known/openid-configuration`
20-
);
18+
const wellKnownResponse = await fetch(
19+
`${configuration.portal.issuer}/.well-known/openid-configuration`
20+
);
2121

22-
const wellKnown = (await wellKnownResponse.json()) as {
23-
issuer: string;
24-
authorization_endpoint: string;
25-
token_endpoint: string;
26-
userinfo_endpoint: string;
27-
end_session_endpoint: string;
28-
};
22+
const wellKnown = (await wellKnownResponse.json()) as {
23+
issuer: string;
24+
authorization_endpoint: string;
25+
token_endpoint: string;
26+
userinfo_endpoint: string;
27+
end_session_endpoint: string;
28+
};
2929

30-
if (wellKnownResponse.status !== 200) {
31-
throw { code: wellKnownResponse.status, details: wellKnown };
32-
}
30+
if (wellKnownResponse.status !== 200) {
31+
throw { code: wellKnownResponse.status, details: wellKnown };
32+
}
3333

34-
const params: {
35-
id_token_hint?: string;
36-
client_id?: string;
37-
post_logout_redirect_uri?: string;
38-
state?: string;
39-
} = {
40-
id_token_hint: idTokenHint,
41-
client_id: clientId,
42-
post_logout_redirect_uri: postLogoutRedirectUri,
43-
state: state,
44-
};
34+
const params: {
35+
client_id?: string;
36+
post_logout_redirect_uri?: string;
37+
id_token_hint?: string;
38+
state?: string;
39+
} = {
40+
client_id: configuration.portal.clientId,
41+
post_logout_redirect_uri: configuration.portal.postLogoutRedirectUri,
42+
};
4543

46-
if (idTokenHint) {
47-
await prisma.session.updateMany({
48-
where: {
49-
idToken: idTokenHint,
50-
},
51-
data: {
52-
deletedAt: new Date(),
53-
},
54-
});
55-
}
44+
if (idTokenHint) params.id_token_hint = idTokenHint;
45+
if (state) params.state = state;
5646

57-
const endSessionUrl = `${
58-
wellKnown.end_session_endpoint
59-
}?${new URLSearchParams(params).toString()}`;
47+
if (idTokenHint) {
48+
await prisma.session.updateMany({
49+
where: {
50+
idToken: idTokenHint,
51+
},
52+
data: {
53+
deletedAt: new Date(),
54+
},
55+
});
56+
}
6057

61-
if (returnUrl) setShortLiveCookie(returnUrlCookieName, returnUrl);
58+
const endSessionUrl = `${
59+
wellKnown.end_session_endpoint
60+
}?${new URLSearchParams(params).toString()}`;
6261

63-
return NextResponse.json({ endSessionUrl });
62+
if (returnUrl) setShortLiveCookie(returnUrlCookieName, returnUrl);
63+
64+
return NextResponse.json({ endSessionUrl });
65+
} catch (error: any) {
66+
return NextResponse.json(error.details || { message: error.message }, {
67+
status: error.code,
68+
});
69+
}
6470
}

auth/configuration.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const configuration = {
4141
issuer: "https://system-v1-fpms4l.zitadel.cloud",
4242
clientId: "279716137237868517",
4343
redirectUrl: "https://auth.example.local/api/auth/callback",
44+
postLogoutRedirectUri: "https://auth.example.local/auth/signedout",
4445
},
4546
};
4647

auth/prisma/dev.db

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)