Android Ionic Facebook身份验证重定向问题
我循序渐进地学习了本教程 我遇到的问题是,登录后,我的应用程序没有重定向到我需要的页面。在运行Android 9的模拟Pixel 2设备上测试时,控制台上没有错误。以下是处理身份验证的代码:Android Ionic Facebook身份验证重定向问题,android,facebook,cordova,ionic-framework,ionic4,Android,Facebook,Cordova,Ionic Framework,Ionic4,我循序渐进地学习了本教程 我遇到的问题是,登录后,我的应用程序没有重定向到我需要的页面。在运行Android 9的模拟Pixel 2设备上测试时,控制台上没有错误。以下是处理身份验证的代码: const permissions = ['public_profile', 'email']; await this.facebook.login(permissions).then(response => { this.uid = response.authResponse.userID
const permissions = ['public_profile', 'email'];
await this.facebook.login(permissions).then(response => {
this.uid = response.authResponse.userID;
this.facebook.api('/me?fields=name,email', permissions)
.then(user => {
user.picture = 'https://graph.facebook.com/' + userId +
'/picture?type=large';
this.dataService.getUser(this.uid).subscribe((data) => {
if (data.data() !== undefined) {
if (data.data().isNew) {
this.zone.run(() => {
this.router.navigate(['/profile']);
});
} else {
this.zone.run(() => {
this.router.navigate(['/home']);
});
}
} else {
this.dataService.addUser(....)}).then(() => {
this.zone.run(() => {
this.router.navigate(['/profile']);
});
}
});
});
我希望登录成功后,应用程序会使用路由器角度组件正确重定向。我会首先解开代码 检查日志 您可以使用Chrome查看正在发生的事情的输出并对其进行调试: 把一些日志放进去 在代码中放入一些console.log,如:
if (data.data() !== undefined) {
if (data.data().isNew) {
this.zone.run(() => {
console.log("routing to profile");
this.router.navigate(['/profile']);
});
} else {
this.zone.run(() => {
console.log("routing to home");
this.router.navigate(['/home']);
});
}
} else {
this.dataService.addUser(....)}).then(() => {
this.zone.run(() => {
console.log("adduser - routing to profile");
this.router.navigate(['/profile']);
});
}
教程本身有效吗?
去掉你的FirebasedataService
调用,看看它是否达到了这一点
你需要这个区域吗?
我认为这是Ionic4 beta版代码中的一些东西,但大多数情况下已经解决了。我不完全理解它是什么,但可能您正在学习过时的Firebase教程。问题原因
我想问题与我的角度火力有关:
this.afAuth.auth.onAuthStateChanged((user) => {
console.log('user state:', user);
if (user) {
this.fireUser = user;
this.deviceStorage.set('uid', user.uid);
this.dataService.userNewListener(user.uid);
} else {
this.deviceStorage.set('uid', '');
this.router.navigate(['/login']);
}
});
因为我使用的是原生解决方案(facebook cordova插件),observable不会改变,因为它将我重定向回登录
解决方案
解决方案是,在我使用插件成功执行登录后,我可以使用响应访问令牌作为凭证,继续使用AngularFire进行登录:
this.facebook.login(permissions).then(response => {
const facebookCredential = firebase.auth.FacebookAuthProvider.credential(response.authResponse.accessToken);
this.afAuth.auth.signInWithCredential(facebookCredential).then(() => {
console.log('navigating profile');
this.router.navigate(['/profile']);
});
});
我从提供的代码中删除了日志,以便于阅读。在模拟设备上测试时,Chrome控制台显示预期输出,但路由器导航仍不工作(在角度区域内或之外)。没有错误,只是没有导航到我需要它的页面:/您是否已注销路由器以查看它是否在那里<代码>控制台.log({router:this.router})代码>就在那里,我在该服务中有一系列其他功能,它们使用路由器在整个应用程序中导航到不同的页面。它位于服务的构造函数中。