mirror of
https://github.com/danbulant/Portfolio
synced 2026-07-05 11:00:56 +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]
|
#[macro_use]
|
||||||
extern crate rocket;
|
extern crate rocket;
|
||||||
|
|
||||||
|
use guard::candidate_jwt::TokenRequest;
|
||||||
|
use portfolio_core::services::candidate_service::CandidateService;
|
||||||
|
use requests::LoginRequest;
|
||||||
use rocket::{Rocket, Build};
|
use rocket::{Rocket, Build};
|
||||||
use rocket::serde::json::Json;
|
use rocket::serde::json::Json;
|
||||||
use rocket::fairing::{self, AdHoc};
|
use rocket::fairing::{self, AdHoc};
|
||||||
|
|
@ -13,6 +16,7 @@ use sea_orm_rocket::{Connection, Database};
|
||||||
|
|
||||||
mod pool;
|
mod pool;
|
||||||
mod guard;
|
mod guard;
|
||||||
|
mod requests;
|
||||||
|
|
||||||
use pool::Db;
|
use pool::Db;
|
||||||
|
|
||||||
|
|
@ -36,6 +40,28 @@ async fn create(conn: Connection<'_, Db>, post_form: Json<candidate::Model>) ->
|
||||||
Ok(plain_text_password)
|
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")]
|
#[get("/hello")]
|
||||||
async fn hello() -> &'static str {
|
async fn hello() -> &'static str {
|
||||||
"Hello, world!"
|
"Hello, world!"
|
||||||
|
|
@ -53,7 +79,7 @@ async fn start() -> Result<(), rocket::Error> {
|
||||||
.attach(Db::init())
|
.attach(Db::init())
|
||||||
.attach(AdHoc::try_on_ignite("Migrations", run_migrations))
|
.attach(AdHoc::try_on_ignite("Migrations", run_migrations))
|
||||||
//.mount("/", FileServer::from(relative!("/static")))
|
//.mount("/", FileServer::from(relative!("/static")))
|
||||||
.mount("/", routes![create, hello])
|
.mount("/", routes![create, login, hello, whoami])
|
||||||
.register("/", catchers![])
|
.register("/", catchers![])
|
||||||
.launch()
|
.launch()
|
||||||
.await
|
.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};
|
use crate::{crypto, Query, token::candidate_token::CandidateToken};
|
||||||
|
|
||||||
pub async fn login(db: &DatabaseConnection, id: i32, password: String) -> Option<String> {
|
pub struct CandidateService;
|
||||||
let candidate = Query::find_candidate_by_id(db, id).await
|
|
||||||
.unwrap()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
|
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 {
|
let jwt = jsonwebtoken::encode(
|
||||||
return None;
|
&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