Laravel auth()-> guard()使用vue js返回null

时间:2019-03-07 08:46:56

标签: php laravel vue.js axios

我在vue.js中有一个登录表单。我只需要使用vue.js和laravel登录用户。登录后,用户将重定向到仅使用laravel开发的仪表板。我正在尝试登录,但无法正常工作,auth()->guard返回 null ,以便用户重定向到登录页面而不是仪表板。在这种情况下,当我使用邮递员时,效果很好。

Vue js

this.$http.post('http://localhost/petamiko/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/petamiko/admin/vendors/dashboard";
  }
})    

Laravel-验证登录名:

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;
}       

路线:

$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']);
});

任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:2)

从浏览器发出POST / PATCH / PUT / DELETE请求时,您需要包括页面的CSRF令牌。

  return (
     <View>

         (...)

         result && result.map( (item, index) => {

         (...)

         }
     </View>
  )

并确保所有页面中都包含CSRF作为元标记:

this.$http.post('http://localhost/petamiko/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/petamiko/admin/vendors/dashboard";
  }
}) 

答案 1 :(得分:2)

只是在这里吐出一些想法:

  • 尝试为所有POST请求设置'Content-Type': 'application/x-www-form-urlencoded'标头。

  • 此外,Access-Control-Allow-Origin是标头,应位于网络服务器的响应中。它不是标头,您应该从javascript发送请求。

答案 2 :(得分:2)

确保在 app / Providers / RouteServiceProvider.php

中的路由文件上启用了Web中间件

然后在 app / Http / Kernel.php 中检查是否在\Illuminate\Session\Middleware\StartSession::class中添加并启用了$middlewareGroups['web']

答案 3 :(得分:0)

如果您使用的是API保护程序,请执行auth()->guard('api')