# Create Token

Create Token

# Configuration

Use the tokenable guard in the guards configuration of your application's auth.php configuration file:

'guards' => [
    'api' => [
        'driver' => 'tokenable',
        'provider' => 'users',
    ],
],
1
2
3
4
5
6

# Model

To start issuing tokens for users, your User model should use the Jundayw\Tokenable\HasTokenable trait and implement the Jundayw\Tokenable\Contracts\Tokenable interface.

namespace App\Models;

use Jundayw\Tokenable\Contracts\Tokenable;
use Jundayw\Tokenable\HasTokenable;

class User extends Authenticatable implements Tokenable
{
    use HasTokenable, HasFactory, Notifiable;
}
1
2
3
4
5
6
7
8
9

# Controller

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Hash;
use Jundayw\Tokenable\Contracts\Token\Token;
use Jundayw\Tokenable\Support\GuardHelper;

class AccountController
{
    use GuardHelper;
    
    public function login(Request $request): ?Token
    {
        $user = User::query()->where([
            'email'    => $request->get('email'),
            'password' => Hash::make($request->get('password')),
        ])->first();
        
        if(is_null($user)){
            return null;
        }

        return $this->guard('web')
            ->login($user)
            ->createToken(name: 'PC Token', platform: 'pc');
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

# Response structure

{
    "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJhNTg5Yjg0Ni1mMjlkLTQ3MDYtYjIyOC1mZjRmYTVhYzZhM2EiLCJpc3MiOiJBcHAuTW9kZWxzLlVzZXIiLCJzdWIiOjEsImF1ZCI6WyIqIl0sImV4cCI6MTc1OTYzMjY4OSwiaWF0IjoxNzU5NjI1NDg5fQ.7kq4DsCJe54g_Q6pMxwI2L913IcdoRDRnE-Ya4TC7Po",
    "token_type": "Bearer",
    "expires_in": "2025-10-05T02:51:29Z",
    "refresh_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJqdGkiOiIzZDkwYTA1ZS1mNGQxLTQ1YzUtYWFjZS0zMzMxNjkxMzA1MTgiLCJpc3MiOiJBcHAuTW9kZWxzLlVzZXIiLCJzdWIiOjEsImV4cCI6MTc1OTYzMjY4OSwibmJmIjoxNzU5NjI5MDg5LCJpYXQiOjE3NTk2MjU0ODl9.ZzZW-VIMFqIJ5ee_Yw6M4T786bjn0OiBPtYY0chXYHE",
    "type": "token"
}
1
2
3
4
5
6
7

JSON Web Token (JWT) Debugger (opens new window)