【发布时间】:2018-03-16 04:03:14
【问题描述】:
我正在创建我的第一个 Progressive Web 应用程序,它使用 firebase 来存储数据。我还使用Gmail 作为所有将使用该应用程序的用户的入口点。但是我坚持实现登录。以下是我的登录代码:
html:
<button md-raised-button color="warn" (click)="logInGoogle()"><md-icon>group</md-icon>Sign in with google to enjoy the app</button>
ts:
logInGoogle(){
this.authService.loginWithGoogle().then( user => {
console.log("User is logged in");
//this.router.navigate(['/addweather']);
})
.catch( err => {
console.log('some error occured');
})
}
这里是服务:
loginWithGoogle() {
return this.afAuth.auth.signInWithPopup(new firebase.auth.GoogleAuthProvider());
}
还要检查auth 状态,我在app.component.ts 构造函数中有这个:
this.authService.afAuth.authState.subscribe(
(auth) => {
if(auth === null){
console.log("Not Logged in.");
this.router.navigate(['login']);
this.isLoggedIn = false;
}
else {
console.log("Successfully Logged in.");
this.isLoggedIn = true;
this.router.navigate(['']);
}
}
)
现在应用程序显示了一些行为:
-
此登录功能在浏览器上运行良好,因为如果我单击登录按钮,它会打开一个新窗口、授权我并返回到打开应用程序的同一选项卡。
李> 当我将应用程序添加到主屏幕并尝试再次登录时,它会提示我输入以下选项:
单击 chrome 后,它会授权我并将我重定向到应用程序,但应用程序现在显示一个空白屏幕,并且 oAuth 屏幕只是进入无限处理状态。 为什么会发生这种情况?我的意思是它不应该像在浏览器中运行应用程序时那样正常工作。
同样在点击登录按钮时,它不应该提示如上图所示的选项;相反,它应该打开一个 oAuth 对话框。我也尝试使用以下代码执行此操作:
logInGoogle(){
var newWindow = window.open('https://accounts.google.com/o/oauth2/auth?scope=https://www.google.com/m8/feeds&client_id=9722-j3fstr5sh6pumie.apps.googleusercontent.com&redirect_uri=https://weatherapp321.firebaseapp.com/__/auth/handler&response_type=token', 'name', 'height=600,width=450');
}
现在这会打开一个所需的对话框,而不是提示选项。但是在授权之后,这让我回到了登录页面。 为什么会发生这种情况?当我已经在检查 app.component.ts 中的身份验证状态并在用户获得授权时将用户重定向到主页时。
感谢您耐心阅读到最后。非常感谢您的帮助。
编辑
正如叶夫根所建议的:
尝试使用signInWithRedirect。当我第一次登录时它工作了,有 2 秒的轻微延迟。但是后来我退出了,又想重新登录,登录后就黑屏了。
【问题讨论】:
标签: angular firebase firebase-authentication progressive-web-apps