-
Hi, I am wondering if it is possible (or even best practice) to get an existing user session without making an additional request to the OAuth provider for a token. For example, the user has authenticated, received a JWT, created a session and accessed an authenticated page. Now, the user wants to visit a different authenticated page with Example: // pages/dashboard.tsx
import Link from 'next/link'
import { useSession } from 'next-auth/react'
const Dashboard = () => {
const { data: session } = useSession()
return (
<>
<h1>Hi {session.name}!</h1>
<Link href="/dashboard/item"><a>Some other page</a></Link>
</>
)
}
export default Dashboard
// pages/dashboard/item.tsx
const DashboardItem = ({ data }) => {
const { data: session } = useSession()
return (
<ul>
{data.map(item => <li key={item.id}>{item.title}</li>)}
</ul>
)
}
export const getServerSideProps = async ctx => {
const session = await getSession(ctx) // this session should already exist in cookies
if (!session) {
// redirect or whatevs
}
const res = await fetch('https://some.fake.api', {
headers: {
Authorization: `Bearer ${session.accessToken}`
}
})
const data = await res.json()
return {
props: { data, session },
}
}
export default DashboardItem Edit: Just trying to figure out how to eliminate the ~100-400ms latency of calling the OAuth provider on page changes. 🤔 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
What's your |
Beta Was this translation helpful? Give feedback.
-
I'm a fool. My |
Beta Was this translation helpful? Give feedback.
I'm a fool. My
jwt
callback was refreshing the token, causing the latency. Without thisgetServerSession(context, authOptions)
works perfectly!