Skip to content

Commit

Permalink
Introduce Client Auth, Upgrade Vite, Introduce Tanstack Router, and more
Browse files Browse the repository at this point in the history
  • Loading branch information
lucemans committed Jul 29, 2024
1 parent 33c2d2b commit 89a7aff
Show file tree
Hide file tree
Showing 16 changed files with 388 additions and 144 deletions.
7 changes: 6 additions & 1 deletion engine/src/auth/middleware.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::Arc;

use poem::{web::Data, Error, FromRequest, Request, RequestBody, Result};
use reqwest::StatusCode;
use uuid::Uuid;

use crate::state::AppState;
Expand Down Expand Up @@ -33,7 +34,11 @@ impl<'a> FromRequest<'a> for AuthToken {
// Check if active session exists with token
let session = SessionState::get_by_id(token, &state.database)
.await
.unwrap();
.unwrap()
.ok_or(Error::from_string(
"Session not found, token valid but no session exists",
StatusCode::UNAUTHORIZED,
))?;

Ok(AuthToken::Active(ActiveUser { session }))
}
Expand Down
4 changes: 2 additions & 2 deletions engine/src/auth/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ impl SessionState {
Ok(session)
}

pub async fn get_by_id(id: Uuid, database: &Database) -> Result<Self, sqlx::Error> {
pub async fn get_by_id(id: Uuid, database: &Database) -> Result<Option<Self>, sqlx::Error> {
let session = sqlx::query_as::<_, SessionState>(
"SELECT * FROM sessions WHERE id = $1 AND valid = TRUE",
)
.bind(id)
.fetch_one(&database.pool)
.fetch_optional(&database.pool)
.await?;

Ok(session)
Expand Down
5 changes: 4 additions & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "web",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "vite",
Expand All @@ -13,6 +13,7 @@
"author": "",
"license": "GPL-3",
"dependencies": {
"@tanstack/react-router": "^1.45.14",
"@vitejs/plugin-react": "^4.3.1",
"autoprefixer": "^10.4.19",
"postcss": "^8.4.38",
Expand All @@ -24,6 +25,8 @@
"zustand": "^4.5.4"
},
"devDependencies": {
"@tanstack/router-devtools": "^1.45.14",
"@tanstack/router-plugin": "^1.45.13",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@types/url-search-params": "^1.1.2",
Expand Down
Loading

0 comments on commit 89a7aff

Please sign in to comment.