I have a login form in vue.js. I just have to login user with vue.js and laravel. Once login user will redirect to a dashboard which is already developed with laravel only. I am trying to login but it is not working and auth()->guard
returns null so that user redirected to the login page instead of the dashboard. When I use postman in that case it works well.
Vue js
this.$http.post('http://localhost/project/admin/vendors/validate_login',{phone_number : this.phone_number},{
headers: {
"Access-Control-Allow-Origin": "*"
}
})
.then((response) =>{
if(response.data.status == 'success')
{
window.location = "http://localhost/project/admin/vendors/dashboard";
}
})
Laravel - validate login :
public function validate_login(Request $request)
{
$arr_rules = array();
$status = false;
$remember_me = "";
$arr_rules['phone_number'] = "required";
$validator = validator::make($request->all(),$arr_rules);
if($validator->fails())
{
return response()->json([
'status' => 'error',
'msg' => "Mobile number is empty"
]);
}
$obj_group_vendor = $this->UsersModel
->where('phone_number',$request->only('phone_number'))
->first();
if($obj_group_vendor)
{
if($this->auth->attempt(['phone_number' => $request->phone_number,
'role_id' => 3]))
{
return response()->json([
'status' => 'success',
'msg' => "You are successfully login to your account."
]);
}
else
{
return response()->json([
'status' => 'error',
'msg' => "Invalid login credential."
]);
}
}
else
{
return response()->json([
'status' => 'error',
'msg' => "Invalid login credentials."
]);
}
return;
}
Route:
$web_vendor_path = config('app.project.vendor_panel_slug');
Route::group(array('prefix' => $web_vendor_path,
'middleware'=> ['vendor_auth_check']), function ()
{
$route_slug = 'vendor_';
Route::post('validate_login', ['as' => $route_slug.'validate',
'uses' => $module_controller.'validate_login']);
});
Route::group(array('prefix' => $web_vendor_path,
'middleware'=>'auth_vendor'), function ()
use($web_vendor_path)
{
$route_slug = 'vendor_';
$module_controller = "Vendor\DashboardController@";
Route::get('/dashboard',['as' => $route_slug.'index',
'uses' => $module_controller.'index']);
});
Any help would be appreciated.
Just spitting out some ideas here:
Try to set the 'Content-Type': 'application/x-www-form-urlencoded'
header for all POST requests.
In addition, the Access-Control-Allow-Origin
is a header that should be in the response of the webserver. It is not a header you should be sending in your request from javascript.
Make sure you have web middleware enabled on your route file in app/Providers/RouteServiceProvider.php
Then check in app/Http/Kernel.php that \Illuminate\Session\Middleware\StartSession::class
, is added and enabled in $middlewareGroups['web']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With