Ionic Facebook 身份验证重定向问题
Ionic Facebook Authentication Redirection Problem
我一步一步跟着这个教程走的https://ionicthemes.com/tutorials/about/ionic-facebook-login
我遇到的问题是登录后,我的应用程序没有重定向到我需要的页面。在模拟的 Pixel 2 设备上测试时,控制台上没有错误 运行 Android 9 。这是处理身份验证的代码:
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']);
});
}
});
});
我希望在登录成功后使用路由器 angular 组件正确重定向应用程序。
我将从解开代码开始。
查看日志
您可以使用 Chrome 查看发生的情况的输出并对其进行调试:
Using Chrome DevTools - Android Development - Ionic Documentation
在那里放一些日志
在代码中添加一些 console.logs,例如:
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']);
});
}
教程本身有效吗?
去掉你的 Firebase dataService
调用,看看它是否达到那个点。
你需要区域吗?
我认为这是 Ionic4 测试版代码中的一些内容,但大部分已解决。我不完全理解它是什么,但也许您正在学习过时的 Firebase 教程。
问题原因
我认为问题与我的 Angular Fire Auth Observable 有关:
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']);
});
});
我一步一步跟着这个教程走的https://ionicthemes.com/tutorials/about/ionic-facebook-login
我遇到的问题是登录后,我的应用程序没有重定向到我需要的页面。在模拟的 Pixel 2 设备上测试时,控制台上没有错误 运行 Android 9 。这是处理身份验证的代码:
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']);
});
}
});
});
我希望在登录成功后使用路由器 angular 组件正确重定向应用程序。
我将从解开代码开始。
查看日志
您可以使用 Chrome 查看发生的情况的输出并对其进行调试:
Using Chrome DevTools - Android Development - Ionic Documentation
在那里放一些日志
在代码中添加一些 console.logs,例如:
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']);
});
}
教程本身有效吗?
去掉你的 Firebase dataService
调用,看看它是否达到那个点。
你需要区域吗?
我认为这是 Ionic4 测试版代码中的一些内容,但大部分已解决。我不完全理解它是什么,但也许您正在学习过时的 Firebase 教程。
问题原因
我认为问题与我的 Angular Fire Auth Observable 有关:
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']);
});
});