mirror of
https://github.com/danbulant/Portfolio
synced 2026-06-18 22:11:14 +00:00
feat: user login service
This commit is contained in:
parent
4a852fa915
commit
32c266e366
5 changed files with 51 additions and 0 deletions
|
|
@ -2,6 +2,7 @@ mod mutation;
|
|||
mod query;
|
||||
pub mod crypto;
|
||||
pub mod token;
|
||||
pub mod services;
|
||||
|
||||
pub use mutation::*;
|
||||
pub use query::*;
|
||||
|
|
|
|||
28
core/src/services/candidate_service.rs
Normal file
28
core/src/services/candidate_service.rs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
use jsonwebtoken::{Header, EncodingKey};
|
||||
use sea_orm::DatabaseConnection;
|
||||
|
||||
use crate::{crypto, Query, token::candidate_token::CandidateToken};
|
||||
|
||||
pub async fn login(db: &DatabaseConnection, id: i32, password: String) -> Option<String> {
|
||||
let candidate = Query::find_candidate_by_id(db, id).await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
|
||||
|
||||
let valid = crypto::verify_password(&password,&candidate.code )
|
||||
.expect("Invalid password");
|
||||
|
||||
if !valid {
|
||||
return None;
|
||||
}
|
||||
let payload = CandidateToken::generate(candidate.name.unwrap(),
|
||||
candidate.surname.unwrap());
|
||||
|
||||
let jwt = jsonwebtoken::encode(
|
||||
&Header::default(),
|
||||
&payload,
|
||||
&EncodingKey::from_secret(&[0])
|
||||
).ok();
|
||||
jwt
|
||||
}
|
||||
|
||||
1
core/src/services/mod.rs
Normal file
1
core/src/services/mod.rs
Normal file
|
|
@ -0,0 +1 @@
|
|||
pub mod candidate_service;
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
use chrono::Utc;
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
|
|
@ -8,4 +9,16 @@ pub struct CandidateToken {
|
|||
pub exp: i64,
|
||||
pub name: String,
|
||||
pub surname: String,
|
||||
}
|
||||
|
||||
impl CandidateToken {
|
||||
pub fn generate(name: String, surname: String) -> Self {
|
||||
let now = Utc::now().timestamp();
|
||||
CandidateToken {
|
||||
iat: now,
|
||||
exp: now + 60 * 60, // 1 hour for now
|
||||
name,
|
||||
surname,
|
||||
}
|
||||
}
|
||||
}
|
||||
8
entity/src/mod.rs
Normal file
8
entity/src/mod.rs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
//! SeaORM Entity. Generated by sea-orm-codegen 0.9.3
|
||||
|
||||
pub mod prelude;
|
||||
|
||||
pub mod admin;
|
||||
pub mod candidate;
|
||||
pub mod parent;
|
||||
pub mod session;
|
||||
Loading…
Reference in a new issue