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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)