javascript AngularJS - 使用 md5 密码检查登录服务中的用户/密码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23688392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJS - Check user/password in Login Service using md5 Password
提问by MizAkita
I have created a login using angularjs - i am simply trying to pass it to my service user.php to check the credentials and then forward after authenticated. Problem is, is my password is ecrytped using md5 and im trying to figure out how to add this encoding into my data being sent from the service.
我已经使用 angularjs 创建了一个登录名 - 我只是想将它传递给我的服务 user.php 以检查凭据,然后在通过身份验证后转发。问题是,我的密码是否使用 md5 加密,我试图弄清楚如何将此编码添加到我从服务发送的数据中。
login form - login.html
登录表单 - login.html
<div class="bs-example">
<form role="form" name="form1">
<div class="pgelogo"></div><p></p>
<div class="form-group">
<p>Welcome : <span>{{user.userid}}</span></p>
<label for="exampleInputEmail1">Userid</label>
<input type="text" placeholder="Enter name" class="form-control"
ng-model="user.userid" required>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" name="pass" placeholder="Password"
id="exampleInputPassword1" class="form-control" ng-model="user.pass"
required>
</div>
<button class="btn btn-default" type="submit" ng-click="login(user)"
ng-disabled="form1.$invalid">Submit</button>
<p>{{msgtxt}}</p>
</form>
</div>
controller - loginctrl.js
控制器 - loginctrl.js
'use strict';
app.controller('loginCtrl', ['$scope','loginService',
function ($scope,loginService) {
$scope.msgtxt='';
$scope.login=function(data){
loginService.login(data,$scope); //call login service
};
}]);
directive - logindrc.js
指令 - logindrc.js
'use strict';
app.directive('loginDirective',function(){
return{
templateUrl:'partials/tpl/login.tpl.html'
}
});
service - loginservice.js
服务 - loginservice.js
'use strict';
app.factory('loginService',function($http, $location, sessionService){
return{
login:function(data,scope){
var $promise=$http.post('data/user.php',data);
//send data to user.php
$promise.then(function(msg){
var uid=msg.data;
var pass=msg.data;
if(uid && pass){
//scope.msgtxt='Correct information';
sessionService.set('uid',uid);
//sessionService.set('lvl',level);
$location.path('/home');
}
else {
scope.msgtxt='incorrect information';
$location.path('/login');
}
});
},
logout:function(){
sessionService.destroy('uid');
//sessionService.destroy('lvl');
$location.path('/login');
},
islogged:function(){
var $checkSessionServer=$http.post('data/check_session.php');
return $checkSessionServer;
/*
if(sessionService.get('user')) return true;
else return false;
*/
}
}
});
**user.php - checks user authentication
**user.php - 检查用户身份验证
ini_set("display_errors",1);
error_reporting(E_ALL);
//connect to db
require "../protected/db.php";
//$user=json_decode(file_get_contents('php://input'));
$user = $_SESSION['uid']=uniqid('ang_');
//$user = $_POST['uid'];
$pass = md5($_POST['pass']));
session_start();
if(isset($user) && isset($pass))
{
//get userid and level from users table according to
username and password entered
$sql = "SELECT userid,lvl FROM dbo.users WHERE userid = ? AND psw = ?";
$params = array($user, $pass);
$stmt = sqlsrv_query($conn,$sql,$params);
//if query does not work, return false
if($stmt === false){
die( print_r( sqlsrv_errors(), true));
}
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
if($row)
{
do{
//store users session for angular to handle
$_SESSION['uid']=uniqid('ang_');
$_SESSION['level'] = $row['lvl'];
//update the userlogin table with active user sesssion
$rec = "INSERT INTO dbo.user_login (
user_id,
time)
VALUES
(?,getdate())";
$recparams = array($user);
$s = sqlsrv_query($conn,$rec,$recparams);
if( $s === false )
{
die( print_r( sqlsrv_errors(), true));
}
} while( $row = sqlsrv_fetch_array( $stmt ) );
}
session_destroy();
}
I am able to authenticate with just simple username password but i need the password to be encoded to check against the values in my db. I am getting error 500 service error meaning something is happening on the server end... just can't pin point it. Any help with this would be appreciated by any of you angularjs masters.
我只能使用简单的用户名密码进行身份验证,但我需要对密码进行编码以检查我的数据库中的值。我收到错误 500 服务错误,这意味着服务器端发生了某些事情......只是无法指出它。你们中的任何一位 angularjs 大师都会对此提供任何帮助。
回答by Konrad Viktor Hefelle
Integrate this module: http://ngmodules.org/modules/angular-md5
集成这个模块:http: //ngmodules.org/modules/angular-md5
On the login form: login form - login.html
在登录表单上:登录表单 - login.html
<input type="password" name="pass" placeholder="Password"
id="exampleInputPassword1" class="form-control" ng-model="pass" required>
And then on the controller: controller - loginctrl.js
然后在控制器上:控制器 - loginctrl.js
app.controller('loginCtrl', ['$scope','md5','loginService', function ($scope,md5,loginService) {
$scope.$watch('pass', function() {
$scope.user.pass = md5.createHash($scope.pass || '');
});
...
}]);
So this solved the issue in my case
所以这解决了我的问题