mirror of
https://github.com/danbulant/Portfolio
synced 2026-07-07 12:00:43 +00:00
refactor: session entity structure
This commit is contained in:
parent
59e028b0ed
commit
5e36b7b3c8
4 changed files with 12 additions and 15 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
use chrono::Utc;
|
use chrono::{Utc, Duration};
|
||||||
use ::entity::{candidate, session};
|
use ::entity::{candidate, session};
|
||||||
use sea_orm::{*, prelude::Uuid};
|
use sea_orm::{*, prelude::Uuid};
|
||||||
use crate::crypto::hash_password;
|
use crate::crypto::hash_password;
|
||||||
|
|
@ -31,14 +31,13 @@ impl Mutation {
|
||||||
db: &DbConn,
|
db: &DbConn,
|
||||||
user_id: i32,
|
user_id: i32,
|
||||||
random_uuid: Uuid,
|
random_uuid: Uuid,
|
||||||
hashed_jwt: String
|
|
||||||
) -> Result<session::Model, DbErr> {
|
) -> Result<session::Model, DbErr> {
|
||||||
session::ActiveModel {
|
session::ActiveModel {
|
||||||
id: Set(random_uuid),
|
id: Set(random_uuid),
|
||||||
hashed_token: Set(hashed_jwt),
|
|
||||||
user_id: Set(user_id),
|
user_id: Set(user_id),
|
||||||
|
ip_address: Set("127.0.0.1".to_string()),
|
||||||
created_at: Set(Utc::now().naive_local()),
|
created_at: Set(Utc::now().naive_local()),
|
||||||
updated_at: Set(Utc::now().naive_local()),
|
expires_at: Set(Utc::now().naive_local().checked_add_signed(Duration::days(1)).unwrap()),
|
||||||
}
|
}
|
||||||
.insert(db)
|
.insert(db)
|
||||||
.await
|
.await
|
||||||
|
|
|
||||||
|
|
@ -53,9 +53,7 @@ impl CandidateService {
|
||||||
// user is authenticated, generate a session
|
// user is authenticated, generate a session
|
||||||
let random_uuid: Uuid = Uuid::new_v4();
|
let random_uuid: Uuid = Uuid::new_v4();
|
||||||
|
|
||||||
let jwt = generate_candidate_token(candidate);
|
let session = match Mutation::insert_session(db, user_id, random_uuid).await {
|
||||||
|
|
||||||
let session = match Mutation::insert_session(db, user_id, random_uuid, hash_sha256(jwt)).await {
|
|
||||||
Ok(session) => session,
|
Ok(session) => session,
|
||||||
Err(_) => return Err(DB_ERROR)
|
Err(_) => return Err(DB_ERROR)
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -7,10 +7,11 @@ use sea_orm::entity::prelude::*;
|
||||||
pub struct Model {
|
pub struct Model {
|
||||||
#[sea_orm(primary_key, auto_increment = false)]
|
#[sea_orm(primary_key, auto_increment = false)]
|
||||||
pub id: Uuid,
|
pub id: Uuid,
|
||||||
pub hashed_token: String,
|
|
||||||
pub user_id: i32,
|
pub user_id: i32,
|
||||||
|
#[sea_orm(column_type = "Custom(\"inet\".to_owned())")]
|
||||||
|
pub ip_address: String,
|
||||||
pub created_at: DateTime,
|
pub created_at: DateTime,
|
||||||
pub updated_at: DateTime,
|
pub expires_at: DateTime,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||||
|
|
|
||||||
|
|
@ -18,10 +18,9 @@ impl MigrationTrait for Migration {
|
||||||
.unique_key()
|
.unique_key()
|
||||||
.primary_key(),
|
.primary_key(),
|
||||||
)
|
)
|
||||||
.col(ColumnDef::new(Session::HashedToken).string().not_null())
|
|
||||||
.col(ColumnDef::new(Session::UserId).integer().not_null())
|
.col(ColumnDef::new(Session::UserId).integer().not_null())
|
||||||
.col(ColumnDef::new(Session::CreatedAt).date_time().not_null())
|
.col(ColumnDef::new(Session::CreatedAt).date_time().not_null())
|
||||||
.col(ColumnDef::new(Session::UpdatedAt).date_time().not_null())
|
.col(ColumnDef::new(Session::ExpiresAt).date_time().not_null())
|
||||||
.to_owned();
|
.to_owned();
|
||||||
|
|
||||||
manager
|
manager
|
||||||
|
|
@ -36,10 +35,10 @@ impl MigrationTrait for Migration {
|
||||||
.unique_key()
|
.unique_key()
|
||||||
.primary_key(),
|
.primary_key(),
|
||||||
)
|
)
|
||||||
.col(ColumnDef::new(Session::HashedToken).string().not_null())
|
|
||||||
.col(ColumnDef::new(Session::UserId).integer().not_null())
|
.col(ColumnDef::new(Session::UserId).integer().not_null())
|
||||||
|
.col(ColumnDef::new(Session::IpAddress).inet().not_null())
|
||||||
.col(ColumnDef::new(Session::CreatedAt).date_time().not_null())
|
.col(ColumnDef::new(Session::CreatedAt).date_time().not_null())
|
||||||
.col(ColumnDef::new(Session::UpdatedAt).date_time().not_null())
|
.col(ColumnDef::new(Session::ExpiresAt).date_time().not_null())
|
||||||
.to_owned(),
|
.to_owned(),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
|
|
@ -57,8 +56,8 @@ impl MigrationTrait for Migration {
|
||||||
pub enum Session {
|
pub enum Session {
|
||||||
Table,
|
Table,
|
||||||
Id,
|
Id,
|
||||||
HashedToken,
|
|
||||||
UserId,
|
UserId,
|
||||||
|
IpAddress,
|
||||||
CreatedAt,
|
CreatedAt,
|
||||||
UpdatedAt
|
ExpiresAt,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue