错误":" token_not_provided:Laravel,Vue和JWT中的API身份验证

时间:2018-05-03 17:25:58

标签: laravel vue.js jwt

我正在Laravel中创建JWT API身份验证,如指南此链接中所述:

https://codeburst.io/api-authentication-in-laravel-vue-spa-using-jwt-auth-d8251b3632e0

其他一切都很好,但我在登录时遇到了这个问题: 的错误":" token_not_provided 正如我在控制台或邮递员中看到的那样,登录似乎成功完成,但永远不会检索令牌。当应用程序尝试在api / auth / user端点加载用户数据时会发生这种情况。

获取http://127.0.0.1:8000/api/auth/user 400(错误请求)

这是我的登录方式:

public function login(Request $request)
{
    $credentials = $request->only('email', 'password');
    if ( ! $token = JWTAuth::attempt($credentials)) {
            return response([
                'status' => 'error',
                'error' => 'invalid.credentials',
                'msg' => 'Invalid Credentials.'
            ], 400);
    }
return response([
            'status' => 'success'
        ]) ->header('Authorization', $token);
}

然后是用户方法

public function user(Request $request)
{
    $user = User::find(Auth::User()->id);
   return response([
            'status' => 'success',
            'data' => $user
        ]);
        return $user;
}

身份验证路由:

Route::post('auth/login', 'JWTAuthenticateController@login');
Route::group(['middleware' => 'jwt.auth'], function(){  
 Route::post('auth/logout', 'JWTAuthenticateController@APIlogout');

  Route::get('auth/user', 'JWTAuthenticateController@user');
});

VueJS登录组件:

<script>
  export default {
    data(){
      return {
        email: null,
        password: null,
        error: false


      }
    },
    methods: {
      login(){
        var app = this
        this.$auth.login({
            params: {
              email: app.email,
              password: app.password,

            }, 
            success: function () {
            console.log('Success')
            },
            error: function () {
                        console.log('Something Went wrong!')


            },
            rememberMe: true,
            redirect: {name:'inventories'},
            fetchUser: true,
        });       
      },
    }
  } 
</script>

我无法弄清楚如何解决这个问题。只是不告诉为什么没有返回授权标题:

 return response([
                'status' => 'success'
            ]) ->header('Authorization', $token);

请协助。

0 个答案:

没有答案