diff --git a/service/src/account.rs b/service/src/account.rs index 10acfd8..18f5164 100644 --- a/service/src/account.rs +++ b/service/src/account.rs @@ -1,17 +1,24 @@ use chrono::Utc; use domain::dto::account::AuthenticateGooleAccountReq; use domain::entities::account::Account; -use library::{db, token}; use library::resp::response::ResErr::ErrPerm; use library::resp::response::{ResErr, ResOK, ResResult}; use library::social::google::GOOGLE_SOCIAL; +use library::{db, token}; + +use crate::utils::login_cache::{LoginAccount, LOGIN_CACHE}; + +pub async fn authenticate_google( + req: AuthenticateGooleAccountReq, +) -> ResResult> { + let verify_result = GOOGLE_SOCIAL + .verify_id_token(&req.id_token.unwrap()) + .await + .map_err(|err| { + tracing::error!(error = ?err, "校验Google Token失败"); + ErrPerm(None) + })?; -pub async fn authenticate_google(req: AuthenticateGooleAccountReq) -> ResResult> { - let verify_result = GOOGLE_SOCIAL.verify_id_token(&req.id_token.unwrap()).await.map_err(|err| { - tracing::error!(error = ?err, "校验Google Token失败"); - ErrPerm(None) - })?; - let account = Account::find_by_google_id(&verify_result.aud, db!()).await?; let account = match account { None => { @@ -24,7 +31,10 @@ pub async fn authenticate_google(req: AuthenticateGooleAccountReq) -> ResResult< display_name: Some(verify_result.given_name), avatar_url: Some(verify_result.picture), ..Default::default() - }, db!()).await? + }, + db!(), + ) + .await? } Some(account) => { tracing::info!("账户已存在, {:?}", account); @@ -36,8 +46,18 @@ pub async fn authenticate_google(req: AuthenticateGooleAccountReq) -> ResResult< } }; - return Ok(ResOK(Some(( - token::generate_token(&account.id), - token::generate_refresh_token(&account.id), - )))); -} \ No newline at end of file + let token = token::generate_token(&account.id); + let refresh_token = token::generate_refresh_token(&account.id); + + LOGIN_CACHE + .insert( + account.id.to_owned(), + LoginAccount { + account, + token: token.to_owned(), + }, + ) + .await; + + return Ok(ResOK(Some((token, refresh_token)))); +} diff --git a/service/src/sys_account.rs b/service/src/sys_account.rs index a4a4f99..6f0be19 100644 --- a/service/src/sys_account.rs +++ b/service/src/sys_account.rs @@ -1,12 +1,21 @@ use chrono::Utc; -use domain::{dto::account::AuthenticateWithPassword, entities::account::{Account, Role}}; -use library::{db, resp::response::{ResErr, ResOK, ResResult}, token::{generate_refresh_token, generate_token}}; +use domain::{ + dto::account::AuthenticateWithPassword, + entities::account::{Account, Role}, +}; +use library::{ + db, + resp::response::{ResErr, ResOK, ResResult}, + token::{generate_refresh_token, generate_token}, +}; use crate::utils::login_cache::{LoginAccount, LOGIN_CACHE}; - -pub async fn authticate_with_password(req: AuthenticateWithPassword) -> ResResult> { - let account = Account::find_with_password(req.username.unwrap(), req.password.unwrap(), db!()).await?; +pub async fn authticate_with_password( + req: AuthenticateWithPassword, +) -> ResResult> { + let account = + Account::find_with_password(req.username.unwrap(), req.password.unwrap(), db!()).await?; if account.is_none() { tracing::info!("登录用户失败,用户查询为空"); return Err(ResErr::params("用户名或密码错误")); @@ -24,10 +33,15 @@ pub async fn authticate_with_password(req: AuthenticateWithPassword) -> ResResul let token = generate_token(&account.id); let refresh_token = generate_refresh_token(&account.id); - LOGIN_CACHE.insert(account.id.to_owned(), LoginAccount{ - account, - token: token.to_owned(), - }).await; + LOGIN_CACHE + .insert( + account.id.to_owned(), + LoginAccount { + account, + token: token.to_owned(), + }, + ) + .await; Ok(ResOK(Some((token, refresh_token)))) -} \ No newline at end of file +}