-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefresh.post.ts
39 lines (33 loc) · 970 Bytes
/
refresh.post.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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { createError, defineEventHandler, getHeader, randomString, getTokensFromCookies, setCookie } from "#imports"
export default defineEventHandler(async (event) => {
const cookie = getHeader(event, "cookie")
console.log("COOKIE ON SERVER in refresh", cookie)
if (!cookie) {
throw createError({
statusCode: 401,
statusMessage: "Unauthorized - no refresh token"
})
}
const tokens = getTokensFromCookies(cookie!)
if (!tokens["refresh-token"]) {
throw createError({
statusCode: 401,
statusMessage: "Unauthorized - no refresh token"
})
}
const accessToken = randomString()
console.log("REFRESHING WITH TOKEN", accessToken)
setCookie(event, "access-token", accessToken, {
httpOnly: true,
maxAge: 10,
sameSite: "strict",
secure: true
})
setCookie(event, "refresh-token", `r-${accessToken}`, {
httpOnly: true,
maxAge: 20,
sameSite: "strict",
secure: true
})
return
})