#Laravel Passport token different from common

16 messages · Page 1 of 1 (latest)

limber valley
#

Hey!

I have a question, I have a register and login controller in laravel, when I use register in Postman it apparently does not return the alphanumeric token (Bearer). Apparently, I installed everything and configured everything normally.

Here the code:

Controller:

public function register(Request $request) 
    {
        try {
            $val = $request->validate([
                'name' => ['required', 'string', 'max:255'],
                'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
                'password' => ['required', 'string', 'min:8', 'confirmed'],
            ]);

            $user = User::create([
                'name' => $val['name'],
                'email' => $val['email'],
                'password' => Hash::make($val['password']),
            ]);

            $token = $user->createToken('auth_token')->accessToken;
            
            return response()->json(['message' => 'Usuário registrado com sucesso', $token], 201);

        } catch (ValidationException $e) {
            return response()->json(['errors' => $e->errors()], 422);
        }
    }

Route
Route::post('register', [AuthController::class, 'register'])->name('register');

Response of postman
{
    "message": "Usuário registrado com sucesso",
    "0": {
        "name": "auth_token",
        "abilities": [
            "*"
        ],
        "expires_at": null,
        "tokenable_id": 5,
        "tokenable_type": "App\\Models\\User",
        "updated_at": "2023-06-22T21:18:03.000000Z",
        "created_at": "2023-06-22T21:18:03.000000Z",
        "id": 1
    }
}

This also happens, of course, when I log in...

uncut sparrow
limber valley
#

It's true you're right

#

I will modify this. But, would that be the mistake? No, right?

uncut sparrow
#

I don’t really understand what it is you’re asking to be honest.

#

But if you’re making requests using Postman then you need to tell Laravel you want JSON responses. So you need to an Accept: application/json header to your requests.

limber valley
#

No problem... I'll explain better

uncut sparrow
#

Then Laravel will return JSON responses, rather than HTML or redirects.

limber valley
#

My question is in the output of the registry. In some videos I saw about Passport, the output is like this, just like the image...
And my departure is the way I ordered, even, apparently, everything is right...

#

It is better now?

#

But, I'll try what you commented on, maybe you haven't seen it...

uncut sparrow
limber valley
#

You mean print output, right?

uncut sparrow
#

No.

#

Read my message again.

limber valley
#

Got it, the url /oauth/token returns the token...
So **/register **doesn't have this output?

(Well, I'm having these doubts, because I got to study Laravel Passport a few days ago)