GenerateApiToken
in package
Use case for generating API tokens for users.
Tokens are hashed before storage for security. Only the hash is stored in the database; the plaintext token is returned to the user and never stored. This protects tokens in case of database breach.
Tags
Table of Contents
Constants
- API_TOKEN_EXPIRATION = 30 * 24 * 60 * 60
- API token expiration time in seconds (default: 30 days).
Properties
- $repository : UserRepositoryInterface
- User repository.
- $tokenHasher : TokenHasher
- Token hasher.
Methods
- __construct() : mixed
- Create a new GenerateApiToken use case.
- execute() : string
- Execute to generate an API token.
Constants
API_TOKEN_EXPIRATION
API token expiration time in seconds (default: 30 days).
private
mixed
API_TOKEN_EXPIRATION
= 30 * 24 * 60 * 60
Properties
$repository
User repository.
private
UserRepositoryInterface
$repository
$tokenHasher
Token hasher.
private
TokenHasher
$tokenHasher
Methods
__construct()
Create a new GenerateApiToken use case.
public
__construct(UserRepositoryInterface $repository[, TokenHasher|null $tokenHasher = null ]) : mixed
Parameters
- $repository : UserRepositoryInterface
-
User repository
- $tokenHasher : TokenHasher|null = null
-
Token hasher
execute()
Execute to generate an API token.
public
execute(int $userId) : string
Returns the plaintext token to the user but stores only the hash.
Parameters
- $userId : int
-
The user ID
Tags
Return values
string —The generated API token (plaintext)