login / register
This commit is contained in:
34
src/auth.rs
34
src/auth.rs
@@ -1,17 +1,10 @@
|
||||
use std::sync::LazyLock;
|
||||
|
||||
use axum::{
|
||||
extract::{Request, State},
|
||||
http::StatusCode,
|
||||
middleware::Next,
|
||||
response::Response,
|
||||
};
|
||||
use axum::{extract::Request, http::StatusCode, middleware::Next, response::Response};
|
||||
use chrono::Utc;
|
||||
use jsonwebtoken::{Algorithm, DecodingKey, EncodingKey, Header, Validation, decode, encode};
|
||||
use jsonwebtoken::{Algorithm, DecodingKey, EncodingKey, Validation, decode};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::{FromRow, Postgres};
|
||||
|
||||
use crate::state::AppState;
|
||||
use sqlx::FromRow;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
@@ -49,24 +42,9 @@ impl Claims {
|
||||
|
||||
#[derive(Debug, Clone, FromRow)]
|
||||
pub struct User {
|
||||
id: i64,
|
||||
}
|
||||
|
||||
pub async fn get_auth_token(State(state): State<AppState>) -> Result<String, StatusCode> {
|
||||
let Ok(user) = sqlx::query_as::<Postgres, User>("INSERT INTO users DEFAULT VALUES RETURNING *")
|
||||
.fetch_one(&state.pg_pool)
|
||||
.await
|
||||
else {
|
||||
return Err(StatusCode::INTERNAL_SERVER_ERROR);
|
||||
};
|
||||
|
||||
let claims = Claims::new(user.id);
|
||||
|
||||
let Ok(token) = encode(&Header::new(Algorithm::RS512), &claims, &AUTH_SECRET_KEY) else {
|
||||
return Err(StatusCode::INTERNAL_SERVER_ERROR);
|
||||
};
|
||||
|
||||
Ok(token)
|
||||
pub id: i64,
|
||||
pub name: String,
|
||||
pub hash: String,
|
||||
}
|
||||
|
||||
pub fn verify_token(token: &str) -> Option<Claims> {
|
||||
|
||||
Reference in New Issue
Block a user