Middleware Creation for REST API

Learn how to add middleware to the REST API application.

We'll cover the following

Add middleware

Middleware is a component that acts as an interceptor for the request before another request has proceeded. For example, the middleware is added to create a new item request. In this example, the middleware acts as authentication to ensure the authenticated user can create a new item request.

Inside the auth.go file in the utils directory, we add some helpers for authentication purposes.

Before the GenerateNewAccessToken() function, we add a struct for storing the JWT token’s metadata.

type TokenMetadata struct {
	Expires int64
}

After the GenerateNewAccessToken() function, we create a helper function called ExtractTokenMetadata.

Get hands-on with 1200+ tech skills courses.