Added session creating
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
use chrono::{DateTime, Duration, Utc};
|
||||
|
||||
use crate::auth::generate_session_token;
|
||||
|
||||
pub struct Session {
|
||||
pub session_id: uuid::Uuid,
|
||||
pub user_id: uuid::Uuid,
|
||||
pub token: String,
|
||||
pub expires_at: DateTime<Utc>,
|
||||
pub created_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
impl Session {
|
||||
pub fn new(user_id: uuid::Uuid) -> Self {
|
||||
let session_id = uuid::Uuid::new_v4();
|
||||
let token = generate_session_token();
|
||||
|
||||
let created_at = Utc::now();
|
||||
let expires_at = Utc::now() + Duration::days(7);
|
||||
|
||||
return Self {
|
||||
session_id,
|
||||
user_id,
|
||||
token,
|
||||
expires_at,
|
||||
created_at,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
use crate::database::model::Session as DbSession;
|
||||
impl From<DbSession> for Session {
|
||||
fn from(db_session: DbSession) -> Self {
|
||||
Self {
|
||||
session_id: db_session.session_id,
|
||||
user_id: db_session.user_id,
|
||||
token: db_session.token,
|
||||
expires_at: db_session.expires_at,
|
||||
created_at: db_session.created_at,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Session> for DbSession {
|
||||
fn from(session: Session) -> Self {
|
||||
Self {
|
||||
session_id: session.session_id,
|
||||
user_id: session.user_id,
|
||||
token: session.token,
|
||||
expires_at: session.expires_at,
|
||||
created_at: session.created_at,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user