Skip to content

Commit

Permalink
Update cookie process, add logout
Browse files Browse the repository at this point in the history
  • Loading branch information
FPT-NMTung committed May 10, 2024
1 parent e200d69 commit f17ed64
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 22 deletions.
11 changes: 1 addition & 10 deletions src/apis/GatewayApi.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Cookie from "js-cookie";
import Cookies from "js-cookie";

const ENDPOINT = import.meta.env.VITE_API_URL;

Expand Down Expand Up @@ -49,13 +48,6 @@ const GatewayApi = async (cmd, dataObj) => {
};

const refreshToken = async () => {
if (!Cookie.get('refresh_token')) {
Cookies.remove('access_token', {path: '/', domain: import.meta.env.VITE_DOMAIN_COOKIE});
Cookies.remove('refresh_token', {path: '/', domain: import.meta.env.VITE_DOMAIN_COOKIE});
window.location.href = '/';
return null;
}

const optionsRefresh = {
headers: {
'Content-Type': 'application/json',
Expand All @@ -73,8 +65,7 @@ const refreshToken = async () => {
);

if (!responseRefresh.ok) {
Cookies.remove('access_token', {path: '/', domain: import.meta.env.VITE_DOMAIN_COOKIE});
Cookies.remove('refresh_token', {path: '/', domain: import.meta.env.VITE_DOMAIN_COOKIE});
Cookie.remove('info', {path: '/', domain: import.meta.env.VITE_DOMAIN_COOKIE});
window.location.href = '/';
return null;
}
Expand Down
8 changes: 3 additions & 5 deletions src/components/auth/Auth.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ const Auth = ({children}) => {

useEffect(() => {
// check token in cookies
const token = Cookie.get('access_token', {path: '/', domain: import.meta.env.VITE_DOMAIN_COOKIE});
const refreshToken = Cookie.get('refresh_token', {path: '/', domain: import.meta.env.VITE_DOMAIN_COOKIE});
const info = Cookie.get('info', {path: '/', domain: import.meta.env.VITE_DOMAIN_COOKIE});

if (token === undefined || refreshToken === undefined) {
Cookie.remove('access_token', {path: '/', domain: import.meta.env.VITE_DOMAIN_COOKIE});
Cookie.remove('refresh_token', {path: '/', domain: import.meta.env.VITE_DOMAIN_COOKIE});
if (info === undefined) {
Cookie.remove('info', {path: '/', domain: import.meta.env.VITE_DOMAIN_COOKIE});
navigate('/login');
}

Expand Down
5 changes: 2 additions & 3 deletions src/components/auth/NonAuth.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ const NonAuth = ({children}) => {

useEffect(() => {
// check token in cookies
const token = Cookie.get('access_token', {path: '/', domain: import.meta.env.VITE_DOMAIN_COOKIE});
const refreshToken = Cookie.get('refresh_token', {path: '/', domain: import.meta.env.VITE_DOMAIN_COOKIE});
const info = Cookie.get('info', {path: '/', domain: import.meta.env.VITE_DOMAIN_COOKIE});

if (token !== undefined && refreshToken !== undefined) {
if (info !== undefined) {
navigate('/');
}

Expand Down
2 changes: 0 additions & 2 deletions src/components/login/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ const Login = () => {
}, [animate, isShowPasswordInput, scope]);

const handleLogin = (data) => {
console.log(data)

if (!isShowPasswordInput) {
setIsShowPasswordInput(true);
return;
Expand Down
11 changes: 9 additions & 2 deletions src/components/menuLayout/menuItems/setting/Setting.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@ import {Fragment} from "react";
import {Button} from "@nextui-org/react";
import Cookies from "js-cookie";
import {useNavigate} from "react-router-dom";
import GatewayApi from "../../../../apis/GatewayApi.js";

const Setting = () => {
const navigate = useNavigate();

const handleLogout = () => {
Cookies.remove('access_token', {path: '/', domain: import.meta.env.VITE_DOMAIN_COOKIE});
Cookies.remove('refresh_token', {path: '/', domain: import.meta.env.VITE_DOMAIN_COOKIE});
Cookies.remove('info', {path: '/', domain: import.meta.env.VITE_DOMAIN_COOKIE});

GatewayApi('pkg_user.delete_refresh_token', {})
.then()
.catch(err => {
console.log(err);
})

navigate('/login');
}

Expand Down

0 comments on commit f17ed64

Please sign in to comment.