6

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.

11
  • Why not using laravel passport? if you don't want use laravel passport then use laravel default login method Commented Mar 7, 2019 at 8:54
  • @bhavinjr Actually I already did all the functionality of the dashboard. This is only change that I have to use vue js Commented Mar 7, 2019 at 9:00
  • A couple of things to check: 1. Check if you have session enabled. It is required with the default Laravel auth. 2. Check that your user table has standard id field or you identify the $primaryKey field in your model for the user id. Commented Mar 7, 2019 at 23:25
  • can you please show your router ? also, could you check what the response is in then callback ?? thanks Commented Mar 12, 2019 at 2:39
  • @punk73 I have updated my question. It returns null. Commented Mar 12, 2019 at 5:26

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2
+50

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

1 Comment

Yes both things are same as said by you but still not working.
2

When doing a POST/PATCH/PUT/DELETE request from the browser, you need to include the CSRF token of your page.

this.$http.post('http://localhost/project/admin/vendors/validate_login',{phone_number : this.phone_number},{
  headers: {
    "Access-Control-Allow-Origin": "*",
    "X-CSRF-TOKEN": document.head.querySelector('meta[name="csrf-token"]').content
  }
})
.then((response) =>{
  if(response.data.status == 'success')
  {
    window.location = "http://localhost/project/admin/vendors/dashboard";
  }
}) 

And be sure that you have the CSRF included as a meta tag in all your pages:

<meta name="csrf-token" content="{{ csrf_token() }}">

1 Comment

I have disable this route to work withour csrf token. So no need to pass csrf token
0

If you're using an api guard, do this auth()->guard('api').

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.