Untitled
unknown
typescript
2 years ago
2.1 kB
14
Indexable
import type {Metadata} from 'next'
import Navbar from '@/components/Navbar'
import Providers from "@/app/utils/Providers";
import './globals.scss'
import React from "react";
import {Roboto} from "next/font/google";
import {cookies} from "next/headers";
import {setToken} from "@/lib/utils/setTokens";
const robotoFont = Roboto({subsets: ['latin'], weight: ["300", "400", "500", "700"], variable: '--font-roboto'})
const getMe = async () => {
"use server"
const cookiesStore = cookies();
const access = cookiesStore.get("access")?.value;
const refresh = cookiesStore.get("refresh")?.value;
let accessToken = access;
if (!access && refresh) {
const refreshResult = await fetch(process.env.BACKEND_URL + "/auth/token/refresh/", {
method: "POST",
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ refresh: refresh }),
});
if (refreshResult.ok) {
// TODO: simplify this
const refreshData: {access: string} = await refreshResult.json();
accessToken = refreshData.access;
setToken({token: accessToken, token_type: "access"});
// Update access token in cookies, if needed
} else {
// Handle refresh token failure
return null;
}
}
if (accessToken) {
const result = await fetch(process.env.URL + "/api/auth/me", {
method: "GET",
headers: {
"Authorization": `Bearer ${accessToken}`,
},
});
if (result.ok) {
const data = await result.json();
console.log(data);
return data;
}
}
return null;
}
export const metadata: Metadata = {
title: 'Create Next App',
description: 'Generated by create next app',
}
export default async function RootLayout({
children,
}: {
children: React.ReactNode
}) {
const user = await getMe()
return (
<html lang="en" className={`${robotoFont.variable} font-sans`}>
<body>
<Providers>
<header>
<Navbar user={user}/>
</header>
{children}
</Providers>
</body>
</html>
);
}
Editor is loading...
Leave a Comment