3

I am currently confused why my response JSON for token doesn't provide token code.

"token": {
    "token": {
        "name": "appToken",
        "abilities": [
            "*"
        ],
        "tokenable_id": 1,
        "tokenable_type": "App\\Models\\User",
        "updated_at": "2021-08-29T17:35:00.000000Z",
        "created_at": "2021-08-29T17:35:00.000000Z",
        "id": 7
    }
},

Here's the code for the response.

public function login()
{
    if (Auth::attempt(['email' => request('email'), 'password' => request('password')])) {
        $user = Auth::user();
        $success['token'] = $user->createToken('appToken')->accessToken;
       //After successfull authentication, notice how I return json parameters
        return response()->json([
          'success' => true,
          'token' => $success,
          'user' => $user
      ]);
    } else {
   //if authentication is unsuccessfull, notice how I return json parameters
      return response()->json([
        'success' => false,
        'message' => 'Invalid Email or Password',
    ], 401);
    }
}

I wanted those codes for authentication: Bearer

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Jade
  • 53
  • 8

4 Answers4

9

If you are using the eighth version of Laravel. In the user model Replace the Sanctum\HasApiTokens call with the Passport\HasApiTokens

Replace the following

use Laravel\Sanctum\HasApiTokens;

by

use Laravel\Passport\HasApiTokens;
Muhammad Dyas Yaskur
  • 6,914
  • 10
  • 48
  • 73
3

I think you must add the following in the top of your User Model and Add use HasApiTokens, in the User class.

use Laravel\Passport\HasApiTokens;


class User extends Authenticatable {
    use HasApiTokens, Notifiable;
}
gehbiszumeis
  • 3,525
  • 4
  • 24
  • 41
0

Did you read Laravel Passport Documentation for password grant token?

Once you have created a password grant client, you may request an access token by issuing a POST request to the /oauth/token route with the user's email address and password.

You have to do something like this, within your login function in your controller:

$response = Http::asForm()->post('http://passport-app.com/oauth/token', [
    'grant_type' => 'password',
    'client_id' => 'client-id',
    'client_secret' => 'client-secret',
    'username' => $request->email,
    'password' => $request->password,
    'scope' => '',
]);

Then, you will be able to get access_token and refresh_token from your $response

$response->access_token;
$response->refresh_token;
Luciano
  • 2,052
  • 1
  • 16
  • 26
  • I tried this but it keeps on loading. i hardcoded the neded data like client_id, client_secret, username and password, but still unable to return – Jade Aug 31 '21 at 13:15
0

when use auth()->user()->createToken('user') this its return data like below

{
    "status": true,
    "token": {
        "accessToken": {
            "name": "user",
            "abilities": [
                "*"
            ],
            "tokenable_id": 1,
            "tokenable_type": "App\\Models\\User",
            "updated_at": "2021-10-08T06:26:34.000000Z",
            "created_at": "2021-10-08T06:26:34.000000Z",
            "id": 11
        },
        "plainTextToken": "11|Gl99gx8sfNVV4wxhlEQ2NBl37aGz1RpFASLIRfDP"
    }
}

so you can use auth()->user()->createToken('user')->plainTextToken you get token. so help this for you.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103