-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(readme,auth): Update README.md. Add AuthTokens trait.
- Loading branch information
m.utkin
committed
Mar 27, 2024
1 parent
0b24460
commit f2ad9c8
Showing
4 changed files
with
66 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Larahook\SanctumRefreshToken\Trait; | ||
|
||
use App\Models\User; | ||
use Carbon\Carbon; | ||
use Illuminate\Support\Str; | ||
use Laravel\Sanctum\NewAccessToken; | ||
|
||
trait AuthTokens | ||
{ | ||
/** | ||
* @param User $user | ||
* @param string $deviceName | ||
* @param null|Carbon $accessTokenExpiresAt | ||
* @param null|Carbon $refreshTokenExpiresAt | ||
* | ||
* @return array | ||
*/ | ||
public function createTokens( | ||
User $user, | ||
string $deviceName, | ||
?Carbon $accessTokenExpiresAt = null, | ||
?Carbon $refreshTokenExpiresAt = null, | ||
): array { | ||
$accessToken = $user->createAuthToken($deviceName, $accessTokenExpiresAt); | ||
$refreshToken = $user->createRefreshToken($deviceName, $refreshTokenExpiresAt); | ||
|
||
$accessTokenId = $accessToken->accessToken->getAttribute('id'); | ||
$refreshTokenId = $refreshToken->accessToken->getAttribute('id'); | ||
|
||
config('sanctum-refresh-token.personal_access_token_model')::whereId($accessTokenId) | ||
->update(['refresh_id' => $refreshTokenId,]); | ||
|
||
return [ | ||
'access_token' => $accessToken->plainTextToken, | ||
'access_token_expiration' => $accessToken->accessToken->expires_at ?? null, | ||
'refresh_token' => $refreshToken->plainTextToken, | ||
'refresh_token_expiration' => $refreshToken->accessToken->expires_at ?? null, | ||
]; | ||
} | ||
} |