Error trying to authenticate to a standard API

Asked

Viewed 116 times

0

I am trying to authenticate my request to an Laravel API (5.4) . Even following the documentation authentication documentation, whenever I send the request, I get the following return:

Not Valid token Provider.

Logincontroller:

<?php

namespace App\Http\Controllers\Auth;

 use App\Http\Controllers\Controller;
 use Illuminate\Contracts\Session\Session;
 use Illuminate\Foundation\Auth\AuthenticatesUsers;
 use Illuminate\Http\Request;
 use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class LoginController extends Controller
{
   use AuthenticatesUsers;

protected $redirectTo = '/home';


public function __construct()
{
    $this->middleware('guest');
}


public function login(Request $request)
{

    $data = $request->all();
    $success = true;

    $user = User::where('email', $data['email'])->first();



    if ($user == null) {
        return $this->outputJSON(null, "Endereço de e-mail incorreto!", 404);
    } elseif (Hash::check($data['password'], $user->getAuthPassword())) {
        $user->remember_token = str_random(60);
        $user->save();

        $result = array(
            'message' => 'Login realizado com sucesso! ',
            'data' => $user,
            'success' => true,
        );
        return response()->json($result);
    } else {
        return $this->outputJSON(null, "Senha inválida", 404);
    }
}

 public function getLogout()
  {
    Session::flush();
    return redirect('/');
  }
}
  • The mistake is very clear Not valid token provider - No valid token provider, i.e., for the API to reply to you without a 403 (access denied) is required to send a token along with the request.

  • Token not retrieved after authentication?

  • Sorry, I was very generic. In this case of login what may be occurring is an error with the generation of Token, the API response in the case. Laravel has a command that you can run for it to create authentication routes and functions, try running in another project and compare: php artisan make:auth (don’t spin on your)

  • 2

    You may be missing a JWT token if that’s what you’re using. I don’t see a new JwtAuth(); in your Controller. I have a model, compare and see if it helps: https://gist.github.com/tiagoboeing/eafe457640b9732cf547e5e21fbf1246

  • Even applying JWT(), generating 'JWT_SECRET', continues to return same error.

  • You are using Postman to test your API?

  • 1

    Solved. Only JWT needed to be set up. Thanks, @Tiagoboeing

  • Glad you could help. Post your code as an answer and mark as solved. If my code has been the solution to your problem I can answer it, but I imagine you have customized for your application.

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.