-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): authenticate with cookies and token (#211)
- Loading branch information
Showing
39 changed files
with
907 additions
and
306 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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
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,21 @@ | ||
query Me { | ||
me { | ||
user { | ||
id | ||
name | ||
surname | ||
username | ||
createdAt | ||
updatedAt | ||
avatar { | ||
id | ||
url | ||
} | ||
} | ||
error { | ||
code | ||
message | ||
} | ||
} | ||
} |
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,37 @@ | ||
use anyhow::{anyhow, bail, Result}; | ||
use graphql_client::reqwest::post_graphql; | ||
use graphql_client::GraphQLQuery; | ||
use pxid::Pxid; | ||
|
||
use me::{MeMeError, MeMeUser, Variables}; | ||
|
||
use crate::{DateTime, GRAPHQL_PATH}; | ||
|
||
use super::AuthClient; | ||
|
||
#[derive(GraphQLQuery)] | ||
#[graphql( | ||
response_derives = "Clone,Debug,Deserialize", | ||
schema_path = "schema.json", | ||
query_path = "src/modules/auth/me/Me.gql" | ||
)] | ||
pub struct Me { | ||
pub user: Option<MeMeUser>, | ||
pub error: Option<MeMeError>, | ||
} | ||
|
||
pub async fn me(auth_client: &AuthClient) -> Result<Me> { | ||
let url = auth_client.domain.join(GRAPHQL_PATH)?; | ||
let res = post_graphql::<Me, _>(&auth_client.client, url, Variables {}) | ||
.await | ||
.map_err(|err| anyhow!("Failed to create token. {err}"))?; | ||
|
||
if let Some(ref data) = res.data { | ||
return Ok(Me { | ||
user: data.me.user.to_owned(), | ||
error: data.me.error.to_owned(), | ||
}); | ||
} | ||
|
||
bail!("Failed to get user data. err = {:#?}", res.errors.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,33 +1,57 @@ | ||
pub mod me; | ||
pub mod token_create; | ||
pub mod user_register; | ||
|
||
use reqwest::Client; | ||
use anyhow::{anyhow, Result}; | ||
use http_auth_basic::Credentials; | ||
use reqwest::header::{HeaderValue, AUTHORIZATION}; | ||
use reqwest::{Client, Url}; | ||
|
||
pub struct AuthClient { | ||
client: Client, | ||
} | ||
|
||
impl Default for AuthClient { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
domain: Url, | ||
} | ||
|
||
impl AuthClient { | ||
pub fn new() -> Self { | ||
pub fn new(domain: Url) -> Self { | ||
Self { | ||
domain, | ||
client: Client::new(), | ||
} | ||
} | ||
|
||
pub async fn token_create(&self, email: String, password: String) -> token_create::TokenCreate { | ||
token_create::token_create(&self.client, email, password).await | ||
pub async fn login(&self, email: String, password: String) -> Result<()> { | ||
let credentials = Credentials::new(email.as_str(), password.as_str()); | ||
let authorization = credentials.as_http_header(); | ||
let authorization = HeaderValue::from_str(authorization.as_str()).unwrap(); | ||
let url = self.domain.join("/api/v1/auth/login")?; | ||
|
||
self.client | ||
.get(url) | ||
.header(AUTHORIZATION, authorization) | ||
.send() | ||
.await | ||
.map_err(|err| anyhow!("Failed to authenticate. {err}"))?; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub async fn token_create( | ||
&self, | ||
email: String, | ||
password: String, | ||
) -> Result<token_create::TokenCreate> { | ||
token_create::token_create(self, email, password).await | ||
} | ||
|
||
pub async fn user_register( | ||
&self, | ||
input: user_register::UserRegisterInput, | ||
) -> user_register::UserRegister { | ||
user_register::user_register(&self.client, input).await | ||
) -> Result<user_register::UserRegister> { | ||
user_register::user_register(self, input).await | ||
} | ||
|
||
pub async fn me(&self) -> Result<me::Me> { | ||
me::me(self).await | ||
} | ||
} |
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
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
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
Oops, something went wrong.