Aak*_*kur 18 firebase firebase-authentication progressive-web-apps angular
我正在创建我的第一个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>
Run Code Online (Sandbox Code Playgroud)
TS:
logInGoogle(){
this.authService.loginWithGoogle().then( user => {
console.log("User is logged in");
//this.router.navigate(['/addweather']);
})
.catch( err => {
console.log('some error occured');
})
}
Run Code Online (Sandbox Code Playgroud)
这是服务:
loginWithGoogle() {
return this.afAuth.auth.signInWithPopup(new firebase.auth.GoogleAuthProvider());
}
Run Code Online (Sandbox Code Playgroud)
还要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(['']);
}
}
)
Run Code Online (Sandbox Code Playgroud)
现在,应用程序显示了一些行为:
此登录功能在浏览器上工作正常,如果我单击登录按钮,它会打开一个新窗口,授权我并返回打开该应用程序的同一选项卡.
当我将应用程序添加到主屏幕并尝试再次登录时,它会提示我以下选项:
一旦我点击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');
}
Run Code Online (Sandbox Code Playgroud)
现在,这不是通过选项提示,而是打开一个所需的对话框.但在授权后,这让我回到了登录页面.为什么会这样?当我已经检查auth状态app.component.ts并在用户获得授权时将用户重定向到主页时.
感谢耐心和阅读直到最后.非常感谢帮助.
编辑
正如叶夫根所建议的:试过signInWithRedirect.它在我第一次登录时有效,稍微延迟了2秒.但后来我退出并尝试再次登录,登录后出现空白屏幕.
小智 6
我的宠物网络应用程序上的行为几乎相同.为了我自己,我将通过以下步骤解决它:
app.module.ts @NgModule({
...
providers: [
{ provide: APP_INITIALIZER, useFactory: appConfig, deps: [AuthService], multi: true }
]
})
export function appConfig(authService) {
const app = firebase.initializeApp({
apiKey
authDomain
});
return () => new Promise((resolve, reject) => {
firebase.auth()
.onAuthStateChanged(data => {
if (data) {
firebase.auth().currentUser.getToken()
.then((token: string) => authService.setToken(token););
}
resolve(true);
}, error => resolve(true));
});
}Run Code Online (Sandbox Code Playgroud)
auth.guard.tsexport class AuthGuard implements CanActivate {
constructor(
private authService: AuthService,
private router: Router
) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if (this.authService.isAuthenticated()) {
return true;
} else {
this.router.navigate(['/signin']);
return false;
}
}
}Run Code Online (Sandbox Code Playgroud)
auth.service.tsexport class AuthService {
token: string;
signinUser(email: string, password: string) {
return new Promise((resolve, reject) => {
firebase.auth().signInWithEmailAndPassword(email, password)
.then(resp => {
firebase.auth().currentUser.getToken()
.then((token: string) => {
this.token = token;
resolve(resp);
}).catch(reject);
return resp;
})
.catch(reject);
});
}
signoutUser() {
return firebase.auth().signOut()
.then(resp => this.token = null);
}
getToken() {
firebase.auth().currentUser.getToken()
.then((token: string) => this.setToken(token));
return this.token;
}
setToken(token) {
this.token = token;
}
isAuthenticated() {
return this.token != null;
}
}Run Code Online (Sandbox Code Playgroud)
我希望它对你有所帮助.
看起来在移动设备上,您的应用程序不是在当前浏览器的新选项卡中打开身份验证,而是在新浏览器中打开身份验证,因此在通过 Google 进行身份验证后,它无法有效重定向回您的初始浏览器。如果您使用重定向登录,您将保留在同一浏览器中,因此您需要将服务更改为:
loginWithGoogle() {
return this.afAuth.auth.signInWithRedirect(new firebase.auth.GoogleAuthProvider());
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1776 次 |
| 最近记录: |