Advertisement
Amirul93

auth.service.ts

Dec 7th, 2020
679
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   async setUserData(user) {
  2.     const userToken = user.getIdToken().then((token) => {
  3.       return token;
  4.     });
  5.  
  6.     const userData = new User(user.uid, user.email, user.displayName, user.photoURL, userToken);
  7.  
  8.     if (this.platform.is('capacitor')) Plugins.Storage.set({ key: 'authData', value: JSON.stringify(userData) });
  9.   }
  10.  
  11.   // Handle login with redirect for web Google auth
  12.   private async handleRedirect() {
  13.     if (await this.isRedirect()) {
  14.       return null;
  15.     }
  16.     const loading = await this.loadingController.create();
  17.     await loading.present();
  18.  
  19.     const result = await this.afAuth.getRedirectResult();
  20.  
  21.     if (result.user) {
  22.       await this.setUserData(result);
  23.     }
  24.  
  25.     await loading.dismiss();
  26.  
  27.     await this.setRedirect(false);
  28.  
  29.     return result;
  30.   }
  31.  
  32.   autoLogin() {
  33.     return from(Plugins.Storage.get({ key: 'authData' })).pipe(
  34.       map((storedData) => {
  35.         if (!storedData || !storedData.value) return null;
  36.         const parsedData = JSON.parse(storedData.value) as User;
  37.  
  38.         const user = new User(
  39.           parsedData.uid,
  40.           parsedData.email,
  41.           parsedData.token,
  42.           parsedData.photoURL,
  43.           parsedData.token
  44.         );
  45.         return user;
  46.       }),
  47.       tap((user) => {
  48.         if (user) this.user$.next(user);
  49.       }),
  50.       map((user) => {
  51.         return !!user;
  52.       })
  53.     );
  54.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement