Created
June 8, 2018 12:45
-
-
Save adash333/5fb8ef127dba5200f034a708c5afc233 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { HttpClient } from '@angular/common/http'; | |
import { Injectable } from '@angular/core'; | |
import 'rxjs/add/operator/map'; | |
import { Observable } from 'rxjs/Observable'; | |
import * as firebase from 'firebase'; | |
@Injectable() | |
export class AuthProvider { | |
constructor(public http: HttpClient) { | |
firebase.auth().onAuthStateChanged((user) => { | |
if (user) { | |
// User is signed in. | |
console.log('User is signed in'); | |
} else { | |
// No user is signed in. | |
console.log('User is NOT signed in'); | |
} | |
}); | |
} | |
/** | |
* Use Firebase Web API signInWithEmailAndPassword method | |
* to authenticate user login attempt | |
* | |
* @method loginWithEmailAndPassword | |
* @param email {string} | |
* @param password {string} | |
* @return {Promise} | |
*/ | |
loginWithEmailAndPassword( | |
email: string, | |
password: string | |
) { | |
return new Promise((resolve, reject) => { | |
firebase.auth().signInWithEmailAndPassword(email, password).then((val: any) => { | |
resolve(val); | |
}).catch((error: any) => { | |
reject(error); | |
}); | |
}); | |
} | |
/** | |
* Log out with Firebase Web API signOutmethod | |
* | |
* @method logOut | |
* @return {Promise} | |
*/ | |
logOut() { | |
return new Promise((resolve, reject) => { | |
firebase.auth().signOut().then(() => { | |
resolve(true); | |
}).catch((error: any) => { | |
reject(error); | |
}); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment