File size: 638 Bytes
6215321
 
8919651
 
6215321
 
 
 
 
 
 
 
 
8919651
 
6215321
8919651
6215321
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { SignJWT } from "jose"

import { secretKey, issuer, audience } from "./config"

// https://jmswrnr.com/blog/protecting-next-js-api-routes-query-parameters

export async function getToken(data: Record<string, any> = {}): Promise<string> {

  const jwtToken = await new SignJWT(data)
   .setProtectedHeader({
    alg: 'HS256'
   }) // algorithm
   .setIssuedAt()
   .setIssuer(issuer) // issuer
   .setAudience(audience) // audience
   .setExpirationTime("1 day") // token expiration time - to prevent hackers from re-using our URLs more than a day
   .sign(secretKey) // secretKey generated from previous step

  return jwtToken
}