5
I am using wamp 2.4 and trying to implement an Angularjs login. However, when I click the button submit
occurs the refresh page but it does not change to the home.
The session is always recorded even if the user and password are incorrect.
Below the part of the code that is wrong:
loginService/js:
'use strict';
app.factory('loginService',function($http, $location, sessionService){
return{
login:function(data,scope){
//o erro ja comeca no retorno da session
var $promise=$http.post('http://localhost/estudos/app/data/user.php',data); //send data to user.php
$promise.then(function(msg){
var uid=msg.data;
if(uid){
//scope.msgtxt='Correct information';
sessionService.set('uid',uid);
$location.path('/home');
}
else {
scope.msgtxt='incorrect information';
$location.path('/login');
}
});
},
logout:function(){
sessionService.destroy('uid');
$location.path('/login');
},
islogged:function(){
var $checkSessionServer=$http.post('http://localhost/estudos/app/data/check_session.php');
return $checkSessionServer;
/*
if(sessionService.get('user')) return true;
else return false;
*/
}
}
});
user.php:
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Auth-Token,Authorization,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type');
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE');
$user=json_decode(file_get_contents('php://input')); //get user from
//nao esta respeitando a validacao
if($user->mail=='[email protected]' && $user->pass=='1234')
session_start();
$_SESSION['uid']=uniqid('ang_');
print $_SESSION['uid'];
?>
app.js:
'use strict';
// Declare app level module which depends on filters, and services
var app= angular.module('myApp', ['ngRoute']);
app.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/login', {templateUrl: 'partials/login.html', controller: 'loginCtrl'});
$routeProvider.when('/home', {templateUrl: 'partials/home.html', controller: 'homeCtrl'});
$routeProvider.otherwise({redirectTo: '/login'});
}]);
app.run(function($rootScope, $location, loginService){
var routespermission=['/home']; //route that require login
$rootScope.$on('$routeChangeStart', function(){
if( routespermission.indexOf($location.path()) !=-1)
{
var connected=loginService.islogged();
connected.then(function(msg){
if(!msg.data) $location.path('/login');
});
}
});
});
I couldn’t find where the bug was.
Add the
loginCtrl
to the question.– Vinícius Gobbo A. de Oliveira