safe to get cookies in RootLayout?
Unanswered
Masai Lion posted this in #help-forum
Masai LionOP
Is there any reason why this wouldn't be safe?
export default async function RootLayout({
children,
}: {
children: React.ReactNode
}) {
let me = undefined
const cookie = cookies().get('token')
if(cookie) {
const response = await fetch('http://localhost:8080/user/me', {
method: 'get',
headers: {
'Authorization': cookie.value
}
})
if (response.ok) {
me = await response.json()
}
}
return (
<Container middle>
<Wrapper>
<Navbar user={me} />
<Content>{children}</Content>
</Wrapper>
</Container>
)
}1 Reply
No, that piece of code will be executed only on server side. Remember that using a dynamic function like
But anyways, if your cookie is secure and/or http only you don't have to worry about someone else getting a hold of it.
cookies or headers will opt your route out of static rendering.But anyways, if your cookie is secure and/or http only you don't have to worry about someone else getting a hold of it.