2022-03-15 14:47:07 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Api\v1\Controllers;
|
|
|
|
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Api\v1\Resources\UserResource;
|
|
|
|
use App\Http\Controllers\Controller;
|
2022-03-24 14:58:30 +01:00
|
|
|
use Illuminate\Http\Request;
|
2022-03-15 14:47:07 +01:00
|
|
|
|
|
|
|
class UserController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Get detailed information about a user
|
|
|
|
*
|
2022-08-26 15:57:18 +02:00
|
|
|
* @return \App\Api\v1\Resources\UserResource|\Illuminate\Http\JsonResponse
|
2022-03-15 14:47:07 +01:00
|
|
|
*/
|
2022-03-24 14:58:30 +01:00
|
|
|
public function show(Request $request)
|
2022-03-15 14:47:07 +01:00
|
|
|
{
|
2022-03-24 14:58:30 +01:00
|
|
|
// 2 cases:
|
|
|
|
// - The method is called from a protected route > we return the request's authenticated user
|
|
|
|
// - The method is called from a guest route > we fetch a possible registered user
|
|
|
|
$user = $request->user() ?: User::first();
|
2022-03-15 14:47:07 +01:00
|
|
|
|
|
|
|
return $user
|
|
|
|
? new UserResource($user)
|
|
|
|
: response()->json(['name' => null], 200);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|