check auth and redirect to login or home
This commit is contained in:
parent
8f0a604042
commit
e3720181f8
|
|
@ -1,4 +1,4 @@
|
||||||
import { BrowserRouter, Navigate, Outlet, Route, Routes } from 'react-router-dom';
|
import { BrowserRouter, useNavigate, Outlet, Route, Routes } from 'react-router-dom';
|
||||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||||
import Login from './routes/login';
|
import Login from './routes/login';
|
||||||
import Home from './routes/home';
|
import Home from './routes/home';
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
import { useCookies } from "react-cookie";
|
||||||
|
import { useNavigate } from "react-router-dom";
|
||||||
|
|
||||||
|
const checkAuth = () => {
|
||||||
|
const [cookie, setCookie] = useCookies(['token']);
|
||||||
|
|
||||||
|
if (cookie.token && cookie.token !== '') {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
export { checkAuth };
|
||||||
|
|
@ -1,14 +1,11 @@
|
||||||
import { useCookies } from "react-cookie";
|
|
||||||
import { Navigate } from "react-router-dom";
|
|
||||||
import AppHeader from "@components/AppHeader";
|
import AppHeader from "@components/AppHeader";
|
||||||
|
import { Navigate } from "react-router-dom";
|
||||||
|
import { checkAuth } from "../controllers/Auth";
|
||||||
|
|
||||||
const Home = () => {
|
const Home = () => {
|
||||||
const [cookie, setCookie] = useCookies(["token"]);
|
if (!checkAuth()) {
|
||||||
|
|
||||||
if (!cookie.token) {
|
|
||||||
return <Navigate to="/login" />;
|
return <Navigate to="/login" />;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<AppHeader />
|
<AppHeader />
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,12 @@ import { useQuery } from '@tanstack/react-query';
|
||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
import { useCookies } from 'react-cookie';
|
import { useCookies } from 'react-cookie';
|
||||||
import type { Token } from '../types';
|
import type { Token } from '../types';
|
||||||
|
import { checkAuth } from '../controllers/Auth';
|
||||||
|
|
||||||
const Login = () => {
|
const Login = () => {
|
||||||
|
if (checkAuth()) {
|
||||||
|
return <Navigate to="/home" />;
|
||||||
|
}
|
||||||
const [cookie, setCookie] = useCookies(['token']);
|
const [cookie, setCookie] = useCookies(['token']);
|
||||||
const [email, setEmail] = useState('');
|
const [email, setEmail] = useState('');
|
||||||
const [password, setPassword] = useState('');
|
const [password, setPassword] = useState('');
|
||||||
|
|
@ -43,7 +47,6 @@ const Login = () => {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{cookie.token && <Navigate to="/home" />}
|
|
||||||
<div className="flex flex-col min-h-full items-center justify-center py-12 px-4 bg-grey-dark sm:px-6 lg:px-8">
|
<div className="flex flex-col min-h-full items-center justify-center py-12 px-4 bg-grey-dark sm:px-6 lg:px-8">
|
||||||
<div>
|
<div>
|
||||||
<img className="mx-auto h-20 pb-2 w-auto" src={logo} alt="Groupomania" />
|
<img className="mx-auto h-20 pb-2 w-auto" src={logo} alt="Groupomania" />
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue