doujia8801 2014-04-22 19:12
浏览 71
已采纳

Angularjs + Codeigniter用户身份验证

I have an app that has a full angularjs frontend with a codeigniter backend (with thishttps://github.com/philsturgeon/codeigniter-restserver)

I am using the codeigniter backend just to make api requests essentially.

The problem I am having now is managing a navigation view based on whether or not a user is logged in.

I have the navigation in its own navCtrl with a userService and loginCtrl.

I am storing loggedIn true or false in a cookie with a $watch on it in the navCtrl so it will update the navigations appropriately.

Any insight on why this may not be working? Any code i need to provide to clarify? Is there a "better" way to do this?

EDIT: The $watch is not actually catching when I update the value using the userService.

Thank you!

  • 写回答

2条回答 默认 最新

  • dsv73806 2014-04-22 21:38
    关注

    We have a very similar setup as you. What we do is have Codeigniter send a HTTP Status code of 419 (not logged in) or something like that. And then you'll use Angular Interceptors to 'listen' for the response from the backend.

    app.factory('loggedIn',function($q,$location){
        return {
            /*
             * Intercept all response errors & handle them
             */
            responseError: function(response) {
                if (response.status == 419) {
                    console.error("You are not logged in");
                    $location.path('/login');
                }
                return $q.reject(response);
            }
        };
    });
    

    Then push it to the $httpProvider:

    app.config(['$httpProvider', function($httpProvider){
        $httpProvider.interceptors.push('loggedIn');
    }]);
    

    This should handle your front-end navigation pretty easily.

    There are also other things that you can do for the $http requests before sending & before response is returned. Makes for a slick setup.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题