/
githubmirror
/
next.js
Обзор
Документация
Войти
/
githubmirror
/
next.js
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
canary
examples/with-magic/lib/auth.js
29 строк
859 B
Steven
chore(examples): use default prettier for examples/templates (#60530)
12 янв 2024, 02:01
Не верифицирован
12 янв 2024, 02:01
4466ba4
Код
Авторство
О чём код?
import Iron from "@hapi/iron"; import { MAX_AGE, setTokenCookie, getTokenCookie } from "./auth-cookies"; const TOKEN_SECRET = process.env.TOKEN_SECRET; export async function setLoginSession(res, session) { const createdAt = Date.now(); // Create a session object with a max age that we can validate later const obj = { ...session, createdAt, maxAge: MAX_AGE }; const token = await Iron.seal(obj, TOKEN_SECRET, Iron.defaults); setTokenCookie(res, token); } export async function getLoginSession(req) { const token = getTokenCookie(req); if (!token) return; const session = await Iron.unseal(token, TOKEN_SECRET, Iron.defaults); const expiresAt = session.createdAt + session.maxAge * 1000; // Validate the expiration date of the session if (Date.now() > expiresAt) { throw new Error("Session expired"); } return session; }