I am using the local API server-side in nextjs server action and server components. I can get the payload-token from cookies(). Now I'd like to know how I can get the user from the local payload API, only if the JWT is untampered.
Not sure if it’s a coincidence or if you asked the same question on Reddit. But I answered there if anyone finds this thread
payload.auth({ headers: request.headers })
Thanks, I went to multiple cities and cried wolf!! Rly wanted to get unblocked on a Friday night. Hopefully it helps people that the issue is in two places.
payload.auth() taking the headers is nice!! can trust payload for all the JWT verification, checking for tampering, extracting the claims... nice indeed
good work everyone 🙂
its working great
also there are some utils in payload, you can decrypt the jwt manually
where is that? i thought i had to pnpm add jose and use (await getPayload().secret) as the secret
it seemed like payload wouldve given us something like this so i was suspicious i was duplicating effort
even though my problem is solved it could be useful to verify the jwt manually in other use cases
maybe this, packages/payload/src/auth/. Payload JWT is HS256, you can even write your own utils use jose or jsonwebtoken.
somthing like:
function isValidToken(token: string): boolean {
// Implement your token validation logic here
// This is just a placeholder example
const hash = crypto
.createHash('sha256')
.update(process.env.PAYLOAD_SECRET || '')
.digest('hex')
.slice(0, 32)
try {
const decoded = jwt.verify(token, hash, { algorithms: ['HS256'] })
return true
} catch {
return false
}
}thank you!
Star
Discord
online
Get dedicated engineering support directly from the Payload team.