🕵️ SicherheitslückenCVE-2024-33668 | Zammad up to 6.2.x Upload Cache excessive authentication(17.09.2026 um 02:15 Uhr)
🕵️ SicherheitslückenCVE-2024-33668 | Zammad up to 6.2.x Upload Cache excessive authentication(17.09.2026 um 02:15 Uhr)
🔧 Programmierung 🕛 vor 2 Jahren 3 Min Lesezeit
0

How to verify JWT Token Expiry in React/Next

↗ Quelle (dev.to)
🗣️ Stimme:

Introduction:

JSON Web Tokens (JWT) are a popular method for authentication in web applications. One critical aspect of using JWT is verifying their expiry to maintain application security. In this tutorial, we'll walk through how to verify JWT token expiry in a React/Next.js app, ensuring that our application remains secure.



Prerequisites:

Before we begin, ensure you have the following:




  • Node.js + Express.js and JSON web token for the backend.

  • React and Next.js for the frontend.



Setting up Token Expiry in the Node/Express Backend:

In our Node/Express backend, let's build a simple login controller with expiration time for our token. Here's an example code snippet:




CODE
const login = async (req, res) => {
try {
const { email, password } = req.body;
const user = await User.findOne({ email });
if (!user) {
return res.status(404).send({ message: 'User not found' });
}
const isMatch = await bcrypt.compare(password, user.password);
if (!isMatch) {
return res.status(404).send({ message: 'Incorrect password' });
}
const token = jwt.sign({ _id: user._id }, process.env.SECRET, {
expiresIn: 86400, // expires in 24 hours
});
res.status(200).send({ token, user });
} catch (error) {
res.status(404).send({ message: error.message });
}
};






Verifying Token Expiry in the React/Next.js Frontend:

In our React/Next.js frontend, let's use the jwt-decode library to check token expiry. Here's how you can do it:

Firstly, we need to install the library:



npm install jwt-decode or yarn add jwt-decode



Then, let's import and use it, like this:




CODE
import jwtDecode from 'jwt-decode';

const isTokenExpired = (token) => {
if (!token) return true;
try {
const decodedToken = jwtDecode(token);
const currentTime = Date.now() / 1000;
return decodedToken.exp < currentTime;
} catch (error) {
console.error('Error decoding token:', error);
return true;
}
};






Handling Expired Tokens:

When a token is expired, we need to remove it from local storage and redirect the user to the login page. Like so:




CODE
useEffect(() => {
if (localStorage.getItem('token')) {
const token = localStorage.getItem('token');
if (isTokenExpired(token)) {
localStorage.removeItem('token');
router.push('/login');
} else {
setUser(JSON.parse(localStorage.getItem('user')));
}
} else {
router.push('/login');
}
}, [router]);






Security Considerations:

It's crucial to handle JWT tokens securely. Consider using secure storage mechanisms and validating tokens on the server side to prevent security vulnerabilities.



Conclusion:

By verifying JWT token expiry in our React/Next.js app, we can ensure that the application remains secure against unauthorized access. Implementing token expiry checks is a critical step in maintaining the integrity of the authentication system.



Additional Resources:





Vollständiger Original-Artikel
Den kompletten Beitrag mit allen Details direkt auf dev.to lesen.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
CVE-2020-20212 | MikroTik RouterOS 6.44.5 /nova/bin/console null pointer dereference
2 Quellen
CVE-2017-17537 | MikroTik RouterBOARD 6.39.2/6.40.5 TCP Service 53 input validation (EDB-43200 / ID 860320)
2 Quellen
CVE-2023-27169 | Xpand IT Write-Back Manager 2.3.1 hash predictable salt (EUVD-2023-30949)
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten How to verify JWT Token Expiry in React/Next

Thematisch verwandte Begriffe: verify, Token, Expiry, ReactNext · 6 Treffer

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...