Session with all null values on user with jwt, how does it work ?
See original GitHub issueYour question I’m using JWT, the object return by useSession is always empty (it exists, but with all null values on the user object ex: { name: null, email: null, picture: null } even tho the debug logs everything correctly.
What are you trying to do I customized the callbacks to return the user, I expected it to this by default, it took me a while to realize how to to it
this is my code
import NextAuth, { InitOptions } from 'next-auth'
import Adapters from 'next-auth/adapters'
import Providers from 'next-auth/providers'
import { prismaClient } from '../../../../graphql/configs/prismaClient'
const options: any = {
providers: [
Providers.Facebook({
clientId: process.env.FACEBOOK_CLIENT_ID,
clientSecret: process.env.FACEBOOK_CLIENT_SECRET,
}),
],
database: process.env.DATABASE_URL,
jwt: {
encryption: true,
// secret: process.env.AUTH_SECRET,
},
session: {
jwt: true,
updateAge: 1000 * 60 * 60 * 24,
},
secret: process.env.AUTH_SECRET,
useSecureCookies: process.env.NODE_ENV === 'production',
adapter: Adapters.Prisma.Adapter({ prisma: prismaClient }),
debug: true,
// these callbacks are what makes the user work as expected, otherwise it returns empty values
callbacks: {
session(session, payload) {
if (payload.account) session.user = payload.account
return session
},
jwt(token, account, user, userInfo) {
if (userInfo) token.account = userInfo
return token
},
},
}
export default (req, res) => {
return NextAuth(req, res, options)
}
// _app.tsx
<Provider session={pageProps.session} options={{ keepAlive: TEN_MINUTES, clientMaxAge: ONE_WEEK }}>
<Component {...pageProps} />
</Provider>
// index.tsx
function Home() {
const { data, loading: queryLoading } = useMyQueryQuery({ ssr: true, fetchPolicy: 'cache-first' })
const [session, sessionLoading] = useSession()
return (
<Layout>
<Head>
<title>Next.js × Nexus Todo App</title>
</Head>
{session && JSON.stringify(session)}
{sessionLoading}
<a href="/api/auth/signin">Sign in</a>
<a href="/api/auth/signout">Sign out</a>
</Layout>
)
}
Feedback
- [-] Found the documentation helpful // meh, kinda, the callbacks doc surely helped, but took me a while to realize I had to use them
- Found documentation but was incomplete
- [] Could not find relevant documentation
- [] Found the example project helpful
- Did not find the example project helpful // it didnt have callbacks, but worked fine appearently
Seems like a great library btw ! 😄, I probably just misunderstood something I suppose
Issue Analytics
- State:
- Created 3 years ago
- Comments:13 (5 by maintainers)
Top Results From Across the Web
JSON Web Token Introduction - jwt.io
Learn about JSON Web Tokens, what are they, how they work, when and why you should use them.
Read more >JWT decode returns null - Stack Overflow
If you are using this, var decoded = jwt.decode(token, {complete: true}); or var payload = jwt.decode(token);. Check if the token is similar to...
Read more >Stop using JSON Web Tokens. Use Cookies & Server ...
JSON Web Tokens are a great stateless way to authenticate your users and remove the load from your servers and APIs, but JWT...
Read more >REST Security with JWT using Java and Spring Security - Toptal
The identity provider verifies the credentials; if all is OK, it retrieves the user data, generates a JWT containing user details and permissions...
Read more >The Ultimate Guide to handling JWTs on frontend clients ...
For the purposes of auth, a JWT is a token that is issued by the server. The token has a JSON payload that...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Yes, hopefully this month.
Hi, @ChristoRibeiro could you please create a PR with your recommended fix so it’s easier to evaluate? Thank you!