i am trying to implement the login system in angularjs and php.
$scope.signForm = function() {
$scope.loading = true;
$scope.remember=true;
$http.post('user/action/signin.php', $scope.formData)
.success(function(data) {
console.log(data);
$scope.error = false;
$scope.message = false;
if (!data.success) {
$scope.error = data.error;
$scope.loading = false;
} else {
$location.url('/userprofile.php'); //
$scope.loading = false;
}
});
};
i am checking for user login if success i need to load the userprofile.php. my userprofile.php page is different and not using angularui.
thanks.
Use $window.location.href = '/userprofile.php'
Related
I have a script running and working, however the insert into members part don't work:
function register_member($username,$name,$email,$password) {
$time = time();
$ip = get_ip();
db()->query("INSERT INTO members (full_name,username,email,password,ip_address,last_active,date_created,banned,active)VALUES(?,?,?,?,?,?,?,?,?)",
$name,$username,$email,$password, $ip, $time, $time,0,1);
//login user
login_user($username, $pass, false);
if (config('auto-follow', '')) {
$users = explode(',', config('auto-follow'));
foreach($users as $user){
$user = get_user($user);
if ($user) {
follow($user['id']);
}
}
}
return true;
}
It seems for me that it can't read the db()->query ("INSERT INTO part. When I remove that query, the signup script can complete reading the function, but if I leave it in, it returns a 500 internal error. Any ideas?
Finally got the php.ini to change, and it is in the js script the error is happening. On this line here: var json = jQuery.parseJSON(result);
$(document).on("submit", "#signup-form", function() {
$(".loader-container").fadeIn();
$(this).ajaxSubmit({
url : baseUrl + 'signup',
success : function(result) {
var json = jQuery.parseJSON(result);
if (json.status == 1) {
//we can redirect to the next destination
window.location.href = json.url;
notify(json.message, 'success');
} else {
notify(json.message, 'error');
$(".loader-container").fadeOut();
}
}
})
return false;
});
I'm using express.js and app.post route is used to validate user and redirect valid user to PHP page using the res.redirect(), but instead of redirecting it is displaying Found. Redirecting to http://localhost/home.php
my server.js code is as follows:
app.post('/set_node_session', function(req, res){
sess = req.session;
var usermail = req.body.field1;
var password = req.body.field2;
var sql="SELECT * FROM userInfo WHERE userEmail='"+usermail+"'";
con.query(sql, function (err, result, fields) {
if (err) throw err;
sess.user_id=result[0].userId;
sess.first_name=result[0].userFname;
sess.last_name=result[0].userLname;
if(sess.user_id) {
res.redirect('http://localhost/home.php');
}
else {
res.redirect('http://localhost/login.php');
}
});
});
nodejs:
var resp = sess.user_id ? '1' : '0';
res.send(resp );
and in your php:
$result = curl_exec($ch);
if(result == 1){
header("Location: http://localhost/home.php");
}
else{
header("Location: http://localhost/login.php");
}
die();
$location.path not redirecting after successful return from php. Data returned from php page is not empty . No problem with php file its returning correct data .The problem is $location.path is not working I referred many sites but I could not find solution help me..
angular.module(MyApp).controller(Part3Controller, function ($scope, LoginService) {
$scope.IsLogedIn = false;
$scope.Message = '';
$scope.Submitted = false;
$scope.IsFormValid = false;
$scope.MyLogin = {
USER_ID:'' ;
Password: '';
};
//Check is Form Valid or Not // Here f1 is our form Name
$scope.$watch(f1.$valid, function (newVal) {
$scope.IsFormValid = newVal;
});
$scope.Login = function () {
$scope.Submitted = true;
if ($scope.IsFormValid) {
LoginService.GetUser($scope.MyLogin).then(function (d) {
if (d.data.USER_ID != null) {
$scope.IsLogedIn = true;
$location.Path(/LandingPage/FetchMenu);
}
else {
alert('Invalid Credential!');
}
});
}
};
})
.factory('LoginService, function ($http) {
var fac = {};
fac.GetUser = function (d) {
return $http({
url:/Data/UserLogin,
method: POST,
data: JSON.stringify(d),
headers: {content-type:application/json}
});
};
return fac;
});
You haven't injected $location:
angular.module(MyApp).controller(Part3Controller,
function ($scope, LoginService) {
Should be:
angular.module(MyApp).controller(Part3Controller,
function ($location, $scope, LoginService) {
I have a login method in my controller where I check if there is such user in database or not. I call this method when press Submit button. I show login from view at the same time.
In my case, it doesn't show message if there is such user. I think because in my controller I load view.
How could I show this message if there is such user using Ajax and if I return view as I do in my case?
I'm using Kohana. Thanks!
My code is:
$(document).ready(function(){
$('#submit').on('click', function() {
if(username.length === 0 || password.length === 0) {
//...check if validation fails
}
else {
$.ajax({
url: "/admin/signin" ,
type: "POST",
data: {
"username":username,
"password":password
},
success: function(data) {
if(data !== 'error') {
window.location = "/admin/index";
}
else
{
alert('no such user');
}
}
});
}
});
});
public function action_signin()
{
if ($_POST) {
$is_admin = Model_Admin::signin($_POST);
print 'success';
} else {
print 'error';
}
}
$this->template->content = View::factory('admin/login_form');
}
If you want not load 'default' template try using $this->auto_render = FALSE;
also kohana controller has method is_ajax $this->request->is_ajax()
You controller code will be like this.
public function action_signin()
{
if($this->request->is_ajax()){
$this->auto_render = FALSE;
}
if ($_POST) {
$is_admin = Model_Admin::signin($_POST);
if($is_admin){
print 'success';
} else {
print 'error';
}
}else{
$this->template->content = View::factory('admin/login_form');
}
}
I'm creating a login system in laravel. On my local server, the code works but when I put it on a live server, the Auth::check() keeps return false and thus when I login with the right credentials it redirects me back to login page again. FYI My liver server is using php 5.4 while my local server is using php 5.5
Here is my code.
routes.php
Route::get('/', function() {
//Auth::check() KEEPS RETURNING FALSE EVEN WHEN USER LOGS IN
if (Auth::check() == true) {
$role = Auth::user()->role;
if ($role == 1) {
return View::make('administrator');
} elseif ($role == 9) {
return View::make('agent');
}
} else {
return View::make('login');
}
});
LoginController.php
public function login() {
if (Auth::attempt(array('username' => Input::json('username'), 'password' => Input::json('password')))) {
return Response::json(Auth::user());
} else {
return Response::json(array('flash' => 'Invalid email or password'), 500);
}
}
}
angularcontroller.js
$scope.login = function() {
//assign variables
var post = {};
post.username = $scope.info.username;
post.password = $scope.info.password;
//Validation
var errors = 0;
if (errors == 0) {
loginServ.login(post).then(function(data) {
if (data.status == 500) {
alert('wrong username or password');
} else {
window.location.replace("");
}
});
}
};
angular_service.js
login_module.factory('loginServ', function($http, $q) {
return {
login: function(post) {
var url = "login/login";
return $q.all([
$http.post(url, {
username: post.username,
password: post.password
})
])
.then(function(results) {
var data = [];
angular.forEach(results, function(result) {
data = data.concat(result.data);
// console.log("data: "+result);
// console.log("result.data: "+result.data);
});
return data[0];
},
function(error) {
console.log(error.status);
return error;
// Handle error here
});
}
}
});
I have login form using angularjs. It sends a POST request and if it gets a 200 response then it sends a GET request to "/". However even with right login credentials it keeps going back to the same login page.
Thanks
Try to change the value in app\config\session.php => 'cookie'.
This should fix the issue