app: refresh/logout use cases, update login with refresh token
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
use chrono::{DateTime, Utc};
|
||||
use chrono::{DateTime, Duration, Utc};
|
||||
use uuid::Uuid;
|
||||
|
||||
use domain::{errors::DomainError, value_objects::Email};
|
||||
use domain::{errors::DomainError, models::RefreshSession, value_objects::Email};
|
||||
|
||||
use crate::{auth::queries::LoginQuery, context::AppContext};
|
||||
|
||||
pub struct LoginResult {
|
||||
pub token: String,
|
||||
pub refresh_token: String,
|
||||
pub user_id: Uuid,
|
||||
pub email: String,
|
||||
pub expires_at: DateTime<Utc>,
|
||||
@@ -33,8 +34,20 @@ pub async fn execute(ctx: &AppContext, query: LoginQuery) -> Result<LoginResult,
|
||||
|
||||
let generated = ctx.services.auth.generate_token(user.id()).await?;
|
||||
|
||||
let refresh_token = Uuid::new_v4().to_string();
|
||||
let refresh_expires = Utc::now() + Duration::seconds(ctx.config.refresh_ttl_seconds as i64);
|
||||
let session = RefreshSession {
|
||||
id: Uuid::new_v4(),
|
||||
user_id: user.id().clone(),
|
||||
token: refresh_token.clone(),
|
||||
expires_at: refresh_expires,
|
||||
created_at: Utc::now(),
|
||||
};
|
||||
ctx.repos.refresh_session.create(&session).await?;
|
||||
|
||||
Ok(LoginResult {
|
||||
token: generated.token,
|
||||
refresh_token,
|
||||
user_id: user.id().value(),
|
||||
email: user.email().value().to_string(),
|
||||
expires_at: generated.expires_at,
|
||||
|
||||
Reference in New Issue
Block a user