修改用户授权、登录接口,返回数据增加用户信息

This commit is contained in:
李运家 2024-06-06 17:18:24 +08:00
parent 3a397dee4e
commit 86bcdfae26
9 changed files with 58 additions and 16 deletions

1
Cargo.lock generated
View File

@ -478,6 +478,7 @@ version = "0.1.0"
dependencies = [
"chrono",
"serde",
"serde_json",
"sqlx",
"tracing",
"tracing-appender",

View File

@ -1,11 +1,11 @@
use axum::Json;
use domain::dto::account::{AuthenticateGooleAccountReq, AuthenticateWithPassword};
use domain::{dto::account::{AuthenticateGooleAccountReq, AuthenticateWithPassword}, vo::account::LoginAccount};
use library::resp::response::{ ResData, ResResult};
use validator::Validate;
pub async fn authenticate_google(
Json(req): Json<AuthenticateGooleAccountReq>
) -> ResResult<ResData<(String, String)>> {
) -> ResResult<ResData<LoginAccount>> {
req.validate()?;
service::account::authenticate_google(req).await
@ -13,7 +13,7 @@ pub async fn authenticate_google(
pub async fn authenticate_with_password(
Json(req): Json<AuthenticateWithPassword>
) -> ResResult<ResData<(String, String)>> {
) -> ResResult<ResData<LoginAccount>> {
req.validate()?;
service::sys_account::authenticate_with_password(req).await

View File

@ -7,6 +7,7 @@ edition = "2021"
[dependencies]
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
sqlx = { workspace = true, features = ["postgres", "uuid", "macros", "sqlx-macros", "chrono", "time", "sqlx-postgres"] }
validator = { workspace = true, features = ["derive"] }
chrono = { workspace = true, features = ["serde"]}

View File

@ -1,3 +1,4 @@
pub mod entities;
pub mod dto;
pub mod vo;
mod db_result;

17
domain/src/vo/account.rs Normal file
View File

@ -0,0 +1,17 @@
use serde::{Deserialize, Serialize};
use sqlx::types::JsonValue;
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct LoginAccount {
pub username: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub display_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub avatar_url: Option<String>,
pub metadata: JsonValue,
pub wallet: JsonValue,
#[serde(skip_serializing_if = "Option::is_none")]
pub email: Option<String>,
pub token: String,
pub refresh_token: String,
}

1
domain/src/vo/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod account;

View File

@ -9,13 +9,13 @@ use moka::{
};
#[derive(Debug, Clone)]
pub struct LoginAccount {
pub struct CacheAccount {
pub account: Account,
pub token: String,
}
lazy_static! {
pub static ref LOGIN_CACHE: Cache<String, LoginAccount> = {
pub static ref LOGIN_CACHE: Cache<String, CacheAccount> = {
CacheBuilder::new(20480)
.name("login_cache")
.eviction_policy(EvictionPolicy::lru())

View File

@ -1,7 +1,8 @@
use chrono::Utc;
use domain::dto::account::AuthenticateGooleAccountReq;
use domain::entities::account::Account;
use library::cache::account_cache::{LoginAccount, LOGIN_CACHE};
use domain::vo::account::LoginAccount;
use library::cache::account_cache::{CacheAccount, LOGIN_CACHE};
use library::resp::response::ResErr::ErrPerm;
use library::resp::response::{ResErr, ResData, ResResult};
use library::social::google::GOOGLE_SOCIAL;
@ -9,7 +10,7 @@ use library::{db, token};
pub async fn authenticate_google(
req: AuthenticateGooleAccountReq,
) -> ResResult<ResData<(String, String)>> {
) -> ResResult<ResData<LoginAccount>> {
let verify_result = GOOGLE_SOCIAL
.verify_id_token(&req.id_token.unwrap())
.await
@ -51,12 +52,22 @@ pub async fn authenticate_google(
LOGIN_CACHE
.insert(
account.id.to_owned(),
LoginAccount {
account,
CacheAccount {
account: account.clone(),
token: token.to_owned(),
},
)
.await;
return Ok(ResData::some((token, refresh_token)));
let login_account = LoginAccount{
username: account.username,
display_name: account.display_name,
avatar_url: account.avatar_url,
metadata: account.metadata,
wallet: account.wallet,
email: account.email,
token,
refresh_token
};
return Ok(ResData::some(login_account));
}

View File

@ -1,16 +1,16 @@
use chrono::Utc;
use domain::{
dto::account::AuthenticateWithPassword,
entities::account::{Account, Role},
entities::account::{Account, Role}, vo::account::LoginAccount,
};
use library::{
cache::account_cache::{LoginAccount, LOGIN_CACHE}, db, resp::response::{ResErr, ResData, ResResult}, token::{generate_refresh_token, generate_token}
cache::account_cache::{CacheAccount, LOGIN_CACHE}, db, resp::response::{ResErr, ResData, ResResult}, token::{generate_refresh_token, generate_token}
};
pub async fn authenticate_with_password(
req: AuthenticateWithPassword,
) -> ResResult<ResData<(String, String)>> {
) -> ResResult<ResData<LoginAccount>> {
let account =
Account::find_with_password(req.username.unwrap(), req.password.unwrap(), db!()).await?;
@ -34,14 +34,24 @@ pub async fn authenticate_with_password(
LOGIN_CACHE
.insert(
account.id.to_owned(),
LoginAccount {
account,
CacheAccount {
account: account.clone(),
token: token.to_owned(),
},
)
.await;
Ok(ResData(Some((token, refresh_token))))
let login_account = LoginAccount{
username: account.username,
display_name: account.display_name,
avatar_url: account.avatar_url,
metadata: account.metadata,
wallet: account.wallet,
email: account.email,
token,
refresh_token
};
Ok(ResData::some(login_account))
}
pub async fn add_account() -> ResResult<ResData<()>> {