Skip to content

Commit 68e18df

Browse files
committed
feat: refactor authentication logic by creating getSSRLoggedInAccount utility function and updating layout component
1 parent 2aec852 commit 68e18df

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

src/app/layout.tsx

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import WindowWrapper from '../components/Window/WindowWrapper';
1111
import Providers from '../providers/Providers';
1212
import { toUITheme } from '../utils/theme';
1313
import { Modals } from '../components/Modals/Modals';
14-
import { getSSRApiRequestService } from '../factories/apiRequestService';
15-
import { DTOAccount } from 'podverse-helpers';
14+
import { getSSRLoggedInAccount } from '../utils/auth/getSSRLoggedInAccount';
1615

1716
export const metadata = {
1817
title: 'Podverse',
@@ -23,23 +22,7 @@ export default async function RootLayout({ children }: { children: React.ReactNo
2322
const [locale, cookieStore] = await Promise.all([getLocale(), cookies()]);
2423
const cookieTheme = cookieStore.get('theme')?.value;
2524
const theme = toUITheme(cookieTheme);
26-
27-
let jwt;
28-
if (typeof window === "undefined") {
29-
const cookieStore = await cookies();
30-
jwt = cookieStore.get("jwt")?.value;
31-
}
32-
const ssrApiRequestService = getSSRApiRequestService(jwt);
33-
34-
const hello = await ssrApiRequestService.reqAccountGetManyPublic();
35-
console.log(hello);
36-
37-
let ssrLoggedInAccount: DTOAccount | null = null;
38-
try {
39-
ssrLoggedInAccount = await ssrApiRequestService.reqAuthMe();
40-
} catch (error) {
41-
// do nothing
42-
}
25+
const ssrLoggedInAccount = await getSSRLoggedInAccount();
4326

4427
return (
4528
<html lang={locale} data-theme={theme}>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { cookies } from 'next/headers';
2+
import { getSSRApiRequestService } from '../../factories/apiRequestService';
3+
import { DTOAccount } from 'podverse-helpers';
4+
5+
export async function getSSRLoggedInAccount(): Promise<DTOAccount | null> {
6+
const cookieStore = await cookies();
7+
const jwt = cookieStore.get("jwt")?.value;
8+
const ssrApiRequestService = getSSRApiRequestService(jwt);
9+
10+
try {
11+
return await ssrApiRequestService.reqAuthMe();
12+
} catch {
13+
return null;
14+
}
15+
}

0 commit comments

Comments
 (0)