Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel auth()->guard() returns null using vue js

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.

like image 772
amM Avatar asked Mar 07 '19 08:03

amM


2 Answers

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.

like image 92
Flame Avatar answered Sep 21 '22 09:09

Flame


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']

like image 45
Apród Illés Avatar answered Sep 22 '22 09:09

Apród Illés