LorenzoBerti
LorenzoBerti

Reputation: 6974

Different Token Expiration Depending of Client Laravel Passport

I have a Laravel application that uses Passport authentication.

Login

public function authenticate(Request $request)
{
    $params = [
        'grant_type' => 'password',
        'client_id' => 1,
        'client_secret' => "secret",
        'username' => request('username'),
        'password' => request('password'),
        'active' => 1,
        'scope' => '*'
    ];

    $request->request->add($params);

    // verify the credentials and create a token for the user
    $proxy = Request::create('oauth/token', 'POST');

    return Route::dispatch($proxy);
}

I have settled the expiration on AuthServiceProvider:

Passport::routes(function ($router) {
   $router->forAccessTokens();
});
Passport::tokensExpireIn(now()->addMinute(1));
Passport::refreshTokensExpireIn(now()->addDays(30));

It works but after 1 minute the token expires. I want a different expiration date for token depending on where I'm trying to make login because I have a website, desktop app and an Android app.

For example:

I was thinking send me from where I'm trying to make the login, but is that a good way? Are there any other possible ways to do it?

For now I have tried this:

-) deleted From AuthServiceProvider:

Passport::tokensExpireIn(now()->addMinute(1));

And added in Login function:

if (request('from') == 'something') {
    Passport::tokensExpireIn(now()->addYears(1));
} else {
    Passport::tokensExpireIn(now()->addHours(8));
}

$proxy = Request::create('oauth/token', 'POST');

Upvotes: 7

Views: 2718

Answers (1)

Adarsh Bhatt
Adarsh Bhatt

Reputation: 588

You can get help from below link please find

For getting user agent detail and apply condition base on agent

for example you can use code like below

if ( Agent::isDesktop() ) {
    Passport::tokensExpireIn(now()->addYears(1));
} else if(Agent::isMobile()){
    Passport::tokensExpireIn(now()->addMonth(5));
}else{
    Passport::tokensExpireIn(now()->addHours(8));
}

Upvotes: 7

Related Questions