Skip to content

Commit

Permalink
#13 Resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
harrisonhoward committed May 17, 2024
1 parent fb2386e commit 8268de3
Showing 1 changed file with 31 additions and 41 deletions.
72 changes: 31 additions & 41 deletions src/commands/auth.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn do(_input: CommandInput) {
case env.is_base_config_valid() {
False -> {
io.println(
"You have no setup the config yet! Please run `plexrpc config` first.",
"You have not setup the config yet! Please run `plexrpc config` first.",
)
terminal.exit()
}
Expand All @@ -39,26 +39,21 @@ fn do(_input: CommandInput) {
default: Some(False),
)
{
True -> {
// Spacing out the terminal if the prompt was sent
io.println("")
}
// Spacing out the terminal if the prompt was sent
True -> io.println("")
False -> terminal.exit()
}

_ -> Nil
}

// Generate the client identifier
let assert Ok(client_id) = client_identifier.generate()

// We need to create the Plex pin and inform the user of the next steps
let create_pin_response =
let assert Ok(created_pin) =
plex_pin_auth.create_pin(client_id)
|> handle_plex_error

// Extract the ok response since we know it's okay to go now :)
let assert Ok(created_pin) = create_pin_response
// Inform the user what the code is and what to do with it
io.println(
"Successfully created your Plex pin as "
Expand All @@ -72,42 +67,37 @@ fn do(_input: CommandInput) {
)

// Setup repeatedly so we can poll the server for the token
repeatedly.call(
// For now only poll every 2 seconds
interval_ms,
Nil,
fn(_, _) {
let token_response =
plex_pin_auth.get_token(client_id, created_pin.id)
|> handle_plex_error
repeatedly.call(interval_ms, Nil, fn(_, _) {
// Poll the server for the token
let assert Ok(token) =
plex_pin_auth.get_token(client_id, created_pin.id)
|> handle_plex_error

let assert Ok(token) = token_response
case token.auth_token {
Some(auth_token) -> {
// Save the token to the config
case
env.set_config(ConfigOption(
hostname: None,
port: None,
username: None,
https: None,
check_users: None,
token: Some(auth_token),
))
{
Ok(_) -> io.println("Successfully authenticated with Plex!")
Error(err) -> {
io.println("Failed to save the token to the config")
io.debug(err)
Nil
}
case token.auth_token {
// If the token is returned then we can save it to the config
Some(auth_token) -> {
case
env.set_config(ConfigOption(
hostname: None,
port: None,
username: None,
https: None,
check_users: None,
token: Some(auth_token),
))
{
Ok(_) -> io.println("Successfully authenticated with Plex!")
Error(err) -> {
io.println("Failed to save the token to the config")
io.debug(err)
Nil
}
terminal.exit()
}
None -> Nil
terminal.exit()
}
},
)
None -> Nil
}
})

// Sleep for the time it takes for the pin to expire
terminal.sleep(created_pin.expires_in * 1000)
Expand Down

0 comments on commit 8268de3

Please sign in to comment.