feat: setup auth flow to work in parallel with api key

Signed-off-by: Varun Raj <mailme@varunraj.in>
This commit is contained in:
Varun Raj committed 2024-09-03 08:11:25 +05:30
1 parent 2f19b5908d
commit 5e71e1eeeb
23 files changed
+296 -30

No files matched your search

+5 -13
View File
@@ -7,23 +7,15 @@ const handleErrors = (error: AxiosError | any) => {
if (error.code === "ERR_BAD_RESPONSE") {
return Promise.reject({
error: "Unable to process this request, please try again later",
message: error.code
message: error.code,
});
}
let errorObject = {}
if (error.response) {
const { status } = error.response
if (status === 422) {
errorObject = {
...error.response.data,
message: 'Unable to process this request, please try again later',
}
} else {
errorObject = {
...error.response.data,
}
}
return Promise.reject(errorObject)
return Promise.reject({
...error.response.data,
status: error.response.status,
})
}
errorObject = {
message: 'Network Error. Please check your connection and try again!',
+30
View File
@@ -0,0 +1,30 @@
import type { CookieSerializeOptions } from 'cookie'
import cookie from 'cookie'
import type { IncomingMessage } from 'http'
import type { NextApiRequest } from 'next'
export const getCookie = (
req: NextApiRequest | IncomingMessage,
name: string
) => {
const cookieData = cookie.parse(req.headers.cookie || '')
if (cookieData && cookieData[name]) {
return cookieData[name]
}
return null
}
export const serializeCookie = (
name: string,
value: string,
options: CookieSerializeOptions = {}
) => {
return cookie.serialize(name, String(value), {
httpOnly: true,
maxAge: 60 * 60 * 24 * 7,
sameSite: process.env.NODE_ENV === 'production' ? 'none' : 'lax',
secure: process.env.NODE_ENV === 'production',
path: '/',
...options,
})
}
+16
View File
@@ -0,0 +1,16 @@
import jwt from 'jsonwebtoken'
export const decodeJWT = (token: string, key: string): any => {
try {
const payload = jwt.verify(token, key, {
ignoreExpiration: true
})
return payload
} catch (error) {
return null
}
}
export const encodeJWT = (payload: any, key: string): string => {
return jwt.sign(payload, key)
}