pub struct Identity(_);
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

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()
    }
}

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()
}

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

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()
    }
}
The associated error which can be returned.
Future that resolves to a Self. Read more
Create a Self from request parts asynchronously.
Create a Self from request head asynchronously. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more