Skip to content

Commit 060df7e

Browse files
authored
fix: ensure user is initialized after login (#38)
1 parent 3f71a05 commit 060df7e

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

rustfmt.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
edition = "2015"
2+
wrap_comments = true

src/api/account.rs

+28
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,34 @@ where
3030
Ok(response)
3131
}
3232

33+
pub async fn initialize_user(access_token: &str) -> Result<String, Error> {
34+
let url = format!("{}/users", build_api_url());
35+
36+
let mut sp = Spinner::new(Spinners::Dots, "".into());
37+
38+
let client = Client::new();
39+
let resp = client
40+
.post(url)
41+
.header("Authorization", format!("Bearer {}", access_token))
42+
.header("agent", build_agent_header())
43+
.send()
44+
.await?;
45+
46+
sp.stop_with_symbol("".into());
47+
48+
check_response_update_header(&resp)?;
49+
let response = resp.json::<serde_json::Value>().await?;
50+
51+
let userid = response
52+
.as_object()
53+
.and_then(|o| o.get("userId"))
54+
.and_then(|x| x.as_str())
55+
.unwrap_or_default()
56+
.to_owned();
57+
58+
Ok(userid)
59+
}
60+
3361
pub async fn create_api_key(
3462
access_token: &str,
3563
project: &str,

src/init/login.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
use miette::bail;
1+
use miette::{bail, Context as _, IntoDiagnostic as _};
22
use reqwest::StatusCode;
33
use serde::{Deserialize, Serialize};
44
use std::{collections::HashMap, time::Duration};
55

6+
use crate::api;
7+
68
async fn find_login_url() -> (String, String) {
79
let mut params = HashMap::new();
810
params.insert("client_id", "gpJ63MG5g1V1PKufM9WHGjjeAe7yCT8L");
@@ -94,6 +96,11 @@ pub async fn run() -> miette::Result<String> {
9496
let (status, access_token) = poll_token(&device_code).await;
9597

9698
if status.is_success() {
99+
api::account::initialize_user(&access_token)
100+
.await
101+
.into_diagnostic()
102+
.context("initializing user")?;
103+
97104
println!("login successful!");
98105
return Ok(access_token);
99106
}

0 commit comments

Comments
 (0)