Creating our Core Login API
For our core login API, we just must see it to believe it. We import the following:
// nanoservices/auth/dal/src/api/auth/login.rs
use auth_dal::users::transactions::get::GetByEmail;
use glue::errors::{NanoServiceError, NanoServiceErrorStatus};
use glue::token::HeaderToken;
And then our login API function is defined with the code below:
// nanoservices/auth/dal/src/api/auth/login.rs
pub async fn login<T: GetByEmail>(
email: String,
password: String
) -> Result<String, NanoServiceError> {
let user = T::get_by_email(email).await?;
let outcome = user.verify_password(password)?;
if outcome {
Ok(HeaderToken{
unique_id: user.unique_id
}.encode()?)
} else {
Err(NanoServiceError::new(
"Invalid password".to_string(),
NanoServiceErrorStatus::Unauthorized
))
}
}
And here, the true power of our approach starts to become apparent. Because...