Check jwt expire date
This commit is contained in:
@@ -29,7 +29,9 @@ module.exports = function (sequelize) {
|
||||
const accessToken = jwt.sign({ userId: user.id }, secretKey, {
|
||||
expiresIn: '1h',
|
||||
});
|
||||
res.status(200).json({ access_token: accessToken, user });
|
||||
const expiresAt = Date.now() + 3600 * 1000; // expire date(ms)
|
||||
|
||||
res.status(200).json({ access_token: accessToken, expires_at: expiresAt, user });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).send('Sign up failed');
|
||||
|
||||
@@ -31,9 +31,10 @@ module.exports = function (sequelize) {
|
||||
const accessToken = jwt.sign({ userId: user.id }, secretKey, {
|
||||
expiresIn: '1h',
|
||||
});
|
||||
const expiresAt = Date.now() + 3600 * 1000; // expire date(ms)
|
||||
|
||||
user.password = undefined;
|
||||
res.status(200).json({ access_token: accessToken, user });
|
||||
res.status(200).json({ access_token: accessToken, expires_at: expiresAt, user });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).send('Sign up failed');
|
||||
|
||||
@@ -3,7 +3,6 @@ import { createContext, useState, useEffect } from 'react';
|
||||
import { TokenContextType, TokenType } from '@/types/user';
|
||||
import { TokenProps } from '@/types/user';
|
||||
import { useRouter, usePathname } from '@/src/navigation';
|
||||
|
||||
const LOCAL_STORAGE_KEY = 'testplat-auth-token';
|
||||
const privatePaths = ['/account', '/projects'];
|
||||
|
||||
@@ -34,11 +33,20 @@ const TokenProvider = ({ locale, children }: TokenProps) => {
|
||||
const [hasRestoreFinished, setHasRestoreFinished] = useState(false);
|
||||
const [token, setToken] = useState<TokenType>({
|
||||
access_token: '',
|
||||
expires_at: 0,
|
||||
user: null,
|
||||
});
|
||||
|
||||
const isSignedIn = () => {
|
||||
return token && token.user && token.user.username ? true : false;
|
||||
// check token
|
||||
if (token && token.user && token.user.username) {
|
||||
// check expire date
|
||||
if (Date.now() < token.expires_at) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
const tokenContext = {
|
||||
|
||||
@@ -14,6 +14,7 @@ export type TokenProps = {
|
||||
|
||||
export type TokenType = {
|
||||
access_token: string;
|
||||
expires_at: number;
|
||||
user: UserType;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user