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.
login form - 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
'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
'use strict';
app.directive('loginDirective',function(){
return{
templateUrl:'partials/tpl/login.tpl.html'
}
});
service - 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
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.