@smbcloud / cli / commits / 68fda2a

link github

Seto Elkahfi committed Sep 12, 2023 at 23:18 UTC 68fda2a5fe48fcb77762344da89ffce3c1882f31
2 files changed +65
smbpndk-cli/src/account/lib.rs
+10
@@ -36,6 +36,7 @@ pub enum ErrorCode {
36 EmailNotFound = 1000,
37 EmailUnverified = 1001,
38 PasswordNotSet = 1003,
39 + GithubNotLinked = 1004,
40 }
41
42 impl Display for ErrorCode {
@@ -44,10 +45,19 @@ impl Display for ErrorCode {
45 ErrorCode::EmailNotFound => write!(f, "Email not found."),
46 ErrorCode::EmailUnverified => write!(f, "Email not verified."),
47 ErrorCode::PasswordNotSet => write!(f, "Password not set."),
48 + ErrorCode::GithubNotLinked => write!(f, "Github not connected."),
49 }
50 }
51 }
52
53 +impl Copy for ErrorCode {}
54 +
55 +impl Clone for ErrorCode {
56 + fn clone(&self) -> Self {
57 + *self
58 + }
59 +}
60 +
61 #[derive(Debug, Serialize, Deserialize)]
62 pub struct GithubInfo {
63 pub id: i64,
smbpndk-cli/src/account/login/process.rs
+55
@@ -145,6 +145,7 @@ async fn process_authorization(auth: SmbAuthorization) -> Result<CommandResult>
145 let error = anyhow!("Password not set.");
146 return Err(error);
147 }
148 + ErrorCode::GithubNotLinked => return connect_github_account(auth).await,
149 }
150 }
151
@@ -263,6 +264,54 @@ async fn resend_email_verification(user: User) -> Result<CommandResult> {
264 }
265 }
266
267 +async fn connect_github_account(auth: SmbAuthorization) -> Result<CommandResult> {
268 + let confirm = Confirm::with_theme(&ColorfulTheme::default())
269 + .with_prompt("Do you want to link your GitHub account?")
270 + .interact()
271 + .unwrap();
272 +
273 + // Link GitHub account if user confirms
274 + if !confirm {
275 + let spinner = Spinner::new(
276 + spinners::Spinners::SimpleDotsScrolling,
277 + style("Cancel operation.").green().bold().to_string(),
278 + );
279 + return Ok(CommandResult {
280 + spinner,
281 + symbol: "✅".to_owned(),
282 + msg: "Doing nothing.".to_owned(),
283 + });
284 + }
285 +
286 + let spinner = Spinner::new(
287 + spinners::Spinners::SimpleDotsScrolling,
288 + style("Linking your GitHub account...")
289 + .green()
290 + .bold()
291 + .to_string(),
292 + );
293 +
294 + let response = Client::new()
295 + .post(build_smb_connect_github_url())
296 + .json(&auth)
297 + .header("Accept", "application/json")
298 + .header("Content-Type", "application/x-www-form-urlencoded")
299 + .send()
300 + .await?;
301 +
302 + match response.status() {
303 + reqwest::StatusCode::OK => Ok(CommandResult {
304 + spinner,
305 + symbol: "✅".to_owned(),
306 + msg: "GitHub account linked!".to_owned(),
307 + }),
308 + _ => {
309 + let error = anyhow!("Failed to link GitHub account.");
310 + Err(error)
311 + }
312 + }
313 +}
314 +
315 async fn login_with_email() -> Result<CommandResult> {
316 println!("Provide your login credentials.");
317 let username = Input::<String>::with_theme(&ColorfulTheme::default())
@@ -490,3 +539,9 @@ fn build_smb_reset_password_url() -> String {
539 url_builder.add_route("v1/users/password");
540 url_builder.build()
541 }
542 +
543 +fn build_smb_connect_github_url() -> String {
544 + let mut url_builder = smb_base_url_builder();
545 + url_builder.add_route("v1/link_github_account");
546 + url_builder.build()
547 +}
\ No newline at end of file