1

Firebase AngularJS アプリケーションがあり、これを使用しFirebaseSimpleLoginて認証を実行していますが、アプリケーション全体でログイン ユーザーを永続化する方法を理解するのに少し苦労しています。ログインしてページを更新するたびに、認証されていないように見えます。ユーザーの認証に使用されるログイン後にトークンが生成されることは理解していますが、各ページでこのトークンを使用してユーザーを認証する必要がありますか? その場合、アプリケーションと Firebase の間で何らかのオーバーヘッドが発生しますか?

ログイン/認証を実行するアプリのセクション

var ref = new Firebase("https://xxx.firebaseio.com/");
angularFireAuth.initialize(ref, {scope: $scope, name: "user", path:"/login"});

    // Used to authenticate user at a later time
$scope.auth = function(token, callback)
{

    ref.auth(token, function(error, data) {

        if(error) 
        {
            console.log("Login Failed!", error);
        } 
        else 
        {
            $scope.$apply(function()
            {
                $scope.user = data.auth;
            }); 
        }

        if (typeof callback === 'function')
        {
            callback(error,data);
        }

    });
};


    // Login to fetch authentication token to be used by $scope.auth()
$scope.login = function()
{   
    angularFireAuth.login("password", { 
        email: $scope.credentials.email,
        password: $scope.credentials.password
    });
};


    // On login stores user within $scope
$scope.$on("angularFireAuth:login", function(evt, user) {
    $scope.$apply(function()
    {
                 // Is it possible to save this user accross my application not just in this $scope
             $scope.user = user; 

    });
});

これは、電子メールとパスワードの組み合わせをキャプチャするマークアップです

<div class="container-single" center>
               <h3>Sign in</h3>
               <input type="text" ng-model="credentials.email" placeholder="Username" />
               <input type="password" ng-model="credentials.password" placeholder="Password" />
               <input type="checkbox" id="checkbox-1" name="checkbox-1" /> <label>Remember my password</label>
               <button ng-click="login()">Sign in</button>
           </div>
4

1 に答える 1

2

を使用している場合はangularFireAuth、自分で認証を行う必要はありません。$scope.user値を確認して、ユーザーがログインしているかどうかをすべてのビューで確認してください。

var ref = new Firebase("https://xxx.firebaseio.com/");
angularFireAuth.initialize(ref, {scope: $scope, name: "user", path:"/login"});

$scope.login = function() {   
  angularFireAuth.login("password", { 
    email: $scope.credentials.email,
    password: $scope.credentials.password
  });
};

ログインに成功$scope.userすると、ユーザーの詳細が自動的に設定されます。

于 2013-10-08T18:55:35.690 に答える