aboutsummaryrefslogtreecommitdiffstats
path: root/crates/api-auth/src/discord/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/api-auth/src/discord/mod.rs')
-rw-r--r--crates/api-auth/src/discord/mod.rs26
1 files changed, 21 insertions, 5 deletions
diff --git a/crates/api-auth/src/discord/mod.rs b/crates/api-auth/src/discord/mod.rs
index dbcb139..1a7d47d 100644
--- a/crates/api-auth/src/discord/mod.rs
+++ b/crates/api-auth/src/discord/mod.rs
@@ -1,11 +1,12 @@
use api_core::models::user::User;
-use async_session::Session;
+use async_session::{Session, serde_json};
use async_trait::async_trait;
use oauth2::{CsrfToken, Scope};
-use sh_util::cache::RedisManager;
+use redis::AsyncCommands;
+use sh_util::cache::{CacheKey, RedisManager};
use sqlx::PgPool;
-use crate::{BasicClient, CSRF_TOKEN, OauthDriver, error::AuthError};
+use crate::{BasicClient, CSRF_TOKEN, OauthDriver, SessionResponse, error::AuthError};
#[derive(Clone)]
pub struct AuthServiceDiscord {
@@ -32,7 +33,7 @@ impl OauthDriver for AuthServiceDiscord {
async fn get_user(&self) -> Result<User, AuthError> {
todo!()
}
- async fn create_oauth_session(&self) -> Result<String, AuthError> {
+ async fn create_oauth_session(&self) -> Result<SessionResponse, AuthError> {
let (auth_url, csrf_token) = self
.client
.authorize_url(CsrfToken::new_random)
@@ -42,7 +43,22 @@ impl OauthDriver for AuthServiceDiscord {
let mut session = Session::new();
session.insert(CSRF_TOKEN, &csrf_token).unwrap();
- Ok(String::default())
+ let cache_key = CacheKey::Session(session.id());
+ let mut cache = self.cache.get().await.unwrap();
+ cache
+ .set::<_, _, ()>(
+ cache_key,
+ serde_json::to_string(&session).or(Err(AuthError::InvalidSession))?,
+ )
+ .await?;
+ let cookie = session
+ .into_cookie_value()
+ .ok_or(AuthError::MissingSession)?;
+
+ Ok(SessionResponse {
+ cookie_value: cookie,
+ auth_url,
+ })
}
async fn save_session(&self, user: &User) -> Result<(), AuthError> {
todo!()