mirror of
https://github.com/vercel/commerce.git
synced 2025-05-17 06:56:59 +00:00
Signup cookie expiration fix and console log cleanup
This commit is contained in:
parent
233070869d
commit
3605947564
@ -2,7 +2,7 @@ import type { SignupEndpoint } from '.'
|
|||||||
|
|
||||||
import { decode, type JwtPayload } from 'jsonwebtoken'
|
import { decode, type JwtPayload } from 'jsonwebtoken'
|
||||||
import { serialize } from 'cookie'
|
import { serialize } from 'cookie'
|
||||||
import { access } from 'fs'
|
import { CommerceAPIError } from '@vercel/commerce/api/utils/errors'
|
||||||
|
|
||||||
const signup: SignupEndpoint['handlers']['signup'] = async ({
|
const signup: SignupEndpoint['handlers']['signup'] = async ({
|
||||||
req,
|
req,
|
||||||
@ -11,7 +11,6 @@ const signup: SignupEndpoint['handlers']['signup'] = async ({
|
|||||||
}) => {
|
}) => {
|
||||||
// Get token
|
// Get token
|
||||||
const token = req.cookies.get(tokenCookie)?.value
|
const token = req.cookies.get(tokenCookie)?.value
|
||||||
let headers: any = {}
|
|
||||||
|
|
||||||
const accessToken = await restBuyerFetch(
|
const accessToken = await restBuyerFetch(
|
||||||
'PUT',
|
'PUT',
|
||||||
@ -32,27 +31,30 @@ const signup: SignupEndpoint['handlers']['signup'] = async ({
|
|||||||
return response.access_token
|
return response.access_token
|
||||||
})
|
})
|
||||||
|
|
||||||
console.log('got access token: ', accessToken)
|
if (!accessToken) {
|
||||||
|
throw new CommerceAPIError('Failed to retrieve access token', {
|
||||||
if (accessToken) {
|
status: 401,
|
||||||
const decodedToken = decode(accessToken) as JwtPayload
|
})
|
||||||
|
|
||||||
console.log('decoded: ', decodedToken)
|
|
||||||
|
|
||||||
return {
|
|
||||||
headers: {
|
|
||||||
'Set-Cookie': serialize(tokenCookie, accessToken, {
|
|
||||||
maxAge: decodedToken.exp,
|
|
||||||
expires: new Date(Date.now() + decodedToken.exp! * 1000),
|
|
||||||
secure: process.env.NODE_ENV === 'production',
|
|
||||||
path: '/',
|
|
||||||
sameSite: 'lax',
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return { data: undefined, headers }
|
const decodedToken = decode(accessToken) as JwtPayload
|
||||||
|
if (!decodedToken || !decodedToken.exp) {
|
||||||
|
throw new CommerceAPIError('Failed to decode access token', {
|
||||||
|
status: 500,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
headers: {
|
||||||
|
'Set-Cookie': serialize(tokenCookie, accessToken, {
|
||||||
|
expires: new Date(decodedToken.exp * 1000),
|
||||||
|
secure: process.env.NODE_ENV === 'production',
|
||||||
|
path: '/',
|
||||||
|
sameSite: 'lax',
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
data: null,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default signup
|
export default signup
|
||||||
|
Loading…
x
Reference in New Issue
Block a user