Guest User

Untitled

a guest
Jan 27th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. public login(loginData: IAuthLoginData): Promise<void> {
  2. let authenticationDetails = new AuthenticationDetails({
  3. Username: loginData.email,
  4. Password: loginData.password
  5. })
  6.  
  7. let cognitoUser = new CognitoUser({
  8. Username: loginData.email,
  9. Pool: this.userPool
  10. })
  11.  
  12. return new Promise<void>((resolve, reject) => {
  13. cognitoUser.authenticateUser(authenticationDetails, {
  14. onSuccess: (session: CognitoUserSession, userTrackingConfirmationNecessary: boolean) => {
  15. resolve()
  16. },
  17. onFailure: (err) => {
  18. switch (err.code) {
  19. case 'UserNotFoundException':
  20. reject(new UserNotFoundError())
  21.  
  22. case 'UserNotConfirmedException':
  23. reject(new UserNotConfirmedError())
  24.  
  25. case 'NotAuthorizedException':
  26. reject(new PasswordIncorrectError())
  27.  
  28. default:
  29. reject(new LoginError())
  30. }
  31. }
  32. })
  33. })
  34. }
  35.  
  36. export class UserNotFoundError extends Error {
  37. constructor(message?: string) {
  38. super(message);
  39.  
  40. // restore prototype chain
  41. const actualProto = new.target.prototype;
  42. if (Object.setPrototypeOf) { Object.setPrototypeOf(this, actualProto); }
  43. else { this.__proto__ = new.target.prototype; }
  44. }
  45. }
  46.  
  47. private login() {
  48. this.auth.login({
  49. email: this.email.value,
  50. password: this.password.value
  51. })
  52. .then(() => {
  53. this.navCtrl.setRoot(HomePage);
  54. })
  55. .catch((err: Error) => {
  56. if (err instanceof UserNotFoundError)
  57. this.email.setErrors({
  58. usernotfound: true
  59. })
  60. else if (err instanceof UserNotConfirmedError)
  61. this.form.setErrors({
  62. usernotconfirmed: true
  63. })
  64. else if (err instanceof PasswordIncorrectError)
  65. this.password.setErrors({
  66. passwordincorrect: true
  67. })
  68. else
  69. this.form.setErrors({
  70. ooops: true
  71. })
  72. })
  73. }
Add Comment
Please, Sign In to add comment