Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Injectable } from '@angular/core';
- import { Router } from '@angular/router';
- import { AngularFireAuth } from 'angularfire2/auth';
- import { AngularFireDatabase } from 'angularfire2/database';
- import * as firebase from 'firebase/app';
- import { Observable } from 'rxjs/Observable';
- @Injectable({
- providedIn: 'root'
- })
- export class AuthService {
- private user: Observable<firebase.User>;
- private authState: any;
- userId: string;
- signupAttempt: number;
- userDetails: any;
- constructor(private afAuth: AngularFireAuth,
- private db: AngularFireDatabase,
- private router: Router) {
- this.user = afAuth.authState;
- }
- authenticateUser() {
- return this.user;
- }
- getUserDetails(uid: string) {
- const path = `users/${uid}`;
- return this.db.object(path);
- }
- login(email: string, password: string) {
- return this.afAuth.auth.signInWithEmailAndPassword(email, password)
- .then((user) => {
- this.authState = user;
- this.signupAttempt = 1;
- this.setUserSatus('online');
- this.getUserDetails(user.uid);
- this.router.navigate(['chat']);
- window.location.reload();
- });
- }
- get currentUserId(): string {
- if (this.signupAttempt === 1) {
- this.signupAttempt = 0;
- return this.authState !== null ? this.authState.uid : ' ';
- } else {
- this.userId = sessionStorage.getItem('userId');
- return this.authState !== null ? this.userId : this.authState.uid;
- }
- }
- setUserSatus(status: string) {
- const path = `users/${this.currentUserId}`;
- const data = {
- status: status
- };
- this.db.object(path).update(data);
- }
- }
Add Comment
Please, Sign In to add comment