Fixes leave on the server and reworked login flow.

This commit is contained in:
2025-06-27 14:45:36 +02:00
parent fbe456a0f5
commit f0883a0ff0
31 changed files with 472 additions and 71 deletions

View File

@@ -26,6 +26,19 @@ paths:
$ref: '#/components/requestBodies/LoginRequest'
description: Submit your credentials to get a JWT-token to use with the rest of the api.
parameters: []
post:
summary: ''
operationId: post-token
responses:
'200':
$ref: '#/components/responses/TokenResponse'
'401':
$ref: '#/components/responses/FailureResponse'
description: Refresh your JWT-token without logging in again.
parameters: []
security:
- JWT-Auth: []
/user:
post:
summary: ''

View File

@@ -38,6 +38,7 @@ async fn main() -> std::io::Result<()> {
.wrap(TracingLogger::default())
.app_data(web::Data::new(pool.clone()))
.service(login)
.service(refresh)
.service(register)
.service(gamenights)
.service(gamenight_post)
@@ -45,6 +46,7 @@ async fn main() -> std::io::Result<()> {
.service(get_user)
.service(get_user_unauthenticated)
.service(post_join_gamenight)
.service(post_leave_gamenight)
.service(get_get_participants)
})
.bind(("::1", 8080))?

View File

@@ -6,7 +6,7 @@ use jsonwebtoken::{encode, Header, EncodingKey, decode, DecodingKey, Validation}
use serde::{Serialize, Deserialize};
use uuid::Uuid;
use gamenight_database::{user::{get_user, Role, User}, DbPool};
use gamenight_database::{user::{get_user, User}, DbPool};
use super::error::ApiError;
@@ -16,21 +16,18 @@ pub struct Claims {
uid: Uuid
}
pub struct AuthUser {
pub id: Uuid,
pub username: String,
pub email: String,
pub role: Role,
}
pub struct AuthUser(pub User);
// pub struct AuthUser {
// pub id: Uuid,
// pub username: String,
// pub email: String,
// pub role: Role,
// }
impl From<User> for AuthUser {
fn from(value: User) -> Self {
Self{
id: value.id,
username: value.username,
email: value.email,
role: value.role,
}
Self(value)
}
}

View File

@@ -14,7 +14,7 @@ impl AddGamenightRequestBody {
datetime: DateTime::parse_from_rfc3339(&self.clone().datetime.unwrap())?.with_timezone(&chrono::Utc),
id: Uuid::new_v4(),
name: self.clone().name.unwrap().clone(),
owner_id: user.id
owner_id: user.0.id
})
}
}

View File

@@ -10,7 +10,7 @@ pub async fn post_join_gamenight(pool: web::Data<DbPool>, user: AuthUser, gameni
let mut conn = pool.get_conn();
Ok(insert_gamenight_participant(&mut conn, GamenightParticipant {
gamenight_id: Uuid::parse_str(&gamenight_id.gamenight_id)?,
user_id: user.id
user_id: user.0.id
})?)
}).await??;
@@ -21,10 +21,16 @@ pub async fn post_join_gamenight(pool: web::Data<DbPool>, user: AuthUser, gameni
pub async fn post_leave_gamenight(pool: web::Data<DbPool>, user: AuthUser, gamenight_id: web::Json<GamenightId>) -> Result<impl Responder, ApiError> {
web::block(move || -> Result<usize, ApiError> {
let mut conn = pool.get_conn();
Ok(delete_gamenight_participant(&mut conn, GamenightParticipant {
let participant = GamenightParticipant {
gamenight_id: Uuid::parse_str(&gamenight_id.gamenight_id)?,
user_id: user.id
})?)
user_id: user.0.id
};
println!("{:?}", participant);
let x = delete_gamenight_participant(&mut conn, participant)?;
println!("Amount of deleted rows: {:?}", x);
Ok(x)
}).await??;
Ok(HttpResponse::Ok())

View File

@@ -7,6 +7,7 @@ mod join_gamenight;
mod participant_handlers;
pub use user_handlers::login;
pub use user_handlers::refresh;
pub use user_handlers::register;
pub use gamenight_handlers::gamenights;
pub use gamenight_handlers::gamenight_post;
@@ -14,4 +15,5 @@ pub use gamenight_handlers::gamenight_get;
pub use user_handlers::get_user;
pub use user_handlers::get_user_unauthenticated;
pub use join_gamenight::post_join_gamenight;
pub use join_gamenight::post_leave_gamenight;
pub use participant_handlers::get_get_participants;

View File

@@ -105,14 +105,22 @@ pub async fn login(pool: web::Data<DbPool>, login_data: web::Json<Login>) -> Res
let response = Token{ jwt_token: Some(token) };
Ok(HttpResponse::Ok()
.content_type(ContentType::json())
.body(serde_json::to_string(&response)?)
)
.body(serde_json::to_string(&response)?))
}
else {
Err(ApiError{status: 401, message: "User doesn't exist or password doesn't match".to_string()})
}
}
#[post("/token")]
pub async fn refresh(user: AuthUser) -> Result<impl Responder, ApiError> {
let new_token = get_token(&user.0)?;
let response = Token{ jwt_token: Some(new_token) };
Ok(HttpResponse::Ok()
.content_type(ContentType::json())
.body(serde_json::to_string(&response)?))
}
#[post("/user")]
pub async fn register(pool: web::Data<DbPool>, register_data: web::Json<Registration>) -> Result<impl Responder, ApiError> {
web::block(move || -> Result<(), ApiError> {