forked from gorb/backend
lets me reuse something that will happen often instead of having to write it manually in every file
41 lines
1.2 KiB
Rust
41 lines
1.2 KiB
Rust
use std::{str::FromStr, time::{SystemTime, UNIX_EPOCH}};
|
|
|
|
use actix_web::{web, HttpResponse, Scope};
|
|
use sqlx::Postgres;
|
|
use uuid::Uuid;
|
|
|
|
mod register;
|
|
mod login;
|
|
mod refresh;
|
|
|
|
pub fn web() -> Scope {
|
|
web::scope("/auth")
|
|
.service(register::res)
|
|
.service(login::response)
|
|
.service(refresh::res)
|
|
}
|
|
|
|
pub async fn check_access_token(access_token: String, pool: sqlx::Pool<Postgres>) -> Result<Uuid, HttpResponse> {
|
|
match sqlx::query_as("SELECT CAST(uuid as VARCHAR), created FROM access_tokens WHERE token = $1")
|
|
.bind(&access_token)
|
|
.fetch_one(&pool)
|
|
.await {
|
|
Ok(row) => {
|
|
let (uuid, created): (String, i64) = row;
|
|
|
|
let current_time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() as i64;
|
|
|
|
let lifetime = current_time - created;
|
|
|
|
if lifetime > 3600 {
|
|
return Err(HttpResponse::Unauthorized().finish())
|
|
}
|
|
|
|
Ok(Uuid::from_str(&uuid).unwrap())
|
|
},
|
|
Err(error) => {
|
|
eprintln!("{}", error);
|
|
Err(HttpResponse::InternalServerError().finish())
|
|
}
|
|
}
|
|
}
|