Skip to content

Commit e6b9f84

Browse files
committed
logout from keycoak completely when logging out of app
1 parent 61d8749 commit e6b9f84

4 files changed

Lines changed: 105 additions & 105 deletions

File tree

app/[locale]/dashboard/components/main-nav.tsx

Lines changed: 22 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
'use client';
22

3-
import React, { useEffect, useState } from 'react';
3+
import { Session } from 'next-auth';
4+
import { signIn, signOut, useSession } from 'next-auth/react';
45
import Image from 'next/image';
56
import Link from 'next/link';
67
import { usePathname, useRouter } from 'next/navigation';
7-
import { Session } from 'next-auth';
8-
import { signIn, signOut, useSession } from 'next-auth/react';
98
import {
10-
Avatar,
11-
Button,
12-
Dialog,
13-
Divider,
14-
IconButton,
15-
Popover,
16-
SearchInput,
17-
Spinner,
18-
Text,
9+
Avatar,
10+
Button,
11+
Dialog,
12+
Divider,
13+
IconButton,
14+
Popover,
15+
SearchInput,
16+
Spinner,
17+
Text,
1918
} from 'opub-ui';
19+
import React, { useEffect, useState } from 'react';
2020

21+
import { Icons } from '@/components/icons';
2122
import { useDashboardStore } from '@/config/store';
2223
import { GraphQL } from '@/lib/api';
23-
import { Icons } from '@/components/icons';
2424
import { UserDetailsQryDoc } from '../[entityType]/[entitySlug]/schema';
2525
import { allOrganizationsListingDoc } from '../[entityType]/schema';
2626
import Sidebar from './sidebar';
@@ -38,20 +38,9 @@ export function MainNav({ hideSearch = false }) {
3838
const [isOpen, setIsOpen] = useState(false);
3939
const [isExploreOpen, setIsExploreOpen] = useState(false);
4040

41-
const [isLoggingOut, setIsLoggingOut] = React.useState(false);
4241
const { data: session, status } = useSession();
4342
const { setUserDetails, setAllEntityDetails } = useDashboardStore();
4443

45-
async function keycloakSessionLogOut() {
46-
try {
47-
setIsLoggingOut(true);
48-
await fetch(`/api/auth/logout`, { method: 'GET' });
49-
} catch (err) {
50-
setIsLoggingOut(false);
51-
console.error(err);
52-
}
53-
}
54-
5544
const handleSignIn = async () => {
5645
try {
5746
// First attempt sign in
@@ -91,10 +80,6 @@ export function MainNav({ hideSearch = false }) {
9180
fetchData();
9281
}, [session, hasFetched, setUserDetails, setAllEntityDetails]);
9382

94-
if (isLoggingOut) {
95-
return <LogginOutPage />;
96-
}
97-
9883
const exploreLinks = [
9984
{
10085
title: 'Datasets',
@@ -145,7 +130,6 @@ export function MainNav({ hideSearch = false }) {
145130
data={[...exploreLinks, ...Navigation]}
146131
session={session}
147132
status={status}
148-
keycloakSessionLogOut={keycloakSessionLogOut}
149133
signIn={signIn}
150134
/>
151135
</div>
@@ -279,7 +263,6 @@ export function MainNav({ hideSearch = false }) {
279263
{session?.user ? (
280264
<ProfileContent
281265
session={session}
282-
keycloakSessionLogOut={keycloakSessionLogOut}
283266
/>
284267
) : (
285268
<Button
@@ -302,10 +285,8 @@ export function MainNav({ hideSearch = false }) {
302285

303286
export const ProfileContent = ({
304287
session,
305-
keycloakSessionLogOut,
306288
}: {
307289
session: Session;
308-
keycloakSessionLogOut: () => Promise<void>;
309290
}) => {
310291
const [open, setOpen] = React.useState(false);
311292

@@ -362,11 +343,16 @@ export const ProfileContent = ({
362343
<Divider className="mx-3 my-3 w-auto" />
363344
<div className="px-3">
364345
<Button
365-
onClick={() => {
346+
onClick={async () => {
366347
setOpen(false);
367-
keycloakSessionLogOut().then(() =>
368-
signOut({ callbackUrl: '/' })
369-
);
348+
const response = await fetch(`/api/auth/logout`, { method: 'GET' });
349+
const data = await response.json();
350+
351+
await signOut({ redirect: false });
352+
353+
if (data.url) {
354+
window.location.href = data.url;
355+
}
370356
}}
371357
kind="secondary"
372358
size="slim"
@@ -381,11 +367,3 @@ export const ProfileContent = ({
381367
);
382368
};
383369

384-
const LogginOutPage = () => {
385-
return (
386-
<div className=" flex items-center justify-end gap-4 bg-surfaceDefault p-5 lg:p-7">
387-
<Spinner color="surface" />
388-
<Text variant="headingLg">Logging out...</Text>
389-
</div>
390-
);
391-
};

app/[locale]/dashboard/components/sidebar.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
1-
import React, { useState } from 'react';
21
import Link from 'next/link';
32
import { Button, Icon, Sheet, Spinner, Text } from 'opub-ui';
3+
import React, { useState } from 'react';
44

55
import { Icons } from '@/components/icons';
66
import { ProfileContent } from './main-nav';
77

88
interface SidebarProps {
99
data: { href: string; title: string }[];
1010
session: any;
11-
keycloakSessionLogOut: any;
1211
signIn: any;
1312
status: any;
1413
}
1514

1615
const Sidebar: React.FC<SidebarProps> = ({
1716
data,
1817
session,
19-
keycloakSessionLogOut,
2018
signIn,
2119
status,
2220
}) => {
@@ -49,7 +47,6 @@ const Sidebar: React.FC<SidebarProps> = ({
4947
{session?.user ? (
5048
<ProfileContent
5149
session={session}
52-
keycloakSessionLogOut={keycloakSessionLogOut}
5350
/>
5451
) : (
5552
<Button

app/api/auth/logout/route.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
import { env } from '@/env';
22
import { getServerSession } from 'next-auth';
33

4-
import { getIdToken } from '@/lib/sessionTokenAccessor';
54
import { authOptions } from '../[...nextauth]/options';
65

76
export async function GET() {
87
const session = await getServerSession(authOptions);
98

109
if (session) {
11-
const idToken = await getIdToken();
10+
11+
const idToken = session.id_token;
1212

13-
const url = `${env.AUTH_ISSUER}/protocol/openid-connect/logout?id_token_hint=${idToken}&post_logout_redirect_uri=${encodeURIComponent(env.NEXTAUTH_URL)}`;
13+
const logoutUrl = `${env.AUTH_ISSUER}/protocol/openid-connect/logout?id_token_hint=${idToken}&post_logout_redirect_uri=${encodeURIComponent(env.NEXTAUTH_URL)}`;
1414

15-
try {
16-
await fetch(url, { method: 'GET' });
17-
} catch (err) {
18-
console.error(err);
19-
return new Response(null, { status: 500 });
20-
}
15+
return new Response(JSON.stringify({ url: logoutUrl }), {
16+
status: 200,
17+
headers: { 'Content-Type': 'application/json' },
18+
});
2119
}
22-
return new Response(null, { status: 200 });
20+
21+
return new Response(JSON.stringify({ url: env.NEXTAUTH_URL }), {
22+
status: 200,
23+
headers: { 'Content-Type': 'application/json' },
24+
});
2325
}

0 commit comments

Comments
 (0)