Check jwt expire date

This commit is contained in:
Takeshi Kimata
2024-05-25 19:50:01 +09:00
parent 25402bd3ba
commit 4092b14f3c
4 changed files with 16 additions and 4 deletions

View File

@@ -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');

View File

@@ -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');

View File

@@ -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 = {

View File

@@ -14,6 +14,7 @@ export type TokenProps = {
export type TokenType = {
access_token: string;
expires_at: number;
user: UserType;
};