summaryrefslogtreecommitdiff
path: root/src/routes
diff options
context:
space:
mode:
Diffstat (limited to 'src/routes')
-rw-r--r--src/routes/auth.rs4
-rw-r--r--src/routes/user.rs16
2 files changed, 16 insertions, 4 deletions
diff --git a/src/routes/auth.rs b/src/routes/auth.rs
index 4fb9124..979e617 100644
--- a/src/routes/auth.rs
+++ b/src/routes/auth.rs
@@ -2,7 +2,7 @@ use std::time::{SystemTime, UNIX_EPOCH};
use axum::extract::{Json, State, FromRequestParts};
use axum::response::IntoResponse;
-use axum::http::{StatusCode, request::Parts};
+use axum::http::{request::Parts};
use axum_extra::extract::CookieJar;
use axum_extra::extract::cookie::{Cookie, SameSite};
use jsonwebtoken::{DecodingKey, EncodingKey, Header, Validation, decode, encode};
@@ -21,6 +21,7 @@ pub struct Claims {
pub sub: String,
pub exp: usize,
pub iat: usize,
+ pub username: String,
pub is_admin: bool,
}
@@ -92,6 +93,7 @@ pub async fn login(
sub: user.email().to_string(),
iat: now,
exp: now + 60 * 60 * 24,
+ username: user.username().to_string(),
is_admin: false
};
diff --git a/src/routes/user.rs b/src/routes/user.rs
index f64cb39..157cf09 100644
--- a/src/routes/user.rs
+++ b/src/routes/user.rs
@@ -1,14 +1,14 @@
use axum::extract::{Json, State};
use axum::http::StatusCode;
use axum::response::IntoResponse;
-use serde::Deserialize;
+use serde::{Deserialize, Serialize};
use crate::AppState;
-use crate::auth::hash_password;
+use crate::routes::auth::{AuthUser, hash_password};
use super::errors::RouteError;
#[derive(Deserialize)]
-pub struct CreateUserRequest {
+pub(crate) struct CreateUserRequest {
email: String,
username: String,
password: String,
@@ -42,3 +42,13 @@ pub async fn create_user(
return Ok((StatusCode::CREATED, Json(user)));
}
+#[derive(Serialize)]
+struct MeResponse {
+ username: String,
+}
+
+pub async fn me(
+ AuthUser(claims): AuthUser,
+) -> Result<impl IntoResponse, RouteError> {
+ Ok(Json(MeResponse { username: claims.username }))
+}