I cannot authenticate to the Laravel api using Sanctum

Asked

Viewed 337 times

0

I am using the Standard Sanctum to generate access tokens for my api. I have followed the step-by-step documentation of the Standard. I generated a token. But when I go to access a route that contains a middleware auth:sanctum com in the documentation gives a 401 error stating that it is not authorized. Even if I pass the token as Header Authorization Bearer. I followed step by step but I can’t find the problem.

Code for token generation

if($this->validator($data)){
            $user = $this->create($data);
            $token = $user->createToken('acesso',['tipo:'.$user->tipo]);
            return $token->plainTextToken;
        }`  Rota no Arquivo api.php `Route::middleware('auth:sanctum')->get('/users',"UserController@users");` Código da requisição com axios `var token = "aeeeabab01978e8edfcba40f523f54b424c2d956e1dfbf856049ad9311241087";
axios
  .get("/api/users", 
    { headers: { Authorization: `Bearer: ${token}`} })
  .then(response => {
            // If request is good...
            console.log(response.data)
          });

1 answer

1

I discovered my mistake. I was picking up the encrypted token in the database and was sending it to Header Authorization Bearer. When generating the ringtone should send to the user immediately. Before encrypting to send to the bank. return $token->plainTextToken;

  • Which route are you using? would be .../api/broadicasting/auth ? I’m doing with Laravel Echo Server and I can’t access a Privatechannel or Presencechannel, only working with Channel(). Could you share your experience?

  • 1

    After I discovered my mistake, I realized I didn’t need to build an api. So I started to define the data routes in the web.php file and I use Laravel’s default authentication. I’m also using Laravel Echo. I was able to access all kinds of channels, but with Laravel’s standard authentication. What type of authentication to access your application ("Other than the API"), you are using?

  • Could you share an example of this implementation?

Browser other questions tagged

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