Skip to content

Commit

Permalink
[#130] Create test_jwt_handler & fix cookies handler on client side
Browse files Browse the repository at this point in the history
  • Loading branch information
wayangalihpratama committed Nov 5, 2024
1 parent df97e58 commit bb32ba9
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 13 deletions.
2 changes: 1 addition & 1 deletion backend/routes/user_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ async def verify_login_code(
"uid": user.id,
"uphone_number": user.phone_number,
},
expires_delta=timedelta(hours=2),
expires_delta=timedelta(days=1),
)
user.login_code = None
session.commit()
Expand Down
41 changes: 41 additions & 0 deletions backend/tests/utils/test_jwt_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from datetime import timedelta
from utils.jwt_handler import create_jwt_token, verify_jwt_token


def test_create_jwt_token_with_timedelta():
res = create_jwt_token(
{
"uid": 1000,
"uphone_number": "+628123456789",
},
expires_delta=timedelta(days=1),
)
assert res is not None


def test_create_jwt_token_without_timedelta():
res = create_jwt_token(
{
"uid": 1000,
"uphone_number": "+628123456789",
},
)
assert res is not None


def test_verify_jwt_token():
token = create_jwt_token(
{
"uid": 1000,
"uphone_number": "+628123456789",
},
expires_delta=timedelta(days=1),
)
assert token is not None

res = verify_jwt_token(token=token)
assert res == {
"uid": 1000,
"uphone_number": "+628123456789",
"exp": res.get("exp"),
}
2 changes: 1 addition & 1 deletion frontend/src/context/AuthContextProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const AuthContextProvider = ({ children }) => {

useEffect(() => {
const updateToken = async () => {
const token = await getCookie("AUTH_TOKEN");
const token = getCookie("AUTH_TOKEN");
if (token) {
api.setToken(token);
dispatch({
Expand Down
27 changes: 16 additions & 11 deletions frontend/src/lib/cookies.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
"use server";

import { cookies } from "next/headers";

export const setCookie = (name, value) => {
cookies().set(name, value);
export const setCookie = (name, value, days = 2) => {
const expires = new Date(
Date.now() + days * 24 * 60 * 60 * 1000
).toUTCString();
document.cookie = `${name}=${value}; expires=${expires}; path=/; SameSite=Strict; Secure;`;
};

export const deleteCookie = (name) => {
cookies().delete(name);
export const getCookie = (name) => {
const cookieArr = document.cookie.split(";");
for (let i = 0; i < cookieArr.length; i++) {
const cookiePair = cookieArr[i].split("=");
if (name === cookiePair[0].trim()) {
return decodeURIComponent(cookiePair[1]);
}
}
return null;
};

export const getCookie = (name) => {
const cookie = cookies().get(name);
return cookie ? cookie.value : null;
export const deleteCookie = (name) => {
document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; SameSite=Strict;`;
};

0 comments on commit bb32ba9

Please sign in to comment.