| | |
| | | <?php |
| | | namespace JVBase\rest\routes; |
| | | |
| | | use JVBase\managers\Cache; |
| | | use JVBase\registrar\Registrar; |
| | | use JVBase\rest\Rest; |
| | | use JVBase\rest\Route; |
| | |
| | | } |
| | | } |
| | | |
| | | protected function buildAuth(?int $user = null): array |
| | | public static function auth():array |
| | | { |
| | | if (is_user_logged_in()) { |
| | | $user = ($user) ?: get_current_user_id(); |
| | | return [ |
| | | 'authenticated' => true, |
| | | 'user' => $user, |
| | | 'nonces' => $this->getUserNonces($user) |
| | | ]; |
| | | return (new self)->buildAuth(); |
| | | } |
| | | |
| | | protected function buildAuth(?int $user = null): array |
| | | { |
| | | $userId = $user ?? (is_user_logged_in() ? get_current_user_id() : 0); |
| | | $cacheKey = $userId ?: 'guest'; |
| | | |
| | | return Cache::for('auth', 300)->remember($cacheKey, function() use ($userId) { |
| | | if ($userId) { |
| | | return [ |
| | | 'authenticated' => true, |
| | | 'user' => $userId, |
| | | 'nonces' => $this->getUserNonces($userId), |
| | | ]; |
| | | } |
| | | return [ |
| | | 'authenticated' => false, |
| | | 'user' => false, |
| | | 'nonces' => [ |
| | | 'wp_rest' => wp_create_nonce('wp_rest') |
| | | ] |
| | | 'nonces' => ['wp_rest' => wp_create_nonce('wp_rest')], |
| | | ]; |
| | | }); |
| | | } |
| | | protected function getUserNonces(int $userID):array { |
| | | $nonces = [ |