Add middleware to check authentication
This commit is contained in:
@ -4,24 +4,24 @@ use actix_web::{Error, FromRequest, HttpMessage, HttpRequest};
|
||||
use futures_util::future::{ready, Ready};
|
||||
use std::fmt::Display;
|
||||
|
||||
pub struct AuthChecker {
|
||||
pub struct AuthExtractor {
|
||||
identity: Option<Identity>,
|
||||
request: HttpRequest,
|
||||
}
|
||||
|
||||
impl AuthChecker {
|
||||
impl AuthExtractor {
|
||||
/// Check whether the user is authenticated or not
|
||||
pub fn is_authenticated(&self) -> bool {
|
||||
self.identity.is_some()
|
||||
}
|
||||
|
||||
/// Authenticate the user
|
||||
pub fn authenticate(&self, username: impl Display) {
|
||||
Identity::login(&self.request.extensions(), username.to_string())
|
||||
pub fn authenticate(&self, id: impl Display) {
|
||||
Identity::login(&self.request.extensions(), id.to_string())
|
||||
.expect("Unable to set authentication!");
|
||||
}
|
||||
|
||||
pub fn user_name(&self) -> Option<String> {
|
||||
pub fn id(&self) -> Option<String> {
|
||||
self.identity.as_ref().map(|i| i.id().unwrap())
|
||||
}
|
||||
|
||||
@ -32,7 +32,7 @@ impl AuthChecker {
|
||||
}
|
||||
}
|
||||
|
||||
impl FromRequest for AuthChecker {
|
||||
impl FromRequest for AuthExtractor {
|
||||
type Error = Error;
|
||||
type Future = Ready<Result<Self, Error>>;
|
||||
|
||||
|
Reference in New Issue
Block a user