1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
use std::str::FromStr;
use serde::{Deserialize, Serialize};
use sqlx::FromRow;
use time::OffsetDateTime;
use uuid::Uuid;
use crate::Error;
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize, FromRow)]
#[serde(rename_all = "camelCase")]
pub struct User {
pub uuid: Uuid,
pub name: String,
pub email: String,
#[serde(default, skip_serializing)]
pub password_hash: String,
pub created_at: Option<OffsetDateTime>,
pub updated_at: Option<OffsetDateTime>,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct TokenClaims {
pub sub: Uuid,
pub exp: i64,
}
impl TokenClaims {
pub fn new(sub: Uuid, max_age: time::Duration) -> Self {
Self {
sub,
exp: (time::OffsetDateTime::now_utc() + max_age).unix_timestamp(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RegisterSchema {
pub name: String,
pub email: String,
pub password: String,
}
impl RegisterSchema {
pub fn validate(&self) -> Result<(), Error> {
email_address::EmailAddress::from_str(&self.email)?;
Ok(())
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct LoginSchema {
pub email: String,
pub password: String,
}
impl From<RegisterSchema> for LoginSchema {
fn from(value: RegisterSchema) -> Self {
let RegisterSchema {
email, password, ..
} = value;
Self { email, password }
}
}
|