Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expiration is not working in #464

Closed
hpt123456 opened this issue Aug 18, 2023 · 5 comments
Closed

Expiration is not working in #464

hpt123456 opened this issue Aug 18, 2023 · 5 comments

Comments

@hpt123456
Copy link

Sanctum Version

3.2

Laravel Version

10.10

PHP Version

8.2.8

Database Driver & Version

mariadb:10

Description

What I understand from documentation if I set expiration' => 50000, in cofig/sanctum.php file and I create below code in one of the controller method.

$token = $request->user()->createToken('personal-token')->plainTextToken;

I should see expires_at with some datetime value at personal_access_tokens table, Am I right? But I got null. I could add that datetime value in expires_at column at personal_access_tokens* table by some ways. But, I would like to use from package if it already has that feature.

Steps To Reproduce

$ laravel new example-app
$ composer require laravel/sanctum
$ php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"
$ php artisan migrate

config/auth.php

    'stateful' => true,
    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],



    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'sanctum' => [
            'driver' => 'sanctum',
            'provider' => 'users',
            'hash' => false,
        ],
    ],

config/sanctum.php

'expiration' => 50000,

One of the controller method

    function login(EmailLoginRequest $request) : JsonResponse {

        $credentials = $request->only(['email', 'password']);

        if (Auth::attempt($credentials)) {

            $token = $request->user()->createToken('personal-token')->plainTextToken;

            return jsonSuccess([
                'user' => array_merge($request->user()->only('id', 'name', 'email'),[
                    'token' => $token
                ])
                
            ], __('auth.Successfully logged in'));
        }

        return jsonError(__('auth.wrong_credentials'), 401);
    }
@crynobone
Copy link
Member

Hey there, thanks for reporting this issue.

We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as one separate commit on the main/master branch and share the repository here?

Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.

laravel new bug-report --github="--public"

Do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue.

Thanks!

@hpt123456
Copy link
Author

Hi @crynobone ,

Thanks for your help. I setup the project using docker. And I also included everything ready. You only need to run docker-compose up and then you could call http://localhost/test in browser . Please let me know if you need another more.

@driesvints
Copy link
Member

@hpt123456 can you provide a link to a public repo?

@hpt123456
Copy link
Author

@crynobone @driesvints

Sorry I thought link was included. Here is the link https://github.com/hpt123456/bug-report

@crynobone
Copy link
Member

I should see expires_at with some datetime value at personal_access_tokens table, Am I right?

No, the expiration is only used in below:

new Guard($auth, config('sanctum.expiration'), $config['provider']),

And validated against created_at:

(! $this->expiration || $accessToken->created_at->gt(now()->subMinutes($this->expiration)))

You should be able to do it by using the following:

$token = $request->user()->createToken(
    name: 'personal-token', 
    expiresAt: now()->addMinutes(30)
)->plainTextToken;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants