Authenticating our requests
Throughout this book, we have been intercepting HTTP requests before they've hit the view in order to inspect the header and extract the token. If the token cannot be verified when we were interacting with to-do item views, we rejected the request and gave an unauthorized response to the user.
It is tempting to start constructing middleware for our Rocket views. However, Rocket provides streamlined mechanisms known as request and data guards. In order to implement a request guard, we are going to define our own JWT and apply it to our get items view.
Implementing a request guard
Follow these steps to implement a request guard:
- We will begin by making a
src/jwt.rs
file and importing therocket
dependencies needed for a JWT struct processes with the following code:use rocket::Outcome; use rocket::http::Status; use rocket::request::{self, Request, FromRequest};
The
FromRequest
struct is needed to enable our JWT struct to be implemented...