Comments (1)
Had the same problem when taking inspiration from this repo for my own API with actix (for training).
The JWT is embedded in the request. In a single controller that takes request: &actix_web::HttpRequest
as the argument you can extract the authentication header with request.headers().get("Authorization")
, then unpack the authentication header like so :
use crate::{jwt::user_token::UserToken, toolbox::errors::CustomError};
use actix_web::HttpRequest;
pub fn get_uid_from_request(request: &HttpRequest) -> Result<i32, CustomError> {
let authen_header = match request.headers().get("Authorization") {
Some(authen_header) => authen_header,
None => {
return Err(
// the authentication middleware should have checked this already
CustomError::new(400, "Something went very wrong".to_string()),
);
}
};
let authen_str = authen_header.to_str()?;
if !authen_str.starts_with("bearer") && !authen_str.starts_with("Bearer") {
return Err(CustomError::new(
400,
"The authentication header doesn't start with 'bearer'".to_string(),
));
}
let raw_token = authen_str[6..authen_str.len()].trim();
let token = UserToken::decode_token(raw_token.to_string())?;
let uid = token.uid;
Ok(uid)
}
And call this function in a controller. As an example :
// GET HOST/persons
pub async fn find_all(
request: HttpRequest,
pool: web::Data<Pool>,
) -> Result<HttpResponse> {
let uid = get_uid_from_request(&request)?;
let persons = Person::find_all(uid, &pool)?;
Ok(HttpResponse::Ok().json(persons))
}
Feel free to ask questions on my own repo. Happy coding :-)
from actix-web-rest-api-with-jwt.
Related Issues (7)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from actix-web-rest-api-with-jwt.