aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/auth-service/src/lib.rs12
-rw-r--r--lib/auth-service/src/service/mod.rs66
-rw-r--r--sellershut/src/server/routes/auth/mod.rs12
3 files changed, 69 insertions, 21 deletions
diff --git a/lib/auth-service/src/lib.rs b/lib/auth-service/src/lib.rs
index 61ff230..abccd65 100644
--- a/lib/auth-service/src/lib.rs
+++ b/lib/auth-service/src/lib.rs
@@ -11,6 +11,18 @@ pub enum Provider {
Discord,
}
+impl std::fmt::Display for Provider {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(
+ f,
+ "{}",
+ match self {
+ Provider::Discord => "discord",
+ }
+ )
+ }
+}
+
#[derive(Error, Debug)]
pub enum AuthServiceError {
#[error("invalid url provided")]
diff --git a/lib/auth-service/src/service/mod.rs b/lib/auth-service/src/service/mod.rs
index 80b29de..6d04b67 100644
--- a/lib/auth-service/src/service/mod.rs
+++ b/lib/auth-service/src/service/mod.rs
@@ -1,7 +1,7 @@
use async_session::{Result, Session, SessionStore};
use async_trait::async_trait;
use shared_svc::cache::{CacheKey, RedisManager, redis::AsyncCommands};
-use sqlx::{PgPool, Postgres, Transaction};
+use sqlx::{Executor, PgPool, Postgres};
use tracing::{debug, instrument};
use crate::Provider;
@@ -9,14 +9,13 @@ use crate::Provider;
#[async_trait]
pub trait AccountMgr {
async fn get_apid_by_email(&self, email: &str) -> Result<Option<String>>;
- async fn create_account(&self, provider: Provider, provider_user_id: &str, ap_id: &str);
- async fn create_account_step(
+ async fn create_account<'c>(
&self,
provider: Provider,
provider_user_id: &str,
ap_id: &str,
email: &str,
- transaction: &mut Transaction<'_, Postgres>,
+ transaction: Option<impl Executor<'c, Database = Postgres>>,
) -> Result;
async fn persist_session(&self);
}
@@ -34,31 +33,34 @@ impl AccountMgr for AuthService {
todo!()
}
- #[instrument(skip(self))]
- async fn create_account(&self, provider: Provider, provider_user_id: &str, ap_id: &str) {
- todo!()
- }
- #[instrument(skip(self, transaction))]
- async fn create_account_step(
+ #[instrument(skip(transaction))]
+ async fn create_account<'c>(
&self,
provider: Provider,
provider_user_id: &str,
ap_id: &str,
email: &str,
- transaction: &mut Transaction<'_, Postgres>,
+ transaction: Option<impl Executor<'c, Database = Postgres>>,
) -> Result {
- sqlx::query!(
+ let query = sqlx::query!(
"insert into account
(provider_id, provider_user_id, email, ap_id)
- values ($1, $2, $3, $4)
+ values
+ ($1, $2, $3, $4)
+ on conflict (provider_id, provider_user_id)
+ do nothing
",
- "",
+ provider.to_string(),
provider_user_id,
- "",
+ email,
ap_id
- )
- .execute(&mut **transaction)
- .await?;
+ );
+
+ if let Some(con) = transaction {
+ query.execute(con).await?;
+ } else {
+ query.execute(&self.database).await?;
+ };
todo!()
}
@@ -129,3 +131,31 @@ impl SessionStore for AuthService {
Ok(())
}
}
+
+async fn create_account_step<'c, E>(
+ provider: Provider,
+ provider_user_id: &str,
+ ap_id: &str,
+ email: &str,
+ transaction: E,
+) -> Result
+where
+ E: Executor<'c, Database = Postgres>,
+{
+ sqlx::query!(
+ "insert into account
+ (provider_id, provider_user_id, email, ap_id)
+ values
+ ($1, $2, $3, $4)
+ on conflict (provider_id, provider_user_id)
+ do nothing
+ ",
+ provider.to_string(),
+ provider_user_id,
+ email,
+ ap_id
+ )
+ .execute(transaction)
+ .await?;
+ todo!()
+}
diff --git a/sellershut/src/server/routes/auth/mod.rs b/sellershut/src/server/routes/auth/mod.rs
index 04b6a2c..7bcfe0b 100644
--- a/sellershut/src/server/routes/auth/mod.rs
+++ b/sellershut/src/server/routes/auth/mod.rs
@@ -147,15 +147,21 @@ pub async fn authorised(
if let Some(ap_id) = data.auth_service.get_apid_by_email(&user.email).await? {
debug!("user exists");
data.auth_service
- .create_account(provider.into(), &user.id, "")
- .await;
+ .create_account(provider.into(), &user.id, &ap_id, &user.email, None::<&sqlx::PgPool>)
+ .await?;
} else {
debug!("user does not exist, creating");
// create account and user in a transaction
let mut transaction = data.database.begin().await?;
data.auth_service
- .create_account_step(provider.into(), &user.id, "", &user.email, &mut transaction)
+ .create_account(
+ provider.into(),
+ &user.id,
+ "",
+ &user.email,
+ Some(&mut *transaction),
+ )
.await?;
transaction.commit().await?;