How to Implement Authorization in Your Application Using JWT (2024)

How to Implement Authorization in Your Application Using JWT (3)

If you are building a web application, implementing APIs is a necessary requirement. The data you display to the user needs to be fetched from an API service that exists on a different server.

Sometimes, your website may need to restrict access to some of these APIs to prevent them from getting fetched anywhere else. The restrictions could be anything ranging from the availability of a user to the role of the user. This is referred to as authorization.

In this post, I am going to show you how to implement authorization with a frontend (React) and a backend (Node JS) using JSON Web Token (JWT). We will be implementing three API calls to demonstrate the process.

Authorization

In an application involving API services, security should be taken into consideration. The APIs should not be accessible from anywhere outside the application. Also, you also want to make sure the APIs are only accessible to the necessary entity. For this, you need to implement authorization. Consider the following two scenarios.

If your application involves authenticating users, you want to ensure that certain services can only be accessed by authenticated users. For example, on Amazon, anyone can view a list of available products, but a particular user’s orders, purchases, payment information, etc. should only be accessible to the authenticated user.

Another use case is where a service is accessible only to a certain user. On a school website, a student can view his/her grades but only a teacher is authorized to modify them.

When a user is authenticated, the authentication service sends an access token back to the application. The access token contains information about the user, his/her role, the time of login, and its expiration time along with other details.

While making an API request, an authorization header containing the access token is added. This token is decoded in the backend and information about the user is retrieved. Depending on the information, the API decides whether the user is authorized to get the response.

We are going to implement a similar scenario. I’ll cover authentication in a later post. Here, we’ll skip straight to the end of authentication i.e. obtaining the access token, and then use the same token to demonstrate three API calls.

We’ll be using JWT in this post.

What is JWT?

JWT is a secure way of sending information in an encoded form. The token contains three parts:

  • Header: This part contains the token’s type and the algorithm used to encode the token.
  • Payload: This part contains information about the user and other information such as token expiry.
  • Signature: This part is used to sign the token and verify that the message was not changed when transferred.

A JWT looks like this with all the parts in encoded form:
[Header].[Payload].[Signature]

An example of JWT:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c

Visit here to decode a JWT. Watch this video to learn more about the implementation of JWT in Node.js.

Now that you know a little bit about Authorization and JWT, let’s start the implementation.

React App

Create a folder authorization-frontend in your project directory. Inside it, run the following command to create the React app.

create-react-app authorization-frontend

We’ll use Bootstrap in this project, so add this CDN to index.html file.

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

Alternatively, you can download the bootstrap source files from here.

We’ll use the Axios library to make API calls. Install it using the command npm i axios.

Node.js Server

First, install Node.js if you don’t have it in your system. Download it from here.

Create a separate folder named api-server for server code. In that folder, run the command npm init and input values for the given prompts. It should create a package.json file in your folder. The file shows the modules you have installed in your project directory.

How to Implement Authorization in Your Application Using JWT (4)

We are using the following modules:

  • express: Node.js framework that makes it easy to build servers
  • cors: Allows cross-origin sources to make API requests. Learn more.
  • jsonwebtoken: To generate an access token

Install them with the command npm i express cors jsonwebtoken.

Now, create a file server.js that contains the necessary code to set up a node server. Include the following code:

const express = require('express')
const cors = require('cors')
const jwt = require('jsonwebtoken')
const app = express()// For sending JSON data through POST requests
app.use(express.json());
app.use(cors({
origin: '*'
}))

// Define APIs here
const PORT = 8000app.listen(PORT, () => {
console.log('Listening on port', PORT);
})

For this project, allow all sources under CORS i.e. any browser can access these APIs. But this is not a good practice while developing an actual application.

Now that you have set up both applications, it’s time to generate the access token in the backend and pass it to the frontend i.e. implement a dummy authentication.

We’ll generate two types of access tokens: one for a normal user and another for an admin user.

Endpoint for fetching the token

Create an API endpoint for fetching the token. Use a POST API since we are getting a request body from the frontend that normally contains the user’s credentials during authentication. In our case, the frontend only sends a value indicating the type of token to fetch.

app.post('/access_token', (req, res) => {
const { value } = req.body;
switch(value) {
case 1:
// generate normal token
case 2:
// generate admin token
default:
// Send error message
}
})

Generate an access token

Let’s have two user objects, a normal and an admin user. Also, define a secret which will be used to sign the token.

const NORMAL_USER = {
name: 'kunal',
admin: false
}
const ADMIN_USER = {
name: 'alex',
admin: true
}
const SECRET = 'client-secret'

Note: Defining a secret inside your application code is not a good practice, you should have it in an env file.

The token can be generated by using the sign() function.

jwt.sign(NORMAL_USER, SECRET, (err, token) => { res.json({ accessToken: token })})

For admin token, replace NORMAL_USER with ADMIN_USER

Now, let’s check by decoding the token. The iat field represents the time at which the token was issued.

How to Implement Authorization in Your Application Using JWT (5)

Fetch the token

On the frontend side, import the Axios library and initialize the hostname of the backend server.

import axios from 'axios'const HOST_NAME = 'http://localhost:8000'

Then, create two buttons for fetching a normal token and an admin token respectively.

How to Implement Authorization in Your Application Using JWT (6)

On clicking the buttons, the handleGetTokenClick() method is called that fetches the access token with an option parameter to determine which token to fetch.

function handleGetTokenClick(option) { }

Inside this method, make a post request to the API to the endpoint /access_token.

axios.post(HOST_NAME+'/access_token', { value: option }) .then(res => { --- HANDLE THE RESPONSE ---})

The response looks like this.

How to Implement Authorization in Your Application Using JWT (7)

Store the token as state

Get the access token from the response and store it in session storage (or local storage; read about the difference here).

Create a state variable to hold the access token.

const [accessToken, setAccessToken] = 
useState(sessionStorage.getItem('accessToken'));

Also, set the state when you receive the access token from the API. Following is the code for handling the response.

const { accessToken } = res.data;
sessionStorage.setItem('accessToken', accessToken);
setAccessToken(accessToken);

Now, it’s time to implement API calls. As mentioned before, we are going to make three GET API calls:

  • Public API: accessible to anyone.
  • Private API: accessible only to the authenticated user (i.e. with the access token)
  • Restricted API: accessible only to the admin user.

Implement the three APIs

Public API

This API sends a simple response when called.

app.get('/public_api', (req, res) => { res.send('Public API called')})

Private API

This API expects an Authorization Header with a valid access token.

app.get('/private_api', (req, res) => { ... })

First, check if the user has sent an authorization header. If not, then send an error message with a 401 status code. This indicates a lack of credentials or invalid credentials.

const auth_header = req.headers.authorization;
if(!auth_header) res.send(401, 'Unauthorized request')

If the request has an authorization header, get the access token from it. Since the authorization header is of the form Bearer access_token, use the split() function to get the access token.

const accessToken = auth_header.split(' ')[1]

Now, use the verify() function to verify the token. It takes the token, the secret, and a callback function as parameters. If the token is valid, the callback function is called with the decoded payload else it is called with an error.

jwt.verify(accessToken, SECRET, (err, payload) => {
if (err) res.send(401, 'Unauthorized request')
res.send('Private API called')
})

Test the API using Postman.

How to Implement Authorization in Your Application Using JWT (8)

Restricted API

This API can only be called by an admin user. The logic is almost similar to the private API except we also check if the user is an admin.

app.get('/restricted_api', (req, res) => {
const auth_header = req.headers.authorization;
if(!auth_header) res.send(401, 'Unauthorized request')

const accessToken = auth_header.split(' ')[1]

jwt.verify(accessToken, SECRET, (err, user) => {
if (err) res.send(401, 'Unauthorized request')
if (user.admin == true) res.send('Restricted API called')
res.send(401, 'Unauthorized request')
})
})

Now, if a normal user makes a request to this API, it sends a 401 error.

Create state to store the response

On the frontend side, create three state variables to store the response from each API.

const [publicResponse, setPublicResponse] = useState('')
const [privateResponse, setPrivateResponse] = useState('')
const [restrictedResponse, setRestrictedResponse] = useState('')

Create buttons to call APIs

How to Implement Authorization in Your Application Using JWT (9)

On clicking each button, the handleAPIButtonClick() method is called that takes also takes an option parameter to decide which API to call.

Using switch-case statements call each API based on the option passed.

Call the APIs

Call the public API.

axios.get(HOST_NAME + '/public_api').then(res => { setPublicResponse(res.data)})

While calling the private API, pass the authorization header in the form Bearer access_token. Read more about Bearer tokens here.

Also, implement the catch block to handle the error response.

axios.get(HOST_NAME + '/private_api', {
headers: {
Authorization: 'Bearer ' + accessToken
}
}).then(res => {
setPrivateResponse(res.data)
}).catch(err => {
setPrivateResponse(err.response.data)
})

Call the restricted API in a similar way, but replace the endpoint and the state update function.

How to Implement Authorization in Your Application Using JWT (10)
How to Implement Authorization in Your Application Using JWT (11)

Consider the above APIs as a template for your own implementation. If you have a page that displays user info, implement the private API. Information that can only be visible to certain users comes under restricted API.

You can add any type of restrictions to your APIs. All you have to do is pass the access token, decode it and check if the user satisfies those restrictions.

I have not added the HTML and CSS code in this post as I only wanted to focus on the logical part. You can find the implementation on GitHub.

Comment down below if you find anything incorrect or know a better implementation.

Authorization helps make your APIs secure and restricted. The use of JWT makes it easy to implement authorization. In this post, I have shown you how to generate an access token and use it to access different types of APIs. This post is a simple demonstration of how authorization can be implemented in your application.

I have explained each and every step in simple words to help you understand authorization. I hope this helps in your future projects.

If you are unable to understand the content or find the explanation unsatisfactory, comment your thoughts below. New ideas are always appreciated! Give a few claps if you liked this post. Subscribe and follow me for weekly content. Reach out to me on Twitter if you want to discuss anything. Till then, Goodbye!

Thanks for being a part of our community! More content in the Level Up Coding publication.
Follow: Twitter, LinkedIn, Newsletter
Level Up is transforming tech recruiting ➡️ Join our talent collective

How to Implement Authorization in Your Application Using JWT (2024)

FAQs

How to implement authorization using JWT? ›

To authenticate a user, a client application must send a JSON Web Token (JWT) in the authorization header of the HTTP request to your backend API. API Gateway validates the token on behalf of your API, so you don't have to add any code in your API to process the authentication.

Should I use JWT for authorization? ›

JWTs are well-suited for server-to-server or microservice-to-microservice communication scenarios within a backend architecture. In this context, JWTs serve as a means of securely transmitting information between services for authorization and authentication purposes.

How can you implement authentication with a JWT describe how it works at a high level? ›

Here is how JWT can be used in an authentication flow: A user provides their credentials (e.g., username and password) and sends them to the server. The server validates the credentials. If they are correct, the server generates a JWT containing the user's information (in a claim) and signs it with a secret key.

How to implement authorisation? ›

Authorization can be implemented using various mechanisms, such as access control lists, roles, permissions, or policies, that define the rules and constraints for each user or group.

How to use JWT authentication with REST API? ›

Procedure
  1. Make sure that the JWT authentication is enabled for REST APIs by setting the value of servlet. jwt. auth. ...
  2. The incoming HTTP request for REST API call must contain the request header “Authorization” with scheme “Bearer” followed by JWT. The signature of the token and expiration date is verified by the system.

Why use JWT instead of Basic Auth? ›

JWT is preferred over any other authentication method because of certain distinct benefits it presents. Developers opt for JWT as these tokens are self-contained and don't ask for any effort to collect info about the user.

What are the disadvantages of JWT authentication? ›

Disadvantages of JWT Authentication:

Token Size: JWTs can become large if they carry extensive user data, leading to increased network traffic. You should strike a balance between token size and necessary information. Limited Token Expiry Control: Once issued, JWTs remain valid until they expire.

Why is JWT better than API key? ›

Typically, the API key provides only application-level security, giving every user the same access; whereas the JWT token provides user-level access. A JWT token can contain information like its expiration date and a user identifier to determine the rights of the user across the entire ecosystem.

How does JWT token authorization work? ›

JWT authorization uses a JWT to represent the user's identity and access rights. The JWT is usually generated by the authentication server after the user logs in and contains the user's identity and access rights. The JWT is then sent with every API request as a bearer token in the authorization header.

How to implement JWT securely? ›

Best Ways to Securely Implement JWTs
  1. Use strong algorithms like HMAC-SHA256 or RSA to sign and encrypt your tokens. ...
  2. Set an expiration time for the JWT to limit its validity period. ...
  3. Set refresh token features to extend the session duration, which allows users to fetch new JWT tokens for an extended period of time.

What is the difference between OAuth and JWT? ›

Here are some differences between OAuth and JWT: Main function: OAuth is used for authorization, while JWT is used for authentication and exchanging information. Security: OAuth is a secure way to manage authorization flows, while JWT is a lightweight and self-contained token.

What is authorization and how can it be implemented? ›

Authorization is a process by which a server determines if the client has permission to use a resource or access a file. Authorization is usually coupled with authentication so that the server has some concept of who the client is that is requesting access.

What is authorization code flow with JWT? ›

During the authorization code flow, the client application takes the parameters they would like to send to the /authorize or /oauth/par endpoints and wraps them in a JSON Web Token (JWT), which they then sign using the private key. The authorization server verifies the signature with your application's public key.

What are the steps of authorization? ›

Authorization involves the following phases: defining a security policy (set of rules), selecting an access control model to encapsulate the defined policy, implementing the model and enforcing the access rules. Each phase requires specific tools to be deployed.

Is authorization code a JWT? ›

The JWT as an authorization grant is the same concept as an authorization code in the authorization code flow (RFC 6749 Section 4.1). An access token requester can obtain an access token by presenting a JWT at the token endpoint (RFC 6749 Section 3.2).

How to implement JWT authentication in MVC? ›

JWT Key you can use any random number strings that you want to use for encryption. Now on the login controller or the controller where you want to create a JWT token for login. Here you can notice I have used Authentication. GenerateJwtToken which i will create in the next step.

How to pass a JWT token in a request? ›

This can be done easily. We have to add an authorization header in our request and this will be a Bearer TOKEN. To avoid any manual copy-pasting of JWT token, we can use variables to add a script in the Tests tab of API request which is generating token.

Top Articles
The Key to Success: Strategic Training & Development | Acorn
TSP G Fund, inflation and TIPS
Canya 7 Drawer Dresser
Minooka Channahon Patch
Chalupp's Pizza Taos Menu
Mcoc Immunity Chart July 2022
CKS is only available in the UK | NICE
Kris Carolla Obituary
Learn How to Use X (formerly Twitter) in 15 Minutes or Less
Mlifeinsider Okta
Elle Daily Horoscope Virgo
Tokioof
Watch TV shows online - JustWatch
104 Whiley Road Lancaster Ohio
fort smith farm & garden - craigslist
Chastity Brainwash
Craigslist Clinton Ar
Lakers Game Summary
Sandals Travel Agent Login
Elbert County Swap Shop
Hannah Palmer Listal
Piedmont Healthstream Sign In
At 25 Years, Understanding The Longevity Of Craigslist
Beaufort 72 Hour
Shelby Star Jail Log
O'reilly's In Mathis Texas
Free T33N Leaks
100 Gorgeous Princess Names: With Inspiring Meanings
Kqelwaob
Florence Y'alls Standings
Does Circle K Sell Elf Bars
Salons Open Near Me Today
Beaver Saddle Ark
Litter-Robot 3 Pinch Contact & DFI Kit
What Are Digital Kitchens & How Can They Work for Foodservice
Nobodyhome.tv Reddit
Albertville Memorial Funeral Home Obituaries
The Best Restaurants in Dublin - The MICHELIN Guide
Kelley Blue Book Recalls
Www Usps Com Passport Scheduler
Worcester County Circuit Court
Carroll White Remc Outage Map
Achieving and Maintaining 10% Body Fat
Www.craigslist.com Waco
Cocorahs South Dakota
Rs3 Nature Spirit Quick Guide
Swsnj Warehousing Inc
American Bully Puppies for Sale | Lancaster Puppies
Unblocked Games 6X Snow Rider
F9 2385
Service Changes and Self-Service Options
Vt Craiglist
Latest Posts
Article information

Author: Laurine Ryan

Last Updated:

Views: 5515

Rating: 4.7 / 5 (77 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Laurine Ryan

Birthday: 1994-12-23

Address: Suite 751 871 Lissette Throughway, West Kittie, NH 41603

Phone: +2366831109631

Job: Sales Producer

Hobby: Creative writing, Motor sports, Do it yourself, Skateboarding, Coffee roasting, Calligraphy, Stand-up comedy

Introduction: My name is Laurine Ryan, I am a adorable, fair, graceful, spotless, gorgeous, homely, cooperative person who loves writing and wants to share my knowledge and understanding with you.