Represent invalid tokens with an error instead of a boolean
This commit is contained in:
parent
5f6c5f5375
commit
b4c4226677
|
@ -27,6 +27,10 @@ pub enum Error {
|
||||||
#[error("HTTP error")]
|
#[error("HTTP error")]
|
||||||
Http(#[source] Box<ureq::Error>),
|
Http(#[source] Box<ureq::Error>),
|
||||||
|
|
||||||
|
/// The token that was attempted to be used for authentication is invalid.
|
||||||
|
#[error("invalid authentication token")]
|
||||||
|
InvalidToken,
|
||||||
|
|
||||||
/// Tried to access a service that requires authentication.
|
/// Tried to access a service that requires authentication.
|
||||||
#[error("not authenticated")]
|
#[error("not authenticated")]
|
||||||
NotAuthenticated,
|
NotAuthenticated,
|
||||||
|
|
|
@ -46,22 +46,23 @@ impl ListenBrainz {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Authenticate this client with the given token.
|
/// Authenticate this client with the given token.
|
||||||
/// If the token is valid, authenticates the client and returns true, otherwise returns false.
|
/// If the token is valid, authenticates the client and returns [`Ok`].
|
||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
///
|
///
|
||||||
|
/// If the token was invalid, returns [`Error::InvalidToken`].
|
||||||
/// If there was an error while validating the token, that error is returned.
|
/// If there was an error while validating the token, that error is returned.
|
||||||
/// See the Errors section of [`Client`] for more info on what errors might occur.
|
/// See the Errors section of [`Client`] for more info on what errors might occur.
|
||||||
pub fn authenticate(&mut self, token: &str) -> Result<bool, Error> {
|
pub fn authenticate(&mut self, token: &str) -> Result<(), Error> {
|
||||||
let result = self.client.validate_token(token)?;
|
let result = self.client.validate_token(token)?;
|
||||||
if result.valid && result.user_name.is_some() {
|
if result.valid && result.user_name.is_some() {
|
||||||
self.auth.replace(Auth {
|
self.auth.replace(Auth {
|
||||||
token: token.to_string(),
|
token: token.to_string(),
|
||||||
user: result.user_name.unwrap(),
|
user: result.user_name.unwrap(),
|
||||||
});
|
});
|
||||||
return Ok(true);
|
return Ok(());
|
||||||
}
|
}
|
||||||
Ok(false)
|
Err(Error::InvalidToken)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Helper method to submit a listen (either "single" or "playing now").
|
/// Helper method to submit a listen (either "single" or "playing now").
|
||||||
|
|
Loading…
Reference in New Issue