-
Notifications
You must be signed in to change notification settings - Fork 0
feat: refacto auth #79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Quentintnrl
wants to merge
5
commits into
main
Choose a base branch
from
mair-69-refacto-auth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fff675b
feat: add login first connection
Quentintnrl ffee7f9
Merge branch 'main' into mair-69-refacto-auth
Quentintnrl 569a71c
feat: add password reset
Quentintnrl f9e3c8e
fix: fix lint error
Quentintnrl 4e212bd
feat: update api_lib version
Quentintnrl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,5 @@ | ||
| mod query; | ||
| pub use query::change_password_query; | ||
|
|
||
| mod view; | ||
| pub use view::ChangePasswordQueryView; |
This file contains hidden or 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,18 @@ | ||
| use mairie360_api_lib::database::db_interface::DatabaseQueryView; | ||
| use mairie360_api_lib::database::errors::DatabaseError; | ||
| use sqlx::PgPool; | ||
|
|
||
| use crate::database::auth::change_password::ChangePasswordQueryView; | ||
|
|
||
| pub async fn change_password_query( | ||
| view: ChangePasswordQueryView, | ||
| pool: PgPool, | ||
| ) -> Result<(), DatabaseError> { | ||
| sqlx::query(&view.get_request()) | ||
| .bind(view.get_password()) | ||
| .bind(view.get_user_id() as i32) | ||
| .execute(&pool) | ||
| .await?; | ||
|
|
||
| Ok(()) | ||
| } |
This file contains hidden or 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,68 @@ | ||
| use mairie360_api_lib::database::db_interface::DatabaseQueryView; | ||
| use std::fmt::Display; | ||
|
|
||
| pub struct ChangePasswordQueryView { | ||
| password: String, | ||
| user_id: u64, | ||
| } | ||
|
|
||
| impl ChangePasswordQueryView { | ||
| pub fn new(password: &str, user_id: u64) -> Self { | ||
| Self { | ||
| password: password.to_string(), | ||
| user_id, | ||
| } | ||
| } | ||
|
|
||
| pub fn get_password(&self) -> &str { | ||
| &self.password | ||
| } | ||
|
|
||
| pub fn get_user_id(&self) -> u64 { | ||
| self.user_id | ||
| } | ||
| } | ||
|
|
||
| impl DatabaseQueryView for ChangePasswordQueryView { | ||
| fn get_request(&self) -> String { | ||
| "UPDATE users SET password = $1 WHERE id = $2".to_string() | ||
| } | ||
| } | ||
|
|
||
| impl Display for ChangePasswordQueryView { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| write!(f, "ChangePasswordQueryView: password = [PROTECTED]") | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, sqlx::FromRow, PartialEq, Eq)] | ||
| pub struct LoginUserQueryResultView { | ||
| #[sqlx(rename = "id")] | ||
| user_id: i32, | ||
| #[sqlx(rename = "password")] | ||
| password: String, | ||
| #[sqlx(rename = "first_connect")] | ||
| first_connect: bool, | ||
| } | ||
|
|
||
| impl LoginUserQueryResultView { | ||
| pub fn new(user_id: i32, password: String, first_connect: bool) -> Self { | ||
| Self { | ||
| user_id, | ||
| password, | ||
| first_connect, | ||
| } | ||
| } | ||
|
|
||
| pub fn password(&self) -> &str { | ||
| &self.password | ||
| } | ||
|
|
||
| pub fn user_id(&self) -> i32 { | ||
| self.user_id | ||
| } | ||
|
|
||
| pub fn first_connect(&self) -> bool { | ||
| self.first_connect | ||
| } | ||
| } | ||
|
Quentintnrl marked this conversation as resolved.
|
||
This file contains hidden or 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,5 @@ | ||
| mod query; | ||
| pub use query::is_first_time_query; | ||
|
|
||
| mod view; | ||
| pub use view::IsFirstTimeQueryView; |
This file contains hidden or 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,16 @@ | ||
| use crate::database::auth::is_first_time::IsFirstTimeQueryView; | ||
| use mairie360_api_lib::database::db_interface::DatabaseQueryView; | ||
| use mairie360_api_lib::database::errors::DatabaseError; | ||
| use sqlx::PgPool; | ||
|
|
||
| pub async fn is_first_time_query( | ||
| view: IsFirstTimeQueryView, | ||
| pool: PgPool, | ||
| ) -> Result<bool, DatabaseError> { | ||
| let result = sqlx::query_scalar::<_, bool>(&view.get_request()) | ||
| .bind(view.user_id() as i32) | ||
| .fetch_one(&pool) | ||
| .await?; | ||
|
|
||
| Ok(result) | ||
| } |
This file contains hidden or 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,28 @@ | ||
| use mairie360_api_lib::database::db_interface::DatabaseQueryView; | ||
| use std::fmt::Display; | ||
|
|
||
| pub struct IsFirstTimeQueryView { | ||
| user_id: u64, | ||
| } | ||
|
|
||
| impl IsFirstTimeQueryView { | ||
| pub fn new(user_id: u64) -> Self { | ||
| Self { user_id } | ||
| } | ||
|
|
||
| pub fn user_id(&self) -> u64 { | ||
| self.user_id | ||
| } | ||
| } | ||
|
|
||
| impl DatabaseQueryView for IsFirstTimeQueryView { | ||
| fn get_request(&self) -> String { | ||
| "SELECT EXISTS(SELECT 1 FROM users WHERE id = $1) AS first_connect".to_string() | ||
|
Quentintnrl marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| impl Display for IsFirstTimeQueryView { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| write!(f, "IsFirstTimeQueryView: user_id = {}", self.user_id) | ||
| } | ||
| } | ||
This file contains hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,2 +1,5 @@ | ||
| pub mod change_password; | ||
| pub mod is_first_time; | ||
| pub mod login; | ||
| pub mod register; | ||
| pub mod unset_first_connection; |
This file contains hidden or 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,5 @@ | ||
| mod query; | ||
| pub use query::unset_first_connection_query; | ||
|
|
||
| mod view; | ||
| pub use view::UnsetFirstConnectionQueryView; |
This file contains hidden or 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,16 @@ | ||
| use crate::database::auth::unset_first_connection::UnsetFirstConnectionQueryView; | ||
| use mairie360_api_lib::database::{db_interface::DatabaseQueryView, errors::DatabaseError}; | ||
| use sqlx::PgPool; | ||
|
|
||
| pub async fn unset_first_connection_query( | ||
| view: UnsetFirstConnectionQueryView, | ||
| pool: PgPool, | ||
| ) -> Result<(), DatabaseError> { | ||
| sqlx::query(&view.get_request()) | ||
| .bind(view.password()) | ||
| .bind(view.user_id() as i32) | ||
| .execute(&pool) | ||
| .await?; | ||
|
|
||
|
Quentintnrl marked this conversation as resolved.
|
||
| Ok(()) | ||
| } | ||
This file contains hidden or 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,40 @@ | ||
| use mairie360_api_lib::database::db_interface::DatabaseQueryView; | ||
| use std::fmt::Display; | ||
|
|
||
| pub struct UnsetFirstConnectionQueryView { | ||
| user_id: u64, | ||
| password: String, | ||
| } | ||
|
|
||
| impl UnsetFirstConnectionQueryView { | ||
| pub fn new(user_id: u64, password: &str) -> Self { | ||
| Self { | ||
| user_id, | ||
| password: password.to_string(), | ||
| } | ||
| } | ||
|
|
||
| pub fn user_id(&self) -> u64 { | ||
| self.user_id | ||
| } | ||
|
|
||
| pub fn password(&self) -> &str { | ||
| &self.password | ||
| } | ||
| } | ||
|
|
||
| impl DatabaseQueryView for UnsetFirstConnectionQueryView { | ||
| fn get_request(&self) -> String { | ||
| "UPDATE users SET first_connect = false AND password = $1 WHERE id = $2".to_string() | ||
|
Quentintnrl marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| impl Display for UnsetFirstConnectionQueryView { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| write!( | ||
| f, | ||
| "UnsetFirstConnectionQueryView: user_id = {}, password = [PROTECTED]", | ||
| self.user_id | ||
| ) | ||
| } | ||
| } | ||
This file contains hidden or 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,5 @@ | ||
| mod query; | ||
| pub use query::get_user_id_query; | ||
|
|
||
| mod view; | ||
| pub use view::GetUserIdQueryView; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.