-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
163 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import ApiAuthorizationError from "@reasn/common/src/errors/ApiAuthorizationError"; | ||
import ApiConnectionError from "@reasn/common/src/errors/ApiConnectionError"; | ||
import fetch from "cross-fetch"; | ||
import { getToken } from "@/lib/token"; | ||
|
||
export enum HttpMethod { | ||
GET = "GET", | ||
POST = "POST", | ||
PUT = "PUT", | ||
DELETE = "DELETE", | ||
} | ||
|
||
export type ProblemDetails = { | ||
type?: string; | ||
title: string; | ||
status: number; | ||
details?: string; | ||
instance?: string; | ||
}; | ||
|
||
export const sendRequest = async <T>( | ||
url: string, | ||
httpMethod: HttpMethod, | ||
body: Object = {}, | ||
authRequired: boolean = false, | ||
): Promise<T> => { | ||
try { | ||
let headers: HeadersInit = new Headers(); | ||
if (authRequired) { | ||
const token = getToken(); | ||
if (!token) { | ||
throw new ApiAuthorizationError( | ||
"Unauthorized access. No token found in cookies", | ||
); | ||
} | ||
headers.set("Authorization", `Bearer ${token}`); | ||
} | ||
|
||
const fetchOptions: RequestInit = { | ||
method: httpMethod, | ||
headers, | ||
}; | ||
|
||
if (httpMethod == HttpMethod.POST || httpMethod == HttpMethod.PUT) { | ||
headers.set("Content-Type", "application/json"); | ||
fetchOptions.body = JSON.stringify(body); | ||
} | ||
|
||
const response = await fetch(url, fetchOptions); | ||
if (!response.ok) { | ||
const problemDetails = (await response.json()) as ProblemDetails; | ||
console.error( | ||
`[HTTP ${problemDetails.instance ?? ""} ${response.status}]: ${ | ||
problemDetails.details ?? problemDetails.title | ||
}`, | ||
); | ||
|
||
throw new ApiConnectionError( | ||
response.status, | ||
problemDetails.details ?? problemDetails.title, | ||
); | ||
} | ||
|
||
return (await response.json()) as T; | ||
} catch (error) { | ||
console.error( | ||
`Error while sending request to ${url} with method ${httpMethod}: ${error}`, | ||
); | ||
throw Error; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { jwtDecode, JwtPayload } from "jwt-decode"; | ||
import { UserRole } from "@reasn/common/src/enums/modelsEnums"; | ||
import { getToken } from "@/lib/token"; | ||
|
||
export const SESSION_DEFAULT = { | ||
token: null, | ||
isAuthenticated: () => false, | ||
}; | ||
|
||
type User = { | ||
email: string; | ||
role: UserRole; | ||
}; | ||
|
||
export type Session = { | ||
token: string | null; | ||
user?: User; | ||
isAuthenticated: () => boolean; | ||
}; | ||
|
||
interface ReasnJwtPayload extends JwtPayload { | ||
email?: string; | ||
role?: UserRole; | ||
} | ||
|
||
export const getSession = (): Session => { | ||
const token = getToken(); | ||
if (!token) { | ||
return SESSION_DEFAULT; | ||
} | ||
|
||
try { | ||
const decodedToken = jwtDecode<ReasnJwtPayload>(token); | ||
const isUserValid = | ||
decodedToken.email !== undefined && decodedToken.role !== undefined; | ||
|
||
return { | ||
token: token, | ||
user: isUserValid | ||
? { | ||
email: decodedToken.email as string, | ||
role: decodedToken.role as UserRole, | ||
} | ||
: undefined, | ||
isAuthenticated: () => token !== null && isUserValid, | ||
}; | ||
} catch (error) { | ||
console.error("[ERROR]: Failed to decode JWT token"); | ||
return SESSION_DEFAULT; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { cookies } from "next/headers"; | ||
import { TokenPayload } from "@reasn/common/src/schemas/tokenPayload"; | ||
|
||
const TOKEN_KEY = "REASN_TOKEN"; | ||
|
||
export const setToken = (tokenPayload: TokenPayload): void => { | ||
cookies().set(TOKEN_KEY, tokenPayload.accessToken, { | ||
maxAge: tokenPayload.expiresIn, | ||
secure: process.env.NODE_ENV === "production", | ||
sameSite: "strict", | ||
httpOnly: true, | ||
}); | ||
}; | ||
|
||
export const clearToken = (): void => { | ||
cookies().set(TOKEN_KEY, "", { maxAge: 0 }); | ||
}; | ||
|
||
export const getToken = (): string | null => { | ||
const token = cookies().get(TOKEN_KEY)?.value; | ||
if (!token) return null; | ||
return token; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
type IconProps = { | ||
export type IconProps = { | ||
className?: string; | ||
colors?: string[]; | ||
gradientTransform?: string; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters