格式化代码
This commit is contained in:
parent
2b008b89c9
commit
8bbf847de0
@ -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<ResOK<(String, String)>> {
|
||||
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<ResOK<(String, String)>> {
|
||||
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),
|
||||
))));
|
||||
}
|
||||
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))));
|
||||
}
|
||||
|
@ -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<ResOK<(String, String)>> {
|
||||
let account = Account::find_with_password(req.username.unwrap(), req.password.unwrap(), db!()).await?;
|
||||
pub async fn authticate_with_password(
|
||||
req: AuthenticateWithPassword,
|
||||
) -> ResResult<ResOK<(String, String)>> {
|
||||
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))))
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user