rsnext/examples/with-passport-and-next-connect/lib/auth.js
Luis Alvarez D b540054388
Update authentication examples (#19330)
* Updated example readme

* Updated with-passport example

* Updated profile page for with-passport

* Updated with-passport-and-next-connect

* Updated with-magic

* Updated with-magic readme

* Updated with-iron-session

* Updated next version in with-iron-session

Co-authored-by: Lee Robinson <me@leerob.io>
2020-12-29 12:43:47 -05:00

21 lines
587 B
JavaScript

import Iron from '@hapi/iron'
export async function createLoginSession(session, secret) {
const createdAt = Date.now()
const obj = { ...session, createdAt }
const token = await Iron.seal(obj, secret, Iron.defaults)
return token
}
export async function getLoginSession(token, secret) {
const session = await Iron.unseal(token, secret, Iron.defaults)
const expiresAt = session.createdAt + session.maxAge * 1000
// Validate the expiration date of the session
if (session.maxAge && Date.now() > expiresAt) {
throw new Error('Session expired')
}
return session
}