mirror of
https://github.com/danbulant/Portfolio
synced 2026-06-18 14:01:04 +00:00
feat: login
This commit is contained in:
parent
951c5de04f
commit
28686a127a
3 changed files with 58 additions and 19 deletions
|
|
@ -1,6 +1,9 @@
|
|||
#[macro_use]
|
||||
extern crate rocket;
|
||||
|
||||
use guard::candidate_jwt::TokenRequest;
|
||||
use portfolio_core::services::candidate_service::CandidateService;
|
||||
use requests::LoginRequest;
|
||||
use rocket::{Rocket, Build};
|
||||
use rocket::serde::json::Json;
|
||||
use rocket::fairing::{self, AdHoc};
|
||||
|
|
@ -13,6 +16,7 @@ use sea_orm_rocket::{Connection, Database};
|
|||
|
||||
mod pool;
|
||||
mod guard;
|
||||
mod requests;
|
||||
|
||||
use pool::Db;
|
||||
|
||||
|
|
@ -36,6 +40,28 @@ async fn create(conn: Connection<'_, Db>, post_form: Json<candidate::Model>) ->
|
|||
Ok(plain_text_password)
|
||||
}
|
||||
|
||||
#[post("/login", data = "<login_form>")]
|
||||
async fn login(conn: Connection<'_, Db>, login_form: Json<LoginRequest>) -> Result<String, Custom<String>> {
|
||||
let db = conn.into_inner();
|
||||
println!("{} {}", login_form.application_id, login_form.password);
|
||||
|
||||
let jwt = CandidateService::login(db,
|
||||
login_form.application_id,
|
||||
login_form.password.to_owned()).await;
|
||||
|
||||
if jwt.is_some() {
|
||||
return Ok(jwt.unwrap())
|
||||
}
|
||||
Ok("jwt here".to_owned())
|
||||
}
|
||||
|
||||
#[get("/whoami")]
|
||||
async fn whoami(token: TokenRequest) -> Result<String, Custom<String>> {
|
||||
println!("{:?}", token.to_token());
|
||||
|
||||
Ok("authenticated!".to_owned())
|
||||
}
|
||||
|
||||
#[get("/hello")]
|
||||
async fn hello() -> &'static str {
|
||||
"Hello, world!"
|
||||
|
|
@ -53,7 +79,7 @@ async fn start() -> Result<(), rocket::Error> {
|
|||
.attach(Db::init())
|
||||
.attach(AdHoc::try_on_ignite("Migrations", run_migrations))
|
||||
//.mount("/", FileServer::from(relative!("/static")))
|
||||
.mount("/", routes![create, hello])
|
||||
.mount("/", routes![create, login, hello, whoami])
|
||||
.register("/", catchers![])
|
||||
.launch()
|
||||
.await
|
||||
|
|
|
|||
9
api/src/requests.rs
Normal file
9
api/src/requests.rs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
use rocket::serde::{Serialize, Deserialize};
|
||||
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(crate = "rocket::serde")]
|
||||
pub struct LoginRequest {
|
||||
pub application_id: i32,
|
||||
pub password: String,
|
||||
}
|
||||
|
|
@ -3,26 +3,30 @@ 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();
|
||||
pub struct CandidateService;
|
||||
|
||||
impl CandidateService {
|
||||
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");
|
||||
|
||||
let valid = crypto::verify_password(&password,&candidate.code )
|
||||
.expect("Invalid password");
|
||||
|
||||
if !valid {
|
||||
return None;
|
||||
}
|
||||
let payload = CandidateToken::generate("candidate.name.unwrap()".to_owned(),
|
||||
"candidate.surname.unwrap()".to_owned());
|
||||
|
||||
if !valid {
|
||||
return None;
|
||||
let jwt = jsonwebtoken::encode(
|
||||
&Header::default(),
|
||||
&payload,
|
||||
&EncodingKey::from_secret(&[0])
|
||||
).ok();
|
||||
jwt
|
||||
}
|
||||
let payload = CandidateToken::generate(candidate.name.unwrap(),
|
||||
candidate.surname.unwrap());
|
||||
|
||||
let jwt = jsonwebtoken::encode(
|
||||
&Header::default(),
|
||||
&payload,
|
||||
&EncodingKey::from_secret(&[0])
|
||||
).ok();
|
||||
jwt
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue