Thursday, May 3, 2018

error":"token_not_provided: API Authentication in Laravel, Vue and JWT - development

error":"token_not_provided: API Authentication in Laravel, Vue and JWT

I am creating JWT API authentication in Laravel with as stated in guide this link:

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

Everything else is fine but I am having this issue on login:
error”:”token_not_provided
Login seem to be going through successfully as I can see in the console or postman, but the token is never being retrieved. This is happening when the app tries to load the user data at api/auth/user end point.

GET http://127.0.0.1:8000/api/auth/user 400 (Bad Request)

this is my login method:

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

Then the user method

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

Authentication Routes:

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 Login Component:

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

I cannot figure out how to fix this. Just don’t tell why the authorization header not being returned:

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

Please Assist.



from Laravel Questions and Answers https://laravelquestions.com/laravel/errortoken_not_provided-api-authentication-in-laravel-vue-and-jwt/
via Lzo Media

No comments:

Post a Comment