|
| 1 | +import { Injectable } from '@angular/core'; |
| 2 | +import { HttpClient } from '@angular/common/http'; |
| 3 | +import { catchError ,tap} from 'rxjs/operators'; |
| 4 | +import { throwError, BehaviorSubject } from 'rxjs'; |
| 5 | +import {User} from './user.model'; |
| 6 | + |
| 7 | +const FIREBASE_API_KEY = 'AIzaSyATF_8gCXQ5VuUuMkRJRo7JI4_ySFFiexM';//settings->webAPI |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +interface AuthResponseData { |
| 14 | + kind: string; |
| 15 | + idToken: string; |
| 16 | + email: string; |
| 17 | + refreshToken: string; |
| 18 | + expiresIn: string; |
| 19 | + localId: string; |
| 20 | + registered?: boolean; |
| 21 | + } |
| 22 | + |
| 23 | +@Injectable({ providedIn: 'root' }) |
| 24 | +export class AuthService { |
| 25 | + |
| 26 | + private _user=new BehaviorSubject<User>(null); |
| 27 | + constructor(private http: HttpClient) {} |
| 28 | + |
| 29 | + signUp(email: string, password: string) { |
| 30 | + return this.http //add return so that you can subscribe in comp.ts file |
| 31 | + .post<AuthResponseData>( |
| 32 | + ` https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=${FIREBASE_API_KEY}`, |
| 33 | + { email: email, password: password, returnSecureToken: true } |
| 34 | + ).pipe(catchError(errRes=>{ |
| 35 | + this.handleError(errRes.error.error.message); |
| 36 | + return throwError(errRes); |
| 37 | + }),tap(resData => { |
| 38 | + if (resData && resData.idToken) { |
| 39 | + |
| 40 | + this.handleLogin( |
| 41 | + email, |
| 42 | + resData.idToken, |
| 43 | + resData.localId, |
| 44 | + parseInt(resData.expiresIn) |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + }) ); |
| 49 | + /*.subscribe(resData => { |
| 50 | + console.log(resData); |
| 51 | + }); remove this bec we weill subscribe after successful llogin and signup in comp.ts */ |
| 52 | + } |
| 53 | + get user(){ |
| 54 | + return this._user.asObservable(); |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + login(email: string, password: string) { |
| 60 | + return this.http |
| 61 | + .post<AuthResponseData>( |
| 62 | + `https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=${FIREBASE_API_KEY}`, |
| 63 | + { email: email, password: password, returnSecureToken: true } |
| 64 | + ).pipe(catchError(errRes=>{ |
| 65 | + |
| 66 | + this.handleError(errRes.error.error.message); |
| 67 | + return throwError(errRes);}),tap(resData => { |
| 68 | + if (resData && resData.idToken) { |
| 69 | + |
| 70 | + this.handleLogin( |
| 71 | + email, |
| 72 | + resData.idToken, |
| 73 | + resData.localId, |
| 74 | + parseInt(resData.expiresIn) |
| 75 | + ); |
| 76 | + |
| 77 | + // paste in handleLogin const expirydate= new Date(new Date().getTime()+parseInt(resData.expiresIn)*(1000)); |
| 78 | + //const user=new User(email,resData.localId,resData.idToken,expirydate); |
| 79 | + //this._user.next(user);//const user wala user |
| 80 | + } |
| 81 | + |
| 82 | + } |
| 83 | + )) |
| 84 | + /* .subscribe(resData => { |
| 85 | + console.log(resData); |
| 86 | + });*/ |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | + private handleLogin( |
| 93 | + email: string, |
| 94 | + token: string, |
| 95 | + userId: string, |
| 96 | + expiresIn: number |
| 97 | + ) { |
| 98 | + const expirationDate = new Date(new Date().getTime() + expiresIn * 1000); |
| 99 | + const user = new User(email, userId, token, expirationDate); |
| 100 | + this._user.next(user); |
| 101 | + } |
| 102 | + |
| 103 | +private handleError (errorMessage:string){ |
| 104 | + |
| 105 | + |
| 106 | +switch(errorMessage){ |
| 107 | + case 'EMAIL-EXISTS': |
| 108 | + alert('email already exists'); |
| 109 | + break; |
| 110 | + case 'INVALID-PASSWORD': |
| 111 | + alert('Password is incorrect'); |
| 112 | + default: |
| 113 | + alert('enter correct information'); |
| 114 | +} |
| 115 | + |
| 116 | +} |
| 117 | + |
| 118 | +} |
0 commit comments