dongping1012 2017-11-17 12:21
浏览 54
已采纳

Angularjs和PHP登录

I'm new to angularjs, I'm bit confused about Login Process.

Everytime I log in I'm redirected to the specific page. Which is set already in the code.

I just want to check if the user is logged in. If yes then redirect to --Home-- if not logged in redirect to --Login Again--

What should I use for this?

I heard about local storage. SessionStorage, I'm not familiar with them. Tell me which way I can manage Login?

Here is my controller for Login

app.controller('AngularLoginController', ['$scope', '$http', function($scope, $http) {   
    $scope.loginForm = function() {         
            $http.post("login.php", {
            'email' :$scope.inputData.email,
            'password':$scope.inputData.password
            }).success(function(data) {
                console.log(data);                  
                if ( data == 'correct') {
                window.location.href = 'welcome_dashboard.php';
                } 
            else {
                $scope.errorMsg = "Invalid Email and Password";
            }
        })
        }
}]);
  • 写回答

2条回答 默认 最新

  • douganggu4392 2017-11-17 12:29
    关注

    You can use $cookieStore of angular js

    after successfull login you can put login true

    $cookieStore.put('login', true);
    

    and check in your dashboard controller if $cookieStore.get('login') undefined than redirect on login page

    When you logout you need to remove this cookie $cookieStore.remove('login')

    if you user routerprovider than use resolve it's easy to check login or not here one simple example of resolve

    function ($routeProvider) {
        $routeProvider.
            when('/', {
                controller: 'dashboardController',
                templateUrl: 'app/views/dashboard.html',
                resolve:{loggedIn:onlyLoggedIn}
             })
     }
    
    var onlyLoggedIn = function ($location,$q,$cookieStore,$rootScope) {
        var deferred = $q.defer();
        if (typeof ($cookieStore.get("login")) === "undefined") {
            deferred.reject();
            window.location.href = 'login.html';
        }else{
            $rootScope.display = true;
            deferred.resolve();
        }
        return deferred.promise;
    
    };
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分