diff --git a/app/Common/Interface/LoginInterface.php b/app/Common/Interface/LoginInterface.php new file mode 100644 index 0000000..cda4caf --- /dev/null +++ b/app/Common/Interface/LoginInterface.php @@ -0,0 +1,10 @@ +logger->request()->info( + sprintf('[%s] Login Attempt by %s', + static::class, + $identifier + ) + ); + } + + /** + * @param array $data + * @return bool + */ + protected function validateCredentials(array $data): bool + { + return !empty($data); + } +} \ No newline at end of file diff --git a/app/Service/Api/Login/LoginFactory.php b/app/Service/Api/Login/LoginFactory.php new file mode 100644 index 0000000..d5a3f31 --- /dev/null +++ b/app/Service/Api/Login/LoginFactory.php @@ -0,0 +1,50 @@ + PasswordLoginService::class + ]; + + /** + * @param ContainerInterface $container + */ + public function __construct( + private readonly ContainerInterface $container + ) {} + + /** + * @param string $type + * @return PasswordLoginService + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function get(string $type): + PasswordLoginService + { + $handlerClass = self::HANDLER_MAP[$type] ?? null; + + return match (true) { + $handlerClass !== null => $this->container->get($handlerClass), + default => throw new ErrException('类不存在') + }; + } +} \ No newline at end of file diff --git a/app/Service/Api/Login/LoginService.php b/app/Service/Api/Login/LoginService.php new file mode 100644 index 0000000..11c0bba --- /dev/null +++ b/app/Service/Api/Login/LoginService.php @@ -0,0 +1,43 @@ +loginFactory->get($type)->authenticate($username, $password); + return ['success']; + } catch (\Exception $e) { + return ['error']; + } + } +} \ No newline at end of file diff --git a/app/Service/Api/Login/PasswordLoginService.php b/app/Service/Api/Login/PasswordLoginService.php new file mode 100644 index 0000000..9c9bdfb --- /dev/null +++ b/app/Service/Api/Login/PasswordLoginService.php @@ -0,0 +1,46 @@ +validateCredentials([ + 'username' => $username, + 'password' => $password, + ])) throw new ErrException('error'); + + $this->logAttempt($username); + + // + return [ + 'code' => 'success' + ]; + } + + /** + * @param string $type + * @return bool + */ + public function supports(string $type): bool + { + return $type === 'password'; + } +} \ No newline at end of file