35 lines
		
	
	
		
			723 B
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			35 lines
		
	
	
		
			723 B
		
	
	
	
		
			Rust
		
	
	
	
	
	
| use chrono::TimeDelta;
 | |
| 
 | |
| use crate::{
 | |
|     auth::{authenticate_user, Error, Token},
 | |
|     prelude::*,
 | |
| };
 | |
| 
 | |
| #[derive(Deserialize, Debug)]
 | |
| pub struct Params {
 | |
|     username: String,
 | |
|     password: String,
 | |
| }
 | |
| 
 | |
| fn get_exp() -> i64 {
 | |
|     let mut time = chrono::Utc::now();
 | |
|     time += TimeDelta::minutes(30);
 | |
|     time.timestamp()
 | |
| }
 | |
| 
 | |
| pub async fn post(
 | |
|     State(state): State<AppState>,
 | |
|     Json(payload): Json<Params>,
 | |
| ) -> Result<Json<Token>, Error> {
 | |
|     let user_id = authenticate_user(&payload.username, &payload.password, &state.pool)
 | |
|         .await
 | |
|         .map_err(|_| Error::WrongCredentials)?
 | |
|         .ok_or(Error::WrongCredentials)?;
 | |
|     Claims {
 | |
|         user_id,
 | |
|         exp: get_exp(),
 | |
|     }
 | |
|     .encode()
 | |
|     .map(Json)
 | |
| }
 |