【发布时间】:2019-01-27 20:12:35
【问题描述】:
我使用 Firebase 的注销功能来注销登录的用户。我可以正确登录,但我显然无法注销。我已经对此进行了多次测试,但注销功能根本不起作用。任何和所有的帮助将不胜感激。以下是我的代码:
export class AuthService {
private user: Observable<firebase.User>;
private userDetails: firebase.User = null;
constructor(private _firebaseAuth: AngularFireAuth, private router: Router) {
this.user = _firebaseAuth.authState;
this.user.subscribe(
(user) => {
if (user) {
this.userDetails = user;
console.log(this.userDetails);
}
else {
this.userDetails = null;
}
}
);
}
signInWithGoogle() {
return this._firebaseAuth.auth.signInWithRedirect(
new firebase.auth.GoogleAuthProvider()
)
}
signup(email: string, password: string) {
this._firebaseAuth.auth.createUserWithEmailAndPassword(email, password)
.then(value => {
console.log('Success!', value);
})
.catch(err => {
console.log('Something went wrong:',err.message);
});
}
login(email: string, password: string) {
this._firebaseAuth.auth.signInWithEmailAndPassword(email, password)
.then(value => {
console.log('Nice, it worked!');
})
.catch(err => {
console.log('Something went wrong:',err.message);
});
}
isLoggedIn() {
if (this.userDetails == null ) {
return false;
} else {
return true;
}
}
directToNext() {
if (this.isLoggedIn){
this.router.navigate(['/or-items/1']);
}
}
logout() {
this._firebaseAuth.auth.signOut()
.then((res) => this.router.navigate(['/']));
}
}
然后在 HTML 中:
<script>
import {AuthService} from' ./../AuthService';
function logout() {
this.authService.logout();
}
function isLoggedIn() {
this.authService.isLoggedIn();
}
</script>
<span>
<button mat-raised-button color="basic" (click)="logout()">
logout
</button>
</span>
我知道用户未正确注销,因为 firebase 控制台显示该用户仍处于登录状态。
【问题讨论】:
-
不应该是
AuthService.logout();和AuthService.isLoggedIn()吗? -
如果没有,您可能需要将
AuthService添加到页面的构造函数/init,因为this.authService没有指向我可以看到的任何内容。
标签: html angular typescript firebase firebase-authentication