set token expiration and fix redirect

This commit is contained in:
Guillaume Dorce 2022-09-09 10:20:24 +02:00
parent c2d96e6159
commit 659dc79c69
2 changed files with 5 additions and 7 deletions

View File

@ -1,16 +1,13 @@
import { Link, Navigate } from 'react-router-dom'; import { Link, Navigate } from 'react-router-dom';
import logo from '@assets/images/logo.svg'; import logo from '@assets/images/logo.svg';
import { useQuery, useQueryClient } from '@tanstack/react-query'; 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 ms from 'ms';
const Login = () => { const Login = () => {
const [cookie, setCookie] = useCookies(['token']);
const [email, setEmail] = useState(''); const [email, setEmail] = useState('');
const [password, setPassword] = useState(''); const [password, setPassword] = useState('');
const [cookie, setCookie] = useCookies(['token']);
const queryClient = useQueryClient();
const { refetch } = useQuery( const { refetch } = useQuery(
['login'], ['login'],
@ -27,7 +24,8 @@ const Login = () => {
}, },
{ {
onSuccess: (data) => { onSuccess: (data) => {
setCookie('token', data.token, { path: '/', expires: new Date(Date.now() + ms('1d')) }); setCookie('token', data.token, { path: '/', expires: new Date(data.expiresAt) });
return <Navigate to="/home" />;
}, },
onError: (error) => { onError: (error) => {
console.error(error); console.error(error);
@ -44,7 +42,7 @@ const Login = () => {
return ( return (
<> <>
{cookie.token ? <Navigate to="/home" /> : null} {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" />

0
client/src/types/index.d.ts vendored Normal file
View File