src/app/core/auth.service.ts
Properties |
Methods |
|
constructor(afAuth: AngularFireAuth, afs: AngularFirestore, notify: NotifyService)
|
||||||||||||
|
Defined in src/app/core/auth.service.ts:22
|
||||||||||||
|
Parameters :
|
| anonymousLogin |
anonymousLogin()
|
|
Defined in src/app/core/auth.service.ts:65
|
|
匿名登陆
Returns :
any
|
| emailLogin |
emailLogin(email: string, password: string)
|
|
Defined in src/app/core/auth.service.ts:80
|
|
邮箱登陆
Returns :
any
|
| emailSignUp |
emailSignUp(email: string, password: string)
|
|
Defined in src/app/core/auth.service.ts:94
|
|
邮箱注册
Returns :
any
|
| facebookLogin |
facebookLogin()
|
|
Defined in src/app/core/auth.service.ts:57
|
|
Returns :
any
|
| githubLogin |
githubLogin()
|
|
Defined in src/app/core/auth.service.ts:42
|
|
Returns :
any
|
| googleLogin |
googleLogin()
|
|
Defined in src/app/core/auth.service.ts:47
|
|
Returns :
any
|
| Private handleError | ||||||
handleError(error: Error)
|
||||||
|
Defined in src/app/core/auth.service.ts:147
|
||||||
|
Parameters :
Returns :
void
|
| Private oAuthLogin | ||||||
oAuthLogin(provider: any)
|
||||||
|
Defined in src/app/core/auth.service.ts:122
|
||||||
|
Parameters :
Returns :
any
|
| resetPassword | ||||||||
resetPassword(email: string)
|
||||||||
|
Defined in src/app/core/auth.service.ts:107
|
||||||||
|
重置密码
Parameters :
Returns :
any
|
| signOut |
signOut()
|
|
Defined in src/app/core/auth.service.ts:118
|
|
退出登录
Returns :
any
|
| twitterLogin |
twitterLogin()
|
|
Defined in src/app/core/auth.service.ts:52
|
|
Returns :
any
|
| Private updateUserData | ||||||
updateUserData(user: User)
|
||||||
|
Defined in src/app/core/auth.service.ts:132
|
||||||
|
Parameters :
Returns :
any
|
| user |
Type : Observable<User | null>
|
|
Defined in src/app/core/auth.service.ts:22
|
import { Injectable } from '@angular/core';
import { AngularFireAuth } from '@angular/fire/auth';
import {
AngularFirestore,
AngularFirestoreDocument
} from '@angular/fire/firestore';
import { auth } from 'firebase/app';
import { Observable, of } from 'rxjs';
import { switchMap, startWith, tap, filter } from 'rxjs/operators';
import { NotifyService } from './notify.service';
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 notify: NotifyService
) {
this.user = this.afAuth.authState.pipe(
switchMap(user => {
if (user) {
return this.afs.doc(`users/${user.uid}`).valueChanges();
} else {
return of(null);
}
})
// tap(user => localStorage.setItem('user', JSON.stringify(user))),
// startWith(JSON.parse(localStorage.getItem('user')))
);
}
githubLogin() {
const provider = new auth.GithubAuthProvider();
return this.oAuthLogin(provider);
}
googleLogin() {
const provider = new auth.GoogleAuthProvider();
return this.oAuthLogin(provider);
}
twitterLogin() {
const provider = new auth.TwitterAuthProvider();
return this.oAuthLogin(provider);
}
facebookLogin() {
const provider = new auth.FacebookAuthProvider();
return this.oAuthLogin(provider);
}
/**
* 匿名登陆
*/
anonymousLogin() {
return this.afAuth.auth
.signInAnonymously()
.then(credential => {
this.notify.update('欢迎回来!', 'success');
return this.updateUserData(credential.user);
})
.catch(error => this.handleError(error));
}
/**
* 邮箱登陆
* @param email
* @param password
*/
emailLogin(email: string, password: string) {
return this.afAuth.auth
.signInWithEmailAndPassword(email, password)
.then(credential => {
this.notify.update('欢迎回来!', 'success');
return this.updateUserData(credential.user);
});
}
/**
* 邮箱注册
* @param email 邮箱
* @param password 密码
*/
emailSignUp(email: string, password: string) {
return this.afAuth.auth
.createUserWithEmailAndPassword(email, password)
.then(credential => {
this.notify.update('注册成功!', 'success');
return this.updateUserData(credential.user);
});
}
/**
* 重置密码
* @param email 邮箱
*/
resetPassword(email: string) {
const fbAuth = auth();
return fbAuth
.sendPasswordResetEmail(email)
.then(() => this.notify.update('密码已发到你的邮箱中!', 'info'))
.catch(error => this.handleError(error));
}
/**
* 退出登录
*/
signOut() {
return this.afAuth.auth.signOut();
}
private oAuthLogin(provider: any) {
return this.afAuth.auth
.signInWithPopup(provider)
.then(credential => {
this.notify.update('欢迎回来!', 'success');
return this.updateUserData(credential.user);
})
.catch(error => this.handleError(error));
}
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 || '佚名',
photoURL: user.photoURL
};
return userRef.set(data);
}
private handleError(error: Error) {
console.log(error);
this.notify.update(error.message, 'error');
}
}