Advertisement
Guest User

Untitled

a guest
Mar 16th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. // If I have this everything works
  2.  
  3. use schema::users::dsl::*;
  4. use schema::users;
  5.  
  6. fn authenticate_user(db: &Db, user_email: &str, password: &str) -> EndpointResult<User> {
  7. let conn = &*db.pool().get()?;
  8.  
  9. let user = match users.filter(users::email.eq(user_email)).first::<User>(conn) {
  10. Ok(user) => user,
  11. Err(DieselError::NotFound) => return Err(EndpointError::from(AuthError::Unauthorized)),
  12. Err(err) => return Err(EndpointError::from(err))
  13. };
  14.  
  15. match verify(password, user.hashed_password.as_ref().expect("hashed password should be there")) {
  16. Ok(true) => Ok(user),
  17. Ok(false) => Err(EndpointError::from(AuthError::Unauthorized)),
  18. Err(err) => Err(EndpointError::from(AuthError::Encryption(err)))
  19. }
  20. }
  21. /////////////////////////////////////////////////////
  22. // But if i do this always returns me the first user
  23. fn authenticate_user(db: &Db, email: &str, password: &str) -> EndpointResult<User> {
  24. use schema::users::dsl::*;
  25. use schema::users;
  26.  
  27. let conn = &*db.pool().get()?;
  28.  
  29. let user = match users.filter(users::email.eq(email)).first::<User>(conn) {
  30. Ok(user) => user,
  31. Err(DieselError::NotFound) => return Err(EndpointError::from(AuthError::Unauthorized)),
  32. Err(err) => return Err(EndpointError::from(err))
  33. };
  34.  
  35. match verify(password, user.hashed_password.as_ref().expect("hashed password should be there")) {
  36. Ok(true) => Ok(user),
  37. Ok(false) => Err(EndpointError::from(AuthError::Unauthorized)),
  38. Err(err) => Err(EndpointError::from(AuthError::Encryption(err)))
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement