/** * @param $userId * @param $email * @param bool $forever * * @return string */ public static function makeJWTByUser($userId, $email, $forever = false) { if (\Config::get('df.allow_forever_sessions') === false) { $forever = false; } $claims = ['sub' => $userId, 'user_id' => $userId, 'email' => $email, 'forever' => $forever]; /** @type Payload $payload */ $payload = JWTFactory::make($claims); /** @type Token $token */ $token = \JWTAuth::encode($payload); $tokenValue = $token->get(); static::setTokenMap($payload, $tokenValue); return $tokenValue; }
public function testToken() { $app = app(); $config = $app->config; $user = Config::get("jwt::user"); $identifier = Config::get("jwt::identifier"); $customer = Customer::whereEmail('*****@*****.**')->first(); $token = JWTAuth::fromUser($customer); $subject = JWTAuth::getSubject($token); // SOS! returns Customer->id === 1 $this->assertEquals($customer->id, $subject); $claims = new stdClass(); $claims->currency_id = 3; $claims->language_id = 1; $token = JWTAuth::encode(1, array('currency_id' => 3, 'language_id' => 1)); $decoded = JWTAuth::decode($token->get()); /********************** Prints ****************************/ /* * Tymon\JWTAuth\Payload Object ( [value:protected] => Array ( [currency_id] => 3 [language_id] => 1 [iss] => http://localhost [sub] => 1 [iat] => 1417101103 [exp] => 1417187503 ) ) */ }
/** * Attempt to authenticate a user using the given credentials. * * @param array $credentials * * @return array */ public static function attempt(array $credentials) { $api = Api::get()->addSession($credentials['user'], $credentials['password']); $payload = \JWTFactory::make(['id' => $api->getSession()]); $token = \JWTAuth::encode($payload); return $token->get(); }