Redirect user after successful login
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
pub mod app_config;
|
||||
pub mod user;
|
||||
pub mod service;
|
||||
pub mod entity_manager;
|
||||
pub mod entity_manager;
|
||||
pub mod session_identity;
|
40
src/data/session_identity.rs
Normal file
40
src/data/session_identity.rs
Normal file
@ -0,0 +1,40 @@
|
||||
use std::fmt::Display;
|
||||
|
||||
use actix_identity::Identity;
|
||||
|
||||
use crate::data::user::User;
|
||||
|
||||
pub struct SessionIdentity {
|
||||
pub id: String,
|
||||
pub is_admin: bool,
|
||||
}
|
||||
|
||||
impl SessionIdentity {
|
||||
pub fn from_user(user: &User) -> Self {
|
||||
Self {
|
||||
id: user.uid.clone(),
|
||||
is_admin: user.admin,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn deserialize<D: Display>(input: D) -> Self {
|
||||
let input = input.to_string();
|
||||
let mut iter = input.split('-');
|
||||
Self {
|
||||
id: iter.next().unwrap_or_default().to_string(),
|
||||
is_admin: iter.next().unwrap_or_default() == "true",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn serialize(&self) -> String {
|
||||
format!("{}-{}", self.id, self.is_admin)
|
||||
}
|
||||
|
||||
pub fn is_authenticated(i: &Identity) -> bool {
|
||||
i.identity()
|
||||
.as_ref()
|
||||
.map(Self::deserialize)
|
||||
.map(|s| !s.id.is_empty())
|
||||
.unwrap_or(false)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user