Skip to content

Commit e8c5d78

Browse files
committed
improvements
1 parent 5d6272a commit e8c5d78

15 files changed

Lines changed: 176 additions & 14 deletions

File tree

Cargo.lock

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ tonic = { version = "0.14", optional = true }
3333
jsonwebtoken = { version = "10.2.0", features = ["aws_lc_rs"], optional = true }
3434
chrono = { version = "0.4.42", features = [], optional = true }
3535
prost = { version = "0.14", optional = true }
36+
prost-types = { version = "0.14", optional = true }
3637
tonic-prost = { version = "0.14", optional = true }
3738
tracing = { version = "0.1", optional = true }
3839
gloo-storage = "0.3.0"
@@ -44,7 +45,11 @@ surrealdb = { version = "3.0.0", features = [
4445
"kv-rocksdb",
4546
"kv-mem",
4647
], optional = true }
47-
web-sys = { version = "0.3.77", features = ["Window", "Storage"], optional = true }
48+
web-sys = { version = "0.3.77", features = [
49+
"Window",
50+
"Storage",
51+
], optional = true }
52+
pbjson-types = { version = "0.9.0", optional = false }
4853

4954

5055
[build-dependencies]
@@ -53,13 +58,19 @@ tonic-prost-build = "0.14"
5358

5459
[features]
5560
default = ["ssr"]
56-
hydrate = ["leptos/hydrate", "dep:console_error_panic_hook", "dep:wasm-bindgen", "dep:web-sys"]
61+
hydrate = [
62+
"leptos/hydrate",
63+
"dep:console_error_panic_hook",
64+
"dep:wasm-bindgen",
65+
"dep:web-sys",
66+
]
5767
ssr = [
5868
"dep:axum",
5969
"dep:tokio",
6070
"dep:leptos_axum",
6171
"dep:tonic",
6272
"dep:prost",
73+
"dep:prost-types",
6374
"dep:tonic-prost",
6475
"dep:tracing",
6576
"dep:tracing-subscriber",
@@ -70,7 +81,7 @@ ssr = [
7081
"leptos_router/ssr",
7182
"dep:surrealdb",
7283
"dep:jsonwebtoken",
73-
"dep:chrono"
84+
"dep:chrono",
7485
]
7586

7687
# Defines a size-optimized profile for the WASM bundle in release mode

build.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
22

33
let proto_root = "./proto";
44
let proto_files = &[
5+
"admin_user_message.proto",
56
"helloworld.proto",
67
"misc.proto",
78
"auth_user.proto",
@@ -18,6 +19,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
1819
let proto_out_dir = "src/infra/grpc";
1920

2021
tonic_prost_build::configure()
22+
.type_attribute(".", "#[derive(serde::Serialize)]")
23+
.extern_path(".google.protobuf.Timestamp", "::pbjson_types::Timestamp")
2124
.out_dir(proto_out_dir)
2225
.build_server(true)
2326
.compile_protos(proto_files, &[proto_root])?;

proto/admin_user_message.proto

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
syntax = "proto3";
2+
package admin_user_message;
3+
4+
5+
import "google/protobuf/timestamp.proto";
6+
7+
8+
9+
message AdminUserMessage {
10+
string full_name = 1;
11+
string email = 2;
12+
// password_hash is not included in the message for now
13+
string profile_image = 3;
14+
bool is_super_admin = 4;
15+
string created_by = 5;
16+
string updated_by = 6;
17+
google.protobuf.Timestamp created_at = 7;
18+
google.protobuf.Timestamp updated_at = 8;
19+
}

proto/auth_user.proto

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
syntax = "proto3";
22
package auth_user;
33

4+
import "admin_user_message.proto";
45

56
// Login Service
67
message LoginRequest {
78
string email = 1;
89
string password = 2;
910
}
11+
12+
message LoginResponseData {
13+
admin_user_message.AdminUserMessage admin_user = 1;
14+
string token = 2;
15+
}
16+
1017
message LoginResponse {
1118
bool status = 1;
12-
string data = 2;
19+
LoginResponseData data = 2;
1320
}
1421

1522
service Auth {

src/adapters/grpc_api/auth_user_api.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ impl Auth for AuthUserGrpcApi {
2222
) -> Result<Response<LoginResponse>, Status> {
2323
let req = request.into_inner();
2424

25-
let token = self
25+
let login_response_data = self
2626
.login_user_use_case
2727
.execute(&req.email, &req.password)
2828
.await?;
2929

3030
let response = LoginResponse {
3131
status: true,
32-
data: token,
32+
data: Some(login_response_data),
3333
};
3434

3535
Ok(Response::new(response))

src/application/use_cases/login_user_use_case.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::domain::models::admin_user::TokenClaims;
22
use crate::error::Result;
3+
use crate::infra::grpc::admin_user_message::AdminUserMessage;
4+
use crate::infra::grpc::auth_user::LoginResponseData;
35
use crate::infra::setup::get_env;
46
use crate::{
57
application::extensions::string_extension::StringExtension,
@@ -17,25 +19,35 @@ impl LoginUserUseCase {
1719
Self { user_repository }
1820
}
1921

20-
pub async fn execute(&self, email: &str, password: &str) -> Result<String> {
22+
pub async fn execute(&self, email: &str, password: &str) -> Result<LoginResponseData> {
2123
if let Some(admin_user_model) = self.user_repository.find_by_email(email).await {
2224
let raw_password = String::from(password);
2325
let is_valid = raw_password.verify_password_hash(&admin_user_model.password_hash)?;
2426

2527
if is_valid {
2628
let jwt_secret_key = get_env("AVORED_JWT_SECRET")?;
2729

28-
let claims: TokenClaims = admin_user_model.try_into()?;
30+
let claims: TokenClaims = admin_user_model.clone().try_into()?;
2931

3032
let token = encode(
3133
&Header::default(),
3234
&claims,
3335
&EncodingKey::from_secret(jwt_secret_key.as_bytes()),
3436
)?;
3537

36-
return Ok(token);
38+
let grpc_admin_user: AdminUserMessage = admin_user_model.try_into()?;
39+
40+
let login_response_data = LoginResponseData {
41+
admin_user: Some(grpc_admin_user),
42+
token,
43+
};
44+
45+
return Ok(login_response_data);
3746
}
3847
}
39-
Ok(String::from(""))
48+
Ok(LoginResponseData {
49+
admin_user: None,
50+
token: String::from(""),
51+
})
4052
}
4153
}

src/domain/models/admin_user.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::domain::models::BaseModel;
22
use crate::error::{Error, Result};
3+
use crate::infra::grpc::admin_user_message::AdminUserMessage;
4+
use pbjson_types::Timestamp;
35
use serde::{Deserialize, Serialize};
46
use surrealdb::types::Datetime;
57
use surrealdb::types::Value;
@@ -109,3 +111,27 @@ impl TryFrom<AdminUserModel> for TokenClaims {
109111
Ok(claims)
110112
}
111113
}
114+
115+
116+
117+
impl TryFrom<AdminUserModel> for AdminUserMessage {
118+
type Error = Error;
119+
120+
fn try_from(val: AdminUserModel) -> Result<Self> {
121+
let created_at = Timestamp::from(val.created_at.to_utc());
122+
let updated_at = Timestamp::from(val.updated_at.to_utc());
123+
124+
let admin_user_message: Self = Self {
125+
full_name: val.full_name,
126+
email: val.email,
127+
profile_image: val.profile_image,
128+
is_super_admin: val.is_super_admin,
129+
created_by: val.created_by,
130+
updated_by: val.updated_by,
131+
created_at: Some(created_at),
132+
updated_at: Some(updated_at),
133+
};
134+
135+
Ok(admin_user_message)
136+
}
137+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// This file is @generated by prost-build.
2+
#[derive(serde::Serialize)]
3+
#[derive(Clone, PartialEq, Eq, Hash, ::prost::Message)]
4+
pub struct AdminUserMessage {
5+
#[prost(string, tag = "1")]
6+
pub full_name: ::prost::alloc::string::String,
7+
#[prost(string, tag = "2")]
8+
pub email: ::prost::alloc::string::String,
9+
/// password_hash is not included in the message for now
10+
#[prost(string, tag = "3")]
11+
pub profile_image: ::prost::alloc::string::String,
12+
#[prost(bool, tag = "4")]
13+
pub is_super_admin: bool,
14+
#[prost(string, tag = "5")]
15+
pub created_by: ::prost::alloc::string::String,
16+
#[prost(string, tag = "6")]
17+
pub updated_by: ::prost::alloc::string::String,
18+
#[prost(message, optional, tag = "7")]
19+
pub created_at: ::core::option::Option<::pbjson_types::Timestamp>,
20+
#[prost(message, optional, tag = "8")]
21+
pub updated_at: ::core::option::Option<::pbjson_types::Timestamp>,
22+
}

src/infra/grpc/auth_user.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
// This file is @generated by prost-build.
22
/// Login Service
3+
#[derive(serde::Serialize)]
34
#[derive(Clone, PartialEq, Eq, Hash, ::prost::Message)]
45
pub struct LoginRequest {
56
#[prost(string, tag = "1")]
67
pub email: ::prost::alloc::string::String,
78
#[prost(string, tag = "2")]
89
pub password: ::prost::alloc::string::String,
910
}
11+
#[derive(serde::Serialize)]
12+
#[derive(Clone, PartialEq, Eq, Hash, ::prost::Message)]
13+
pub struct LoginResponseData {
14+
#[prost(message, optional, tag = "1")]
15+
pub admin_user: ::core::option::Option<super::admin_user_message::AdminUserMessage>,
16+
#[prost(string, tag = "2")]
17+
pub token: ::prost::alloc::string::String,
18+
}
19+
#[derive(serde::Serialize)]
1020
#[derive(Clone, PartialEq, Eq, Hash, ::prost::Message)]
1121
pub struct LoginResponse {
1222
#[prost(bool, tag = "1")]
1323
pub status: bool,
14-
#[prost(string, tag = "2")]
15-
pub data: ::prost::alloc::string::String,
24+
#[prost(message, optional, tag = "2")]
25+
pub data: ::core::option::Option<LoginResponseData>,
1626
}
1727
/// Generated client implementations.
1828
pub mod auth_client {

0 commit comments

Comments
 (0)