Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

jwt-on-react

JSON Web Token (JWT) is a secure method for transmitting information between parties, consisting of a header, payload, and signature. In React, JWT facilitates stateless authentication by allowing users to log in, store tokens, and attach them to API calls for protected routes. Key security practices include using httpOnly cookies, validating tokens on the server, and implementing token expiration and refresh mechanisms.

Uploaded by

Ahmed Mahjoub
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

jwt-on-react

JSON Web Token (JWT) is a secure method for transmitting information between parties, consisting of a header, payload, and signature. In React, JWT facilitates stateless authentication by allowing users to log in, store tokens, and attach them to API calls for protected routes. Key security practices include using httpOnly cookies, validating tokens on the server, and implementing token expiration and refresh mechanisms.

Uploaded by

Ahmed Mahjoub
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

JSON Web Token

(JWT) in React.
Simplified Security

Vladyslav Demirov
@vladyslav-demirov
What is JWT?
JSON Web Token (JWT): A compact, URL-safe
token used to securely transmit information
between parties.
• Structure:
1. Header: Contains the type of token and
signing algorithm.
2. Payload: Contains claims (user data or
metadata).
3. Signature: Verifies the integrity of the token.

Why Use JWT?


• Stateless authentication.
• No need for server-side session storage.
• Secure and efficient.

Vladyslav Demirov
How JWT Works in React
Authentication Flow:
- User Login:
• User submits credentials (email/password).
• Server validates credentials and returns a
JWT.

- Store Token:
• Save the token in localStorage or httpOnly
cookies.

- Attach Token:
• Include the token in the Authorisation header
for protected API calls.

- Verify & Decode:


• Server verifies the token and grants/denies
access.
• Decode the payload on the client for user-
specific info (e.g., roles).

Vladyslav Demirov
Implementation in React
Login & Store Token:

Vladyslav Demirov
Protect Routes
Create Protected Routes:

Usage:

Vladyslav Demirov
Decode JWT
Decode JWT for User Info:

Common Use Case: Display user name or roles


dynamically.

Vladyslav Demirov
Conclusion
JWT in React allows you to:
• Securely handle user authentication.
• Protect routes easily.
• Decode and use user data dynamically.

Tips for Security:


• Use httpOnly cookies for better security
against XSS attacks.
• Always validate JWT on the server.
• Implement token expiration and handle
refresh tokens.

Vladyslav Demirov
HAPPY
CODING

Vladyslav Demirov
@vladyslav-demirov

You might also like