Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
9 / 9 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| AuthController | |
100.00% |
9 / 9 |
|
100.00% |
2 / 2 |
3 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| loginJWT | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers; |
| 4 | |
| 5 | use Firebase\JWT\JWT; |
| 6 | use Illuminate\Http\Request; |
| 7 | |
| 8 | use Core\Services\{ |
| 9 | Usuario\LoginEmailSenhaService, |
| 10 | }; |
| 11 | |
| 12 | class AuthController extends Controller |
| 13 | { |
| 14 | public function __construct( |
| 15 | private LoginEmailSenhaService $loginEmailSenhaService |
| 16 | ) {} |
| 17 | |
| 18 | /** |
| 19 | * @OA\Post( |
| 20 | * tags={"auth"}, |
| 21 | * path="/api/auth/login/jwt", |
| 22 | * description="Login com JWT", |
| 23 | * @OA\RequestBody( |
| 24 | * @OA\MediaType(mediaType="application/json;charset=UTF-8", |
| 25 | * @OA\Schema( |
| 26 | * required={"email", "password"}, |
| 27 | * @OA\Property( |
| 28 | * property="email", |
| 29 | * type="string", |
| 30 | * example="admin@desativemeemprod.com", |
| 31 | * ), |
| 32 | * @OA\Property( |
| 33 | * property="password", |
| 34 | * type="string", |
| 35 | * example="asdf", |
| 36 | * ), |
| 37 | * ), |
| 38 | * ), |
| 39 | * ), |
| 40 | * @OA\Response(response="2XX", description="OK"), |
| 41 | * ) |
| 42 | */ |
| 43 | public function loginJWT(Request $r) |
| 44 | { |
| 45 | $this->validate($r, [ |
| 46 | 'email' => 'required|email', |
| 47 | 'password' => 'required' |
| 48 | ]); |
| 49 | |
| 50 | $ok = $this->loginEmailSenhaService->execute($r->email, $r->password); |
| 51 | |
| 52 | if (!$ok) { |
| 53 | return response()->json('Usuário ou senha inválidos', 401); |
| 54 | } |
| 55 | |
| 56 | $token = JWT::encode( |
| 57 | ['email' => $r->email], |
| 58 | env('JWT_KEY'), |
| 59 | 'HS256' |
| 60 | ); |
| 61 | |
| 62 | return $token; |
| 63 | } |
| 64 | } |