-
Modules/Auth/Services/v2/AuthService.php
Open in GitHubuse Illuminate\Http\Response; use Illuminate\Support\Facades\Hash; use Module\User\Models\User; use Module\Auth\Services\AuthService as Service; class AuthService extends Service { // public function login($request): array { $user = User::whereEmail($request->email)->first(); // Check exist user if (!$user || !Hash::check($request->password, $user->password)) { return $this->response( 'error', Response::HTTP_UNAUTHORIZED, 'invalid email or password', null ); } $token = $user->createToken('test')->plainTextToken; return $this->response( 'success', Response::HTTP_OK, 'Successfully login', ['user' => $user, 'token' => $token] ); } private function response(string $status, int $code, string $message, $data): array { return [ 'status' => $status, 'code' => $code, 'message' => $message, 'data' => $data ?: null ]; } }
-
Modules/Auth/Http/Controllers/v2/AuthController.php
Open in GitHubuse App\Http\Controllers\Controller; use Module\Share\Contracts\Response\ResponseGenerator; use Module\Auth\Http\Requests\v2\LoginRequest; use Module\User\Http\Resources\v2\UserResource; use Module\Auth\Services\v2\AuthService; class AuthController extends Controller implements ResponseGenerator { private AuthService $service; public function __construct() { $this->service = resolve(AuthService::class); } // public function login(LoginRequest $request): \Illuminate\Http\JsonResponse { $login = $this->service->login($request); return $this->res($login['status'], $login['code'], $login['message'], $login['data']); } public function res($status, $code, $message, $data = null): \Illuminate\Http\JsonResponse { return response()->json([ 'status' => $status, 'message' => $message, 'data' => !$data ? null : [ 'user' => new UserResource($data['user']), 'token' => $data['token'] ] ], $code); } }