Skip to content

Commit

Permalink
Add auth token struct
Browse files Browse the repository at this point in the history
  • Loading branch information
sirkrypt0 committed Apr 18, 2021
1 parent 5338c98 commit 1d1032f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
11 changes: 9 additions & 2 deletions server/src/user/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ pub enum AuthenticationError {
DatabaseError(UserError),
}

#[derive(Serialize, Deserialize, Debug)]
pub struct AuthToken {
pub token: String,
}

impl User {
pub fn generate_password(clear_password: &str) -> String {
bcrypt::hash(clear_password).unwrap()
Expand All @@ -23,7 +28,7 @@ impl User {
pub fn authenticate(
conn: &PgConnection,
user_data: UserData,
) -> Result<String, AuthenticationError> {
) -> Result<AuthToken, AuthenticationError> {
let user = User::_find_by_username(&conn, &*user_data.username);
let user = match user {
Err(e) => return Err(DatabaseError(e)),
Expand All @@ -34,7 +39,9 @@ impl User {
Some(u) => u,
};
if user.check_password(&*user_data.password) && user_data.username == user.username {
Ok(String::from("AUTH_TOKEN_NOT_IMPLEMENTED"))
Ok(AuthToken {
token: String::from("AUTH_TOKEN_NOT_IMPLEMENTED"),
})
} else {
Err(AuthenticationError::IncorrectPassword)
}
Expand Down
2 changes: 1 addition & 1 deletion server/src/user/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub async fn authenticate(
let conn = pool.get().expect("couldn't get db connection from pool");
let auth_token = web::block(move || User::authenticate(&conn, user_data.into_inner())).await;
match auth_token {
Ok(token) => Ok(HttpResponse::Ok().json(json!({ "token": token }))),
Ok(token) => Ok(HttpResponse::Ok().json(token)),
Err(e) => match e {
BlockingError::Error(e) => Err(ServiceError::from(e)),
BlockingError::Canceled => Err(ServiceError::InternalServerError),
Expand Down

0 comments on commit 1d1032f

Please sign in to comment.