dsfgds4215 2013-12-12 09:00
浏览 47
已采纳

AngularJs + php身份验证

Hi i started learning AngularJs and now im trying to do my Login module using angular and php, but i have some issues. I have watched alot tutorials but none of them was helpful in my case, so here is what i have: controllers.js:

var controllers = angular.module('controllers', []);     

controllers.controller('loginController', ['$scope', '$http', 'UserService', function(scope, $http, User) {
scope.main = [
    {username: '', password: ''}
]

scope.login = function(){
    var config = {
        url: '../auth/login.php',
        method: 'POST',
        data: {
            username: scope.main.username,
            password: scope.main.password
        },
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    }

    $http(config)
    .success(function(data,status,headers,config){
        if(data.status){
            //succefull login
            User.isLogged = true;
            User.username = data.username;
        }
        else{
            User.isLogged = false;
            User.username = '';
        }
    })
    .error(function(data,status,headers,config){
        User.isLogged = false;
        User.username = '';
    });
}
}])

auth.js:

var services = angular.module('services', []);

services.factory('UserService', [function(){
    var sdo = {
        isLogged: false,
        username: ''
    };
    return sdo;
}]);

login.php:

$username = $_POST['username'];
if($username){
    return "Logged";
}else{
    return false;
}

and the html:

<div class="col-xs-12" id="loginCol" ng-controller="loginController">
   <form ng-submit='login()' name="form" novalidate>
      <div class="form-group">
         <label for="username" class="sr-only">Username</label>
         <input type="text" ng-model="scope.main.username" class="form-control" id="username" placeholder="Име..." />
         <label for="password" class="sr-only">Password</label>
         <input type="password" ng-model="scope.main.password" class="form-control" id="password" placeholder="Парола..." />
      </div>

      <div class="form-group pull-right">
         <button type="button" class="btn btn-primary">Login</button>
         <button type="button" class="btn btn-default">Register</button>
      </div>
   </form>
</div>

In this case i want just if user type something in the username input and hit the login button and on successful call of login.php to return some message. The problem is that code written like that got error "'loginController' is not a function, got undefined" how to fix it?

  • 写回答

2条回答 默认 最新

  • duancuan7057 2013-12-16 23:15
    关注

    (Disclosure: I'm one of the developers of UserApp)

    You could try the third-party service UserApp, together with the AngularJS module.

    Check out the getting started guide, or take the course on Codecademy. Here's some examples of how it works:

    • Login form with error handling:

      <form ua-login ua-error="error-msg">
          <input name="login" placeholder="Username"><br>
          <input name="password" placeholder="Password" type="password"><br>
          <button type="submit">Log in</button>
          <p id="error-msg"></p>
      </form>
      
    • Signup form with error handling:

      <form ua-signup ua-error="error-msg">
        <input name="first_name" placeholder="Your name"><br>
        <input name="login" ua-is-email placeholder="Email"><br>
        <input name="password" placeholder="Password" type="password"><br>
        <button type="submit">Create account</button>
        <p id="error-msg"></p>
      </form>
      

      ua-is-email means that the username is the same as the email.

    • How to specify which routes that should be public, and which route that is the login form:

      $routeProvider.when('/login', {templateUrl: 'partials/login.html', public: true, login: true});
      $routeProvider.when('/signup', {templateUrl: 'partials/signup.html', public: true});
      

      The .otherwise() route should be set to where you want your users to be redirected after login. Example:

      $routeProvider.otherwise({redirectTo: '/home'});

    • Log out link:

      <a href="#" ua-logout>Log Out</a>

    • Access user properties:

      User info is accessed using the user service, e.g: user.current.email

      Or in the template: <span>{{ user.email }}</span>

    • Hide elements that should only be visible when logged in:

      <div ng-show="user.authorized">Welcome {{ user.first_name }}!</div>

    • Show an element based on permissions:

      <div ua-has-permission="admin">You are an admin</div>

    And to authenticate to your back-end services, just use user.token() to get the session token and send it with the AJAX request. At the back-end, use the UserApp API with the PHP library to check if the token is valid or not.

    If you need any help, just let me know :)

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

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看