/
auth.service.ts
150 lines (128 loc) 路 3.94 KB
/
auth.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { firebase } from '@firebase/app';
import { auth } from 'firebase';
import { AngularFireAuth } from 'angularfire2/auth';
import {
AngularFirestore,
AngularFirestoreDocument
} from 'angularfire2/firestore';
import { NotifyService } from './notify.service';
import { Observable, of } from 'rxjs';
import { switchMap, startWith, tap, filter } from 'rxjs/operators';
interface User {
uid: string;
email?: string | null;
photoURL?: string;
displayName?: string;
}
@Injectable()
export class AuthService {
user: Observable<User | null>;
constructor(
private afAuth: AngularFireAuth,
private afs: AngularFirestore,
private router: Router,
private notify: NotifyService
) {
this.user = this.afAuth.authState.pipe(
switchMap(user => {
if (user) {
return this.afs.doc<User>(`users/${user.uid}`).valueChanges();
} else {
return of(null);
}
})
// tap(user => localStorage.setItem('user', JSON.stringify(user))),
// startWith(JSON.parse(localStorage.getItem('user')))
);
}
////// OAuth Methods /////
googleLogin() {
const provider = new auth.GoogleAuthProvider();
return this.oAuthLogin(provider);
}
githubLogin() {
const provider = new auth.GithubAuthProvider();
return this.oAuthLogin(provider);
}
facebookLogin() {
const provider = new auth.FacebookAuthProvider();
return this.oAuthLogin(provider);
}
twitterLogin() {
const provider = new auth.TwitterAuthProvider();
return this.oAuthLogin(provider);
}
private oAuthLogin(provider: any) {
return this.afAuth.auth
.signInWithPopup(provider)
.then(credential => {
this.notify.update('Welcome to Firestarter!!!', 'success');
return this.updateUserData(credential.user);
})
.catch(error => this.handleError(error));
}
//// Anonymous Auth ////
anonymousLogin() {
return this.afAuth.auth
.signInAnonymously()
.then(credential => {
this.notify.update('Welcome to Firestarter!!!', 'success');
return this.updateUserData(credential.user); // if using firestore
})
.catch(error => {
this.handleError(error);
});
}
//// Email/Password Auth ////
emailSignUp(email: string, password: string) {
return this.afAuth.auth
.createUserWithEmailAndPassword(email, password)
.then(credential => {
this.notify.update('Welcome new user!', 'success');
return this.updateUserData(credential.user); // if using firestore
})
.catch(error => this.handleError(error));
}
emailLogin(email: string, password: string) {
return this.afAuth.auth
.signInWithEmailAndPassword(email, password)
.then(credential => {
this.notify.update('Welcome back!', 'success');
return this.updateUserData(credential.user);
})
.catch(error => this.handleError(error));
}
// Sends email allowing user to reset password
resetPassword(email: string) {
const fbAuth = auth();
return fbAuth
.sendPasswordResetEmail(email)
.then(() => this.notify.update('Password update email sent', 'info'))
.catch(error => this.handleError(error));
}
signOut() {
this.afAuth.auth.signOut().then(() => {
this.router.navigate(['/']);
});
}
// If error, console log and notify user
private handleError(error: Error) {
console.error(error);
this.notify.update(error.message, 'error');
}
// Sets user data to firestore after succesful login
private updateUserData(user: User) {
const userRef: AngularFirestoreDocument<User> = this.afs.doc(
`users/${user.uid}`
);
const data: User = {
uid: user.uid,
email: user.email || null,
displayName: user.displayName || 'nameless user',
photoURL: user.photoURL || 'https://goo.gl/Fz9nrQ'
};
return userRef.set(data);
}
}