Struct actix_identity::Identity
source · pub struct Identity(/* private fields */);
Expand description
A verified user identity. It can be used as a request extractor.
The lifecycle of a user identity is tied to the lifecycle of the underlying session. If the session is destroyed (e.g. the session expired), the user identity will be forgotten, de-facto forcing a user log out.
Examples
use actix_web::{
get, post, Responder, HttpRequest, HttpMessage, HttpResponse
};
use actix_identity::Identity;
#[get("/")]
async fn index(user: Option<Identity>) -> impl Responder {
if let Some(user) = user {
format!("Welcome! {}", user.id().unwrap())
} else {
"Welcome Anonymous!".to_owned()
}
}
#[post("/login")]
async fn login(request: HttpRequest) -> impl Responder {
Identity::login(&request.extensions(), "User1".into());
HttpResponse::Ok()
}
#[post("/logout")]
async fn logout(user: Identity) -> impl Responder {
user.logout();
HttpResponse::Ok()
}
Extractor Behaviour
What happens if you try to extract an Identity
out of a request that does not have a valid
identity attached? The API will return a 401 UNAUTHORIZED
to the caller.
If you want to customise this behaviour, consider extracting Option<Identity>
or
Result<Identity, actix_web::Error>
instead of a bare Identity
: you will then be fully in
control of the error path.
Examples
use actix_web::{http::header::LOCATION, get, HttpResponse, Responder};
use actix_identity::Identity;
#[get("/")]
async fn index(user: Option<Identity>) -> impl Responder {
if let Some(user) = user {
HttpResponse::Ok().finish()
} else {
// Redirect to login page if unauthenticated
HttpResponse::TemporaryRedirect()
.insert_header((LOCATION, "/login"))
.finish()
}
}
Implementations§
source§impl Identity
impl Identity
sourcepub fn id(&self) -> Result<String, GetIdentityError>
pub fn id(&self) -> Result<String, GetIdentityError>
Return the user id associated to the current session.
Examples
use actix_web::{get, Responder};
use actix_identity::Identity;
#[get("/")]
async fn index(user: Option<Identity>) -> impl Responder {
if let Some(user) = user {
format!("Welcome! {}", user.id().unwrap())
} else {
"Welcome Anonymous!".to_owned()
}
}
sourcepub fn login(ext: &Extensions, id: String) -> Result<Self, LoginError>
pub fn login(ext: &Extensions, id: String) -> Result<Self, LoginError>
Attach a valid user identity to the current session.
This method should be called after you have successfully authenticated the user. After
login
has been called, the user will be able to access all routes that require a valid
Identity
.
Examples
use actix_web::{post, Responder, HttpRequest, HttpMessage, HttpResponse};
use actix_identity::Identity;
#[post("/login")]
async fn login(request: HttpRequest) -> impl Responder {
Identity::login(&request.extensions(), "User1".into());
HttpResponse::Ok()
}
sourcepub fn logout(self)
pub fn logout(self)
Remove the user identity from the current session.
After logout
has been called, the user will no longer be able to access routes that
require a valid Identity
.
The behaviour on logout is determined by IdentityMiddlewareBuilder::logout_behaviour
.
Examples
use actix_web::{post, Responder, HttpResponse};
use actix_identity::Identity;
#[post("/logout")]
async fn logout(user: Identity) -> impl Responder {
user.logout();
HttpResponse::Ok()
}
Trait Implementations§
source§impl FromRequest for Identity
impl FromRequest for Identity
Extractor implementation for Identity
.
Examples
use actix_web::{get, Responder};
use actix_identity::Identity;
#[get("/")]
async fn index(user: Option<Identity>) -> impl Responder {
if let Some(user) = user {
format!("Welcome! {}", user.id().unwrap())
} else {
"Welcome Anonymous!".to_owned()
}
}