Skip to content

Commit

Permalink
feat(readme,auth): Update README.md. Add AuthTokens trait.
Browse files Browse the repository at this point in the history
  • Loading branch information
m.utkin committed Mar 27, 2024
1 parent 0b24460 commit f2ad9c8
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 19 deletions.
31 changes: 13 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,22 @@ Install migrations
```

## Usage

Add trait `AuthTokens` and call method `createTokens`
```php
public function createTokens(User $user, string $deviceName, ?\DateTimeInterface $expiresAt = null): array
{
# Create tokens
$accessToken = $user->createAuthToken($deviceName, $expiresAt);
$refreshToken = $user->createRefreshToken($deviceName, $expiresAt);
use Larahook\SanctumRefreshToken\Trait\AuthTokens;

# Get ID
$accessTokenId = $accessToken->accessToken->getAttribute('id'),
$refreshTokenId = $refreshToken->accessToken->getAttribute('id')
class SomeClass
{
use AuthTokens;

# Save refresh_id for access token
PersonalAccessToken::whereId($accessTokenId)->update(['refresh_id' => $refreshTokenId]);
public function login(string $email, string $password, string $deviceName): array
{
$user = User::whereEmail($email)->first();
if (!$user || !$this->isValidPassword($password, $user->getPassword())) {
throw new UnauthorizedException('The provided credentials are incorrect.', Errors::AUTHORIZATION_ERROR->value);
}

# Send response with access_token and refresh_token
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,
];
return $this->createTokens($user, $deviceName);
}
}
```
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"source": "https://github.com/Mishanki/sanctum-refresh-token"
},
"require": {
"php": "^7.4|^8.0",
"php": "^8.0",
"laravel/framework": "^10.0"
},
"require-dev": {
Expand Down
10 changes: 10 additions & 0 deletions config/sanctum-refresh-token.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
<?php

return [
/*
|--------------------------------------------------------------------------
| PersonalAccessToken model
|--------------------------------------------------------------------------
|
| This model with refresh_id column
|
*/
'personal_access_token_model' => \Larahook\SanctumRefreshToken\Model\PersonalAccessToken::class,

/*
|--------------------------------------------------------------------------
| Refresh Route Name
Expand Down
42 changes: 42 additions & 0 deletions src/Trait/AuthTokens.php
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,
];
}
}

0 comments on commit f2ad9c8

Please sign in to comment.