-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnonce.ts
24 lines (20 loc) · 807 Bytes
/
nonce.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { withSessionRoute } from "@/utils/withSession"
import { getRandomValues, hexToBigInt, toHexString } from "@pcd/util"
import { NextApiRequest, NextApiResponse } from "next"
/**
* The nonce is a value used in the authentication mechanism and as a
* watermark in the EdDSA ticket. Its value is generated randomly and saved
* in the current session.
*/
export default withSessionRoute(async function (req: NextApiRequest, res: NextApiResponse) {
try {
req.session.nonce = hexToBigInt(toHexString(getRandomValues(30))).toString()
await req.session.save()
res.status(200).send({
nonce: req.session.nonce
})
} catch (error: any) {
console.error(`[ERROR] ${error}`)
res.status(500).send(`Unknown error: ${error.message}`)
}
})