GenTux\Jwt\JwtToken::createToken PHP Method

createToken() public method

The default algorithm used is HS256. To set a custom one, set the env variable JWT_ALGO.
public createToken ( GenTux\Jwt\JwtPayloadInterface | array $payload, string | null $secret = null, string | null $algo = null ) : JwtToken
$payload GenTux\Jwt\JwtPayloadInterface | array
$secret string | null
$algo string | null
return JwtToken
    public function createToken($payload, $secret = null, $algo = null)
    {
        $algo = $algo ?: $this->algorithm();
        $secret = $secret ?: $this->secret();
        if ($payload instanceof JwtPayloadInterface) {
            $payload = $payload->getPayload();
        }
        $newToken = $this->jwt->createToken($payload, $secret, $algo);
        $token = clone $this;
        $token->setToken($newToken);
        return $token;
    }

Usage Example

コード例 #1
0
 public function it_encodes_to_json_as_a_string_representation_of_the_token(JwtDriverInterface $jwt)
 {
     $driver = new FirebaseDriver();
     $jwt = new JwtToken($driver);
     $token = $jwt->createToken(['exp' => time() + 100], 'secret');
     $serialized = json_encode(['token' => $token]);
     $decoded = json_decode($serialized);
     if (!is_string($decoded->token) || strlen($decoded->token) < 1) {
         throw new FailureException('Token was not json encoded.');
     }
 }