gpt4 book ai didi

angularjs - Firebase $authWithOAuthRedirect 不会在没有页面刷新的情况下调用 $onAuth

转载 作者:行者123 更新时间:2023-12-03 08:09:19 24 4
gpt4 key购买 nike

我按照 official tutorial 设置了一个简单的 Firebase Facebook OAuth 登录设置,如下所示用于 Ionic Firebase Facebook 登录。

问题是,一旦我用 Facebook 按钮点击登录,我就会被重定向到 Facebook,我登录,然后我被重定向回我的应用程序。但是,问题是我停留在登录页面。奇怪的是,如果我通过按 F5 物理刷新我的浏览器(使用 ionic serve 测试) onAuth触发器,我被重定向到 Items页。如果我不刷新浏览器,onAuth不会被调用。

有人有这个问题吗?你是怎么解决的?

请注意,是的,我做了我的研究(并且有点开始失去它),但无法让它工作。我搜索了SO,尝试使用$timeout从谷歌组,试图调用 $scope.$apply(),但都无济于事 - 所以请帮我找出我做错了什么?

.controller('AppCtrl', function($scope, Items, Auth, $state, $ionicHistory) {
$scope.login = function(provider) {
Auth.$authWithOAuthRedirect(provider).then(function(authData) {

}).catch(function(error) {
if (error.code === "TRANSPORT_UNAVAILABLE") {
Auth.$authWithOAuthPopup(provider).then(function(authData) {

});
} else {
console.log(error);
}
});
};

$scope.logout = function() {
Auth.$unauth();
$scope.authData = null;

$window.location.reload(true);
};

Auth.$onAuth(function(authData) {
if (authData === null) {
console.log("Not logged in yet");
$state.go('app.login');
} else {
console.log("Logged in as", authData.uid);

$ionicHistory.nextViewOptions({
disableBack: true
});
$state.go('app.items');
}
$scope.authData = authData; // This will display the user's name in our view
});
})
<ion-view view-title="Members area">
<ion-content padding="true">
<div ng-if="!authData">
<button class="button button-positive button-block" ng-click="login('facebook')">Log In with Facebook</button>
</div>

<div ng-if="authData.facebook">
<div class="card">
<div class="item item-text-wrap">Hello {{authData.facebook.displayName}}!</div>
</div>

<button class="button button-assertive button-block" ng-click="logout()">Log out</button>
</div>

</ion-content>
</ion-view>


编辑:我通过使用 $timeout 解决了它:
$timeout(function(){
Auth.$onAuth(function(authData) {
if (authData === null) {
console.log("Not logged in yet");
$state.go('app.login');
} else {
console.log("Logged in as", authData.uid);

$ionicHistory.nextViewOptions({
disableBack: true
});
$state.go('app.items');
}
$scope.authData = authData; // This will display the user's name in our view
});
}, 3000);

然而,这只是感觉不对(温和地说),必须有更好的方法,所以请提出一个。此外,我注意到高达 3 秒的延迟几乎不够(我发现的资源很少表明 500 毫秒就足够了,但在我的情况下,情况并非如此)。

最佳答案

发布在 github 问题上,但也会在这里回复。

这只发生在使用 $authWithOAuthRedirect 的浏览器中.该示例适用于cordova 应用程序,因此这真的不应该成为问题。

但是如果你真的需要它,你可以跳过重定向并使用弹出窗口。

    Auth.$authWithOAuthPopup(authMethod).then(function(authData) {
console.dir(authData);
});

关于angularjs - Firebase $authWithOAuthRedirect 不会在没有页面刷新的情况下调用 $onAuth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31872935/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com