Guest User

Untitled

a guest
Aug 30th, 2018
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. interface Session {
  2. }
  3.  
  4. interface User {
  5. email: string,
  6. lastLoginAt: Date,
  7. session: Session,
  8.  
  9. numLoginAttempts: number,
  10.  
  11. /** return true if the passed in password is valid for this User */
  12. checkPassword: (p: String) => boolean,
  13.  
  14. /** returns the updated user with a new session */
  15. generateSession: () => User,
  16.  
  17. /** updates the lastLoginAt date and returns the updated User */
  18. updateLastLogin: () => User,
  19.  
  20. /** sets the number of login attempts, returns the updated User */
  21. setNumLoginAttempts: (attempts: number) => User,
  22.  
  23. }
  24.  
  25. interface UserService {
  26.  
  27. /** returns the User matching the email, otherwise returns null */
  28. getUserByEmail: (email: String) => User | null
  29. }
  30.  
  31. namespace RouteImpls {
  32.  
  33. const userService: UserService;
  34.  
  35. /**
  36. * Logs a user in
  37. *
  38. * Requirements:
  39. * - password must be correct
  40. * - can try up to 3 times
  41. * - updates lastloginAt date
  42. * - returns a new session
  43. * @param email
  44. * @param password
  45. */
  46. function signin(email: string, password: string): Session {
  47. let user = userService.getUserByEmail(email);
  48. user = user.setNumLoginAttempts(user.numLoginAttempts + 1);
  49.  
  50. if (user.numLoginAttempts >= 3) {
  51. throw new Error("no more login attempts");
  52. }
  53.  
  54. if (user.checkPassword(password)) {
  55. user.updateLastLogin();
  56. user.generateSession();
  57. return user.session;
  58. }
  59.  
  60. throw new Error("invalid email or password");
  61. }
  62.  
  63. }
Add Comment
Please, Sign In to add comment