Skip to content

Commit 8c10436

Browse files
Extract generate token method (#488)
* Extract generate token method * Update comment * Update comment * Update HasApiTokens.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 4fc91ed commit 8c10436

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/HasApiTokens.php

+16-6
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,7 @@ public function tokenCan(string $ability)
4545
*/
4646
public function createToken(string $name, array $abilities = ['*'], DateTimeInterface $expiresAt = null)
4747
{
48-
$plainTextToken = sprintf(
49-
'%s%s%s',
50-
config('sanctum.token_prefix', ''),
51-
$tokenEntropy = Str::random(40),
52-
hash('crc32b', $tokenEntropy)
53-
);
48+
$plainTextToken = $this->generateTokenString();
5449

5550
$token = $this->tokens()->create([
5651
'name' => $name,
@@ -62,6 +57,21 @@ public function createToken(string $name, array $abilities = ['*'], DateTimeInte
6257
return new NewAccessToken($token, $token->getKey().'|'.$plainTextToken);
6358
}
6459

60+
/**
61+
* Generate the token string.
62+
*
63+
* @return string
64+
*/
65+
public function generateTokenString()
66+
{
67+
return sprintf(
68+
'%s%s%s',
69+
config('sanctum.token_prefix', ''),
70+
$tokenEntropy = Str::random(40),
71+
hash('crc32b', $tokenEntropy)
72+
);
73+
}
74+
6575
/**
6676
* Get the access token currently associated with the user.
6777
*

0 commit comments

Comments
 (0)