Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public login(loginData: IAuthLoginData): Promise<void> {
- let authenticationDetails = new AuthenticationDetails({
- Username: loginData.email,
- Password: loginData.password
- })
- let cognitoUser = new CognitoUser({
- Username: loginData.email,
- Pool: this.userPool
- })
- return new Promise<void>((resolve, reject) => {
- cognitoUser.authenticateUser(authenticationDetails, {
- onSuccess: (session: CognitoUserSession, userTrackingConfirmationNecessary: boolean) => {
- resolve()
- },
- onFailure: (err) => {
- switch (err.code) {
- case 'UserNotFoundException':
- reject(new UserNotFoundError())
- case 'UserNotConfirmedException':
- reject(new UserNotConfirmedError())
- case 'NotAuthorizedException':
- reject(new PasswordIncorrectError())
- default:
- reject(new LoginError())
- }
- }
- })
- })
- }
- export class UserNotFoundError extends Error {
- constructor(message?: string) {
- super(message);
- // restore prototype chain
- const actualProto = new.target.prototype;
- if (Object.setPrototypeOf) { Object.setPrototypeOf(this, actualProto); }
- else { this.__proto__ = new.target.prototype; }
- }
- }
- private login() {
- this.auth.login({
- email: this.email.value,
- password: this.password.value
- })
- .then(() => {
- this.navCtrl.setRoot(HomePage);
- })
- .catch((err: Error) => {
- if (err instanceof UserNotFoundError)
- this.email.setErrors({
- usernotfound: true
- })
- else if (err instanceof UserNotConfirmedError)
- this.form.setErrors({
- usernotconfirmed: true
- })
- else if (err instanceof PasswordIncorrectError)
- this.password.setErrors({
- passwordincorrect: true
- })
- else
- this.form.setErrors({
- ooops: true
- })
- })
- }
Add Comment
Please, Sign In to add comment