From 7e4f73b695d514c6cfc2535f3f10c2afc27e07e4 Mon Sep 17 00:00:00 2001 From: kozabrada123 <59031733+kozabrada123@users.noreply.github.com> Date: Fri, 1 Sep 2023 15:17:49 +0000 Subject: [PATCH] Add option to login with only a token (#427) * Add login with token --- src/api/auth/mod.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/api/auth/mod.rs b/src/api/auth/mod.rs index 663bf5e..3ad4a60 100644 --- a/src/api/auth/mod.rs +++ b/src/api/auth/mod.rs @@ -1,5 +1,41 @@ +use std::sync::{Arc, RwLock}; + pub use login::*; pub use register::*; +use crate::{ + errors::ChorusResult, + gateway::Gateway, + instance::{ChorusUser, Instance}, + types::{GatewayIdentifyPayload, User}, +}; + pub mod login; pub mod register; + +impl Instance { + /// Logs into an existing account on the spacebar server, using only a token. + pub async fn login_with_token(&mut self, token: String) -> ChorusResult { + let object_result = self.get_user(token.clone(), None).await; + if let Err(e) = object_result { + return Result::Err(e); + } + + let user_settings = User::get_settings(&token, &self.urls.api, &mut self.clone()) + .await + .unwrap(); + let mut identify = GatewayIdentifyPayload::common(); + let gateway = Gateway::new(self.urls.wss.clone()).await.unwrap(); + identify.token = token.clone(); + gateway.send_identify(identify).await; + let user = ChorusUser::new( + Arc::new(RwLock::new(self.clone())), + token.clone(), + self.clone_limits_if_some(), + Arc::new(RwLock::new(user_settings)), + Arc::new(RwLock::new(object_result.unwrap())), + gateway, + ); + Ok(user) + } +}