Skip to content

Commit

Permalink
feature: add ws_name into jwt and support cors
Browse files Browse the repository at this point in the history
  • Loading branch information
tyrchen committed Aug 17, 2024
1 parent b10b296 commit cf7f073
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ tokio = { version = "1.37.0", features = ["rt", "rt-multi-thread", "macros"] }
tower = "0.4.13"
tower-http = { version = "0.5.2", features = [
"compression-full",
"cors",
"fs",
"trace",
] }
Expand Down
4 changes: 4 additions & 0 deletions chat_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ pub use utils::*;
use utoipa::ToSchema;

#[derive(Debug, Clone, FromRow, ToSchema, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct User {
pub id: i64,
pub ws_id: i64,
#[sqlx(default)]
pub ws_name: String,
pub fullname: String,
pub email: String,
#[sqlx(default)]
Expand Down Expand Up @@ -71,6 +74,7 @@ impl User {
Self {
id,
ws_id: 0,
ws_name: "".to_string(),
fullname: fullname.to_string(),
email: email.to_string(),
password_hash: None,
Expand Down
16 changes: 15 additions & 1 deletion chat_server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ use openapi::OpenApiRouter;
use sqlx::PgPool;
use std::{fmt, ops::Deref, sync::Arc};
use tokio::fs;
use tower_http::cors::{self, CorsLayer};

pub use error::{AppError, ErrorOutput};
pub use models::*;

use axum::{
http::Method,
middleware::from_fn_with_state,
routing::{get, post},
Router,
Expand Down Expand Up @@ -54,6 +56,17 @@ pub async fn get_router(state: AppState) -> Result<Router, AppError> {
.layer(from_fn_with_state(state.clone(), verify_chat))
.route("/", get(list_chat_handler).post(create_chat_handler));

let cors = CorsLayer::new()
// allow `GET` and `POST` when accessing the resource
.allow_methods([
Method::GET,
Method::POST,
Method::PATCH,
Method::DELETE,
Method::PUT,
])
.allow_origin(cors::Any)
.allow_headers(cors::Any);
let api = Router::new()
.route("/users", get(list_chat_users_handler))
.nest("/chats", chat)
Expand All @@ -62,7 +75,8 @@ pub async fn get_router(state: AppState) -> Result<Router, AppError> {
.layer(from_fn_with_state(state.clone(), verify_token::<AppState>))
// routes doesn't need token verification
.route("/signin", post(signin_handler))
.route("/signup", post(signup_handler));
.route("/signup", post(signup_handler))
.layer(cors);

let app = Router::new()
.openapi()
Expand Down
7 changes: 6 additions & 1 deletion chat_server/src/models/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl AppState {
};

let password_hash = hash_password(&input.password)?;
let user: User = sqlx::query_as(
let mut user: User = sqlx::query_as(
r#"
INSERT INTO users (ws_id, email, fullname, password_hash)
VALUES ($1, $2, $3, $4)
Expand All @@ -81,6 +81,8 @@ impl AppState {
.fetch_one(&self.pool)
.await?;

user.ws_name = ws.name.clone();

if ws.owner_id == 0 {
self.update_workspace_owner(ws.id as _, user.id as _)
.await?;
Expand All @@ -103,6 +105,9 @@ impl AppState {
let is_valid =
verify_password(&input.password, &password_hash.unwrap_or_default())?;
if is_valid {
// load ws_name, ws should exist
let ws = self.find_workspace_by_id(user.ws_id as _).await?.unwrap();
user.ws_name = ws.name;
Ok(Some(user))
} else {
Ok(None)
Expand Down

0 comments on commit cf7f073

Please sign in to comment.