Skip to main content

dioxus_auth/
session.rs

1#[derive(Clone, Debug, Eq, Hash, PartialEq)]
2pub struct SessionId(String);
3
4impl SessionId {
5    pub fn new(value: impl Into<String>) -> Self {
6        Self(value.into())
7    }
8
9    pub fn as_str(&self) -> &str {
10        &self.0
11    }
12
13    pub fn into_string(self) -> String {
14        self.0
15    }
16}
17
18#[derive(Clone, Debug, Eq, PartialEq)]
19pub struct Session<UserId> {
20    id: SessionId,
21    user_id: UserId,
22    created_at_unix: u64,
23    expires_at_unix: u64,
24    auth_hash: Option<String>,
25}
26
27impl<UserId> Session<UserId> {
28    pub fn new(id: SessionId, user_id: UserId, created_at_unix: u64, expires_at_unix: u64) -> Self {
29        Self {
30            id,
31            user_id,
32            created_at_unix,
33            expires_at_unix,
34            auth_hash: None,
35        }
36    }
37
38    pub fn with_auth_hash(mut self, auth_hash: impl Into<String>) -> Self {
39        self.auth_hash = Some(auth_hash.into());
40        self
41    }
42
43    pub fn id(&self) -> &SessionId {
44        &self.id
45    }
46
47    pub fn user_id(&self) -> &UserId {
48        &self.user_id
49    }
50
51    pub fn created_at_unix(&self) -> u64 {
52        self.created_at_unix
53    }
54
55    pub fn expires_at_unix(&self) -> u64 {
56        self.expires_at_unix
57    }
58
59    pub fn auth_hash(&self) -> Option<&str> {
60        self.auth_hash.as_deref()
61    }
62
63    pub fn is_expired_at(&self, unix_timestamp: u64) -> bool {
64        unix_timestamp >= self.expires_at_unix
65    }
66}