SHARE
TWEET

Untitled

a guest Jan 25th, 2019 142 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Injectable } from '@angular/core';
  2. import { from, Observable, throwError } from 'rxjs';
  3.  
  4. import { Auth } from 'aws-amplify';
  5. import { catchError, map } from 'rxjs/operators';
  6.  
  7. @Injectable({
  8.   providedIn: 'root'
  9. })
  10. export class CognitoService {
  11.   public signUp(username: string, password: string): Observable<any> {
  12.     return from(
  13.       Auth.signUp({
  14.         username,
  15.         password
  16.       })
  17.     ).pipe(catchError(error => throwError(error)));
  18.   }
  19.  
  20.   public signUpWithAttributes(
  21.     username: string,
  22.     password: string,
  23.     attributes: any
  24.   ): Observable<any> {
  25.     return from(
  26.       Auth.signUp({
  27.         username,
  28.         password,
  29.         attributes
  30.       })
  31.     ).pipe(catchError(error => throwError(error)));
  32.   }
  33.  
  34.   public confirmEmail(username: string, code: string) {
  35.     return from(Auth.confirmSignUp(username, code)).pipe(
  36.       catchError(error => throwError(error))
  37.     );
  38.   }
  39.  
  40.   public signIn(username: string, password: string): Observable<any> {
  41.     return from(Auth.signIn(username, password)).pipe(
  42.       catchError(error => throwError(error))
  43.     );
  44.   }
  45.  
  46.   public completeNewPassowrd(user: any, newPassword: string): Observable<any> {
  47.     return from(
  48.       Auth.completeNewPassword(
  49.         user, // Cognito User Object
  50.         newPassword, // New password
  51.         {}
  52.       )
  53.     ).pipe(catchError(error => throwError(error)));
  54.   }
  55.  
  56.   public getSession() {
  57.     return from(Auth.currentSession()).pipe(
  58.       catchError(error => throwError(error))
  59.     );
  60.   }
  61.  
  62.   public getAccessToken() {
  63.     return from(Auth.currentSession()).pipe(
  64.       map((session: any) => {
  65.         return session.getAccessToken();
  66.       }),
  67.       catchError(error => throwError(error))
  68.     );
  69.   }
  70.  
  71.   public signOut() {
  72.     return from(Auth.signOut()).pipe(catchError(error => throwError(error)));
  73.   }
  74.  
  75.   public signOutGlobal() {
  76.     // By doing this, you are revoking all the auth tokens(id token, access token and refresh token)
  77.     // which means the user is signed out from all the devices
  78.     // Note: although the tokens are revoked, the AWS credentials will remain valid until they expire (which by default is 1 hour)
  79.     return from(Auth.signOut({ global: true })).pipe(
  80.       catchError(error => throwError(error))
  81.     );
  82.   }
  83. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top