-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- Add down migration script here | ||
DROP TABLE IF EXISTS accounts; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
-- Add up migration script here | ||
CREATE TABLE IF NOT EXISTS accounts ( | ||
id serial NOT NULL, | ||
email VARCHAR(255) NOT NULL PRIMARY KEY, | ||
password VARCHAR(255) NOT NULL | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use argon2::Config; | ||
use axum::{extract::State, Json}; | ||
use rand::Rng; | ||
use tracing::{event, Level}; | ||
|
||
use crate::{common::error::Error, models::account::Account, repositories::store::Store}; | ||
|
||
pub async fn register( | ||
State(store): State<Store>, | ||
Json(account): Json<Account>, | ||
) -> Result<String, Error> { | ||
event!(target:"axum-web-dev", Level::INFO, "register new user"); | ||
let hashed_pwd = hash_passowrd(account.password.as_bytes()); | ||
|
||
let account = Account { | ||
id: account.id, | ||
email: account.email, | ||
password: hashed_pwd, | ||
}; | ||
|
||
let _res: Result<bool, Error> = match store.add_account(account).await { | ||
Err(e) => return Err(e), | ||
Ok(res) => Ok(res), | ||
}; | ||
|
||
Ok(String::from("Success")) | ||
} | ||
|
||
pub fn hash_passowrd(password: &[u8]) -> String { | ||
let salt = rand::thread_rng().gen::<[u8; 32]>(); | ||
let config = Config::default(); | ||
argon2::hash_encoded(password, &salt, &config).unwrap() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
use axum::{response::IntoResponse, Json}; | ||
|
||
pub mod account; | ||
pub mod answer; | ||
pub mod question; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone)] | ||
pub struct Account { | ||
pub id: Option<AccountId>, | ||
pub email: String, | ||
pub password: String, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)] | ||
pub struct AccountId(pub i32); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
use serde::Deserialize; | ||
|
||
pub mod account; | ||
pub mod answer; | ||
pub mod question; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use axum::{routing::post, Router}; | ||
|
||
use crate::{handlers::account::register, repositories::store::Store}; | ||
|
||
pub fn create_router(store: Store) -> Router { | ||
Router::new() | ||
.route("/api/registration", post(register)) | ||
.with_state(store) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters