In this blog, we will have a look at how we can authenticate a user using JSON Web Token aka JWT. JWT is one of the best standards to implement an authentication system in your MERN stack app.
JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed. JWTs can be signed using a secret (with the HMAC algorithm) or a public/private key pair using RSA or ECDSA.
➡️ Authorization: This is the most common scenario for using JWT. Once the user is logged in, each subsequent request will include the JWT, allowing the user to access routes, services, and resources that are permitted with that token. Single Sign On is a feature that widely uses JWT nowadays, because of its small overhead and its ability to be easily used across different domains.
➡️ Information Exchange: JSON Web Tokens are a good way of securely transmitting information between parties.
In its compact form, JSON Web Tokens consist of three parts separated by dots (.), which are:
✅ Header
✅ Payload
✅ Signature
Therefore, a JWT typically appears as follows:
Xxxxx.yyyyy.zzzzz
Because JWTs can be signed, for example, using public/private key pairs — you can be sure the senders are who they say they are. Additionally, as the signature is calculated using the header and the payload, you can verify that the content hasn’t been tampered with.
So below I have shared some code snippets with some explanations. Below these code snippets, we will check if the signed-up user doesn’t exist in the database then it will encrypt the password to store in the database and create a user.
After creating the user it will create a JWT token using the ID of the created user and the name of the user. You can use any type of inputted data like email or phone number too. But I will suggest also including the user ID every time to maintain the uniqueness of the token.
jwt.sign( { id: response.user_id, name: response.user_name }, config.get("jwtSecret"), { expiresIn: 86400 }, (err, token) => { if (err) throw err; res.status(200).json({ expiresIn:1d, data: response, token: token, }); } );
So as I mentioned earlier, the JWT token accepts 3 parameters. The first parameter is the “sign” method which takes information that needs to be placed in a payload that gets converted into the JWT token.
The second parameter is the secret key used to create the digest.
The third parameter is the option representation. So in this example, I have set the expiration time of the token in seconds.
const jwt = require("jsonwebtoken"); const config = require("config"); module.exports = function (req, res, next) { const token = req.header("Authorization"); if (!token) { return res.status(401).json({ msg: "Authrization denied", }); } try { const decoded = jwt.verify(token, config.get("jwtSecret")); req.user = decoded; next(); } catch (err) { res.status(401).json({ msg: "invalid token", }); } };
In the above example, I implemented an Express.js middleware, which checks the presence of the token and validates the token that we have passed.
You can refer to the above example as the basic implementation of middleware, which expects JWT to be passed in the HTTP request headers to check whether it has the required data or not.
As a result of the token verification best practices, we are sending 401 status codes (Unauthorized) if the token is invalid or expired and 400 (Bad Request) if the token is missing in the headers.
So as mentioned in the title above, we will cover the full tech stack required to implement this functionality. On the client side, we will create a React App that interacts with the backend we created above.
npx create-react-app blogapp-fe cd blogapp-fe npm start
So if you are new to React.js, the above commands will create a React application named blogapp-fe with a small boiler plate code and start the server.
Related read: A Complete Guide to Build the First React Native App
Once your server starts running create basic authentication pages for both SignUp and SignIn. And on clicking a button, we will call the respective APIs.
Here I have a shared basic Signup component example where I have added a form with some fields (Name, Email, and Password) and made a mock submit to demonstrate authentication from the front end side.
import React, { useState } from 'react'; const SignupComponent = () => { const [name, setName] = useState(''); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const handleSubmit = async (e) => { e.preventDefault(); // Create a user object with the form data const user = { name, email, password }; try { // Make a POST request to the API endpoint const response = await fetch('http://localhost:8080/signup', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(user) }); // Check if the request was successful if (response.ok) { const data = await response.json(); console.log('Signup successful:', data); // Reset form fields setName(''); setEmail(''); setPassword(''); } else { console.log('Signup failed:', response.status); } } catch (error) { console.log('An error occurred:', error); } }; return ( <form onSubmit={handleSubmit}> <label> Name: <input type="text" value={name} onChange={(e) => setName(e.target.value)} /> </label> <br /> <label> Email: <input type="email" value={email} onChange={(e) => setEmail(e.target.value)} /> </label> <br /> <label> Password: <input type="password" value={password} onChange={(e) => setPassword(e.target.value)} /> </label> <br /> <button type="submit">Sign Up</button> </form> ); }; export default SignupComponent;
Post-signup API calls will be made by the above component to our backend, which will return user details along with an ID and a JWT access token that we will use to authenticate users and authenticate API requests which I will cover below in this blog.
You can store the response access token in your Redux state, context, or browser local storage to access it from any component.
Now in the below snippet, I made a dummy get API call which authenticates using the access token which is passed inside the request headers as Authorization. And if the token is valid it will give the response with a 200 status code.
If it’s not valid or not passed then our auth middleware will throw an error of 401 with an Invalid token error message.
Related read: How To Implement Redux Toolkit With React.js
app.get('/data', auth, (req, res) => { const data = { message: 'Data fetched successfully' }; res.json(data); });
Here auth is our middleware which needs to be imported.
const fetchData = async () => { try { const response = await fetch('http://localhost:8080/data', { method: 'GET', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer YOUR_AUTH_TOKEN' // Replace YOUR_AUTH_TOKEN with t} }); if (response.ok) { const data = await response.json(); console.log('Data fetched successfully:', data); } else { console.log('Failed to fetch data:', response.status); } } catch (error) { console.log('An error occurred:', error); } };
In conclusion, we’ve explored the server-side implementation of JWTs, including creating and verifying tokens. We’ve also delved into the client-side, creating a React app that interacts with the backend, handling user sign-up and token-based authentication.
By following the examples and best practices outlined in this blog, you can confidently implement MERN applications with robust authentication and authorization systems. JSON Web Tokens are a valuable tool in your security arsenal, enabling you to protect your application and its data effectively.
Join us for “Your 24/7 Clinical Knowledge Partner – The AI Companion” Webinar on Wednesday, 30th July 2025 at 11:00 AM EDT
Register NowWe worked with Mindbowser on a design sprint, and their team did an awesome job. They really helped us shape the look and feel of our web app and gave us a clean, thoughtful design that our build team could...
The team at Mindbowser was highly professional, patient, and collaborative throughout our engagement. They struck the right balance between offering guidance and taking direction, which made the development process smooth. Although our project wasn’t related to healthcare, we clearly benefited...
Founder, Texas Ranch Security
Mindbowser played a crucial role in helping us bring everything together into a unified, cohesive product. Their commitment to industry-standard coding practices made an enormous difference, allowing developers to seamlessly transition in and out of the project without any confusion....
CEO, MarketsAI
I'm thrilled to be partnering with Mindbowser on our journey with TravelRite. The collaboration has been exceptional, and I’m truly grateful for the dedication and expertise the team has brought to the development process. Their commitment to our mission is...
Founder & CEO, TravelRite
The Mindbowser team's professionalism consistently impressed me. Their commitment to quality shone through in every aspect of the project. They truly went the extra mile, ensuring they understood our needs perfectly and were always willing to invest the time to...
CTO, New Day Therapeutics
I collaborated with Mindbowser for several years on a complex SaaS platform project. They took over a partially completed project and successfully transformed it into a fully functional and robust platform. Throughout the entire process, the quality of their work...
President, E.B. Carlson
Mindbowser and team are professional, talented and very responsive. They got us through a challenging situation with our IOT product successfully. They will be our go to dev team going forward.
Founder, Cascada
Amazing team to work with. Very responsive and very skilled in both front and backend engineering. Looking forward to our next project together.
Co-Founder, Emerge
The team is great to work with. Very professional, on task, and efficient.
Founder, PeriopMD
I can not express enough how pleased we are with the whole team. From the first call and meeting, they took our vision and ran with it. Communication was easy and everyone was flexible to our schedule. I’m excited to...
Founder, Seeke
We had very close go live timeline and Mindbowser team got us live a month before.
CEO, BuyNow WorldWide
Mindbowser brought in a team of skilled developers who were easy to work with and deeply committed to the project. If you're looking for reliable, high-quality development support, I’d absolutely recommend them.
Founder, Teach Reach
Mindbowser built both iOS and Android apps for Mindworks, that have stood the test of time. 5 years later they still function quite beautifully. Their team always met their objectives and I'm very happy with the end result. Thank you!
Founder, Mindworks
Mindbowser has delivered a much better quality product than our previous tech vendors. Our product is stable and passed Well Architected Framework Review from AWS.
CEO, PurpleAnt
I am happy to share that we got USD 10k in cloud credits courtesy of our friends at Mindbowser. Thank you Pravin and Ayush, this means a lot to us.
CTO, Shortlist
Mindbowser is one of the reasons that our app is successful. These guys have been a great team.
Founder & CEO, MangoMirror
Kudos for all your hard work and diligence on the Telehealth platform project. You made it possible.
CEO, ThriveHealth
Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.
CEO, SMILINGMIND
They were a very responsive team! Extremely easy to communicate and work with!
Founder & CEO, TotTech
We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.
Co-Founder, TEAM8s
Mindbowser was very helpful with explaining the development process and started quickly on the project.
Executive Director of Product Development, Innovation Lab
The greatest benefit we got from Mindbowser is the expertise. Their team has developed apps in all different industries with all types of social proofs.
Co-Founder, Vesica
Mindbowser is professional, efficient and thorough.
Consultant, XPRIZE
Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.
Founder, S.T.A.R.S of Wellness
Mindbowser was great; they listened to us a lot and helped us hone in on the actual idea of the app. They had put together fantastic wireframes for us.
Co-Founder, Flat Earth
Mindbowser was incredibly responsive and understood exactly what I needed. They matched me with the perfect team member who not only grasped my vision but executed it flawlessly. The entire experience felt collaborative, efficient, and truly aligned with my goals.
Founder, Child Life On Call
The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!
CEO, SDOH2Health LLC
Mindbowser was easy to work with and hit the ground running, immediately feeling like part of our team.
CEO, Stealth Startup
Mindbowser was an excellent partner in developing my fitness app. They were patient, attentive, & understood my business needs. The end product exceeded my expectations. Thrilled to share it globally.
Owner, Phalanx
Mindbowser's expertise in tech, process & mobile development made them our choice for our app. The team was dedicated to the process & delivered high-quality features on time. They also gave valuable industry advice. Highly recommend them for app development...
Co-Founder, Fox&Fork