@@ -48,6 +48,12 @@ class User
4848 */
4949 protected array $ tokens = [];
5050
51+ /**
52+ * Expiry used when tokens are lazily minted
53+ * @var int
54+ */
55+ protected $ tokenLifetime ;
56+
5157 /**
5258 * All errors caught
5359 * @var array{
@@ -103,8 +109,7 @@ public function __construct($data, $session = true)
103109 ? strtotime ($ sessionLifetime )
104110 : (time () + intval ($ sessionLifetime ));
105111
106- $ this ->tokens ['access ' ] = $ this ->generateToken ($ sessionLifetime );
107- $ this ->tokens ['refresh ' ] = $ this ->generateToken ($ sessionLifetime + 259200 );
112+ $ this ->tokenLifetime = $ sessionLifetime ;
108113
109114 if (function_exists ('crash ' )) {
110115 crash ()->context (['user ' => array_filter ([
@@ -299,10 +304,12 @@ public function resetPassword(string $newPassword): bool
299304 */
300305 public function getAuthInfo (): object
301306 {
307+ $ tokens = $ this ->tokens ();
308+
302309 $ dataToReturn = (object ) [
303310 'user ' => $ this ->get (),
304- 'accessToken ' => $ this -> tokens ['access ' ] ?? null ,
305- 'refreshToken ' => $ this -> tokens ['refresh ' ] ?? null ,
311+ 'accessToken ' => $ tokens ['access ' ] ?? null ,
312+ 'refreshToken ' => $ tokens ['refresh ' ] ?? null ,
306313 ];
307314
308315 if (count ($ this ->roles )) {
@@ -325,6 +332,11 @@ public function getAuthInfo(): object
325332 */
326333 public function tokens (): array
327334 {
335+ if (!isset ($ this ->tokens ['access ' ])) {
336+ $ this ->tokens ['access ' ] = $ this ->generateToken ($ this ->tokenLifetime );
337+ $ this ->tokens ['refresh ' ] = $ this ->generateToken ($ this ->tokenLifetime + 259200 );
338+ }
339+
328340 return $ this ->tokens ;
329341 }
330342
@@ -420,10 +432,10 @@ public function get()
420432 $ hidden = array_merge (Config::get ('hidden ' ), [Config::get ('roles.key ' )]);
421433 $ passwordKey = Config::get ('password.key ' );
422434
435+ unset($ userData [$ passwordKey ], $ userData ['remember_token ' ]);
436+
423437 if (count ($ hidden ) > 0 ) {
424438 foreach ($ hidden as $ item ) {
425- // array_key_exists, not isset: a present-but-null column
426- // (like the roles column before first assign) must hide too
427439 if (array_key_exists ($ item , $ userData )) {
428440 unset($ userData [$ item ]);
429441 }
0 commit comments