Skip to content

Instantly share code, notes, and snippets.

@goyox86
Created March 16, 2017 14:13
Show Gist options
  • Save goyox86/734f317dc4111d527a75eaa263031abf to your computer and use it in GitHub Desktop.
Save goyox86/734f317dc4111d527a75eaa263031abf to your computer and use it in GitHub Desktop.
// If I have this everything works
use schema::users::dsl::*;
use schema::users;
fn authenticate_user(db: &Db, user_email: &str, password: &str) -> EndpointResult<User> {
let conn = &*db.pool().get()?;
let user = match users.filter(users::email.eq(user_email)).first::<User>(conn) {
Ok(user) => user,
Err(DieselError::NotFound) => return Err(EndpointError::from(AuthError::Unauthorized)),
Err(err) => return Err(EndpointError::from(err))
};
match verify(password, user.hashed_password.as_ref().expect("hashed password should be there")) {
Ok(true) => Ok(user),
Ok(false) => Err(EndpointError::from(AuthError::Unauthorized)),
Err(err) => Err(EndpointError::from(AuthError::Encryption(err)))
}
}
/////////////////////////////////////////////////////
// But if i do this always returns me the first user
fn authenticate_user(db: &Db, email: &str, password: &str) -> EndpointResult<User> {
use schema::users::dsl::*;
use schema::users;
let conn = &*db.pool().get()?;
let user = match users.filter(users::email.eq(email)).first::<User>(conn) {
Ok(user) => user,
Err(DieselError::NotFound) => return Err(EndpointError::from(AuthError::Unauthorized)),
Err(err) => return Err(EndpointError::from(err))
};
match verify(password, user.hashed_password.as_ref().expect("hashed password should be there")) {
Ok(true) => Ok(user),
Ok(false) => Err(EndpointError::from(AuthError::Unauthorized)),
Err(err) => Err(EndpointError::from(AuthError::Encryption(err)))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment