-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
preparing structure for user feature
- Loading branch information
Showing
22 changed files
with
531 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace App\Console; | ||
use Slim\Console\App; | ||
|
||
class Application extends App { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<?php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace App\Facades; | ||
|
||
use App\Http\Middleware\AuthGuard; | ||
use Psr\Container\ContainerInterface; | ||
|
||
class AuthFacade extends Facade | ||
{ | ||
protected static function getFacadeAccessor() | ||
{ | ||
return 'auth'; | ||
} | ||
|
||
public function __construct(ContainerInterface $container) | ||
{ | ||
parent::__construct($container); | ||
} | ||
|
||
/** | ||
* Tạo guard mới. | ||
* | ||
* @return AuthGuard | ||
*/ | ||
public static function factory(): AuthGuard | ||
{ | ||
return new AuthGuard(); | ||
} | ||
|
||
/** | ||
* Lấy guard hiện tại. | ||
* | ||
* @return AuthGuard | ||
*/ | ||
public static function get(): AuthGuard | ||
{ | ||
return app(AuthGuard::class); | ||
} | ||
|
||
/** | ||
* Xác thực người dùng. | ||
* | ||
* @param string $email | ||
* @param string $password | ||
* @return bool | ||
*/ | ||
public static function attempt(string $email, string $password): bool | ||
{ | ||
return self::get()->attempt($email, $password); | ||
} | ||
|
||
/** | ||
* Kiểm tra xem người dùng đã đăng nhập hay chưa. | ||
* | ||
* @return bool | ||
*/ | ||
public static function check(): bool | ||
{ | ||
return self::get()->check(); | ||
} | ||
|
||
/** | ||
* Đăng xuất người dùng. | ||
* | ||
* @return void | ||
*/ | ||
public static function logout(): void | ||
{ | ||
self::get()->logout(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace App\Facades; | ||
|
||
use Psr\Container\ContainerInterface; | ||
|
||
abstract class Facade | ||
{ | ||
protected static $container; | ||
|
||
public static function setContainer(ContainerInterface $container) | ||
{ | ||
static::$container = $container; | ||
} | ||
|
||
public static function getFacadeAccessor() | ||
{ | ||
throw new \RuntimeException('Facade does not have a facade accessor.'); | ||
} | ||
|
||
public static function __callStatic($method, $args) | ||
{ | ||
$instance = static::$container->get(static::getFacadeAccessor()); | ||
|
||
if (! method_exists($instance, $method)) { | ||
throw new \BadMethodCallException(sprintf('Method %s does not exist on facade %s.', $method, get_class($instance))); | ||
} | ||
|
||
return $instance->$method(...$args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace App\Facades; | ||
use Psr\Container\ContainerInterface; | ||
|
||
class UserServiceFacade extends Facade | ||
{ | ||
const FACADE_ACCESSOR = 'user_service'; | ||
|
||
protected $container; | ||
|
||
public function __construct(ContainerInterface $container) | ||
{ | ||
$this->container = $container; | ||
} | ||
|
||
public function __get($name) | ||
{ | ||
return $this->container->get(self::FACADE_ACCESSOR); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace App\Http; | ||
|
||
use App\Http\Middleware\AuthGuard; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Slim\App; | ||
use Slim\Middleware\ErrorMiddleware; | ||
use Slim\Routing\RouteCollector; | ||
|
||
class Kernel | ||
{ | ||
/** | ||
* @var App | ||
*/ | ||
private $app; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param App $app | ||
*/ | ||
public function __construct(App $app) | ||
{ | ||
$this->app = $app; | ||
} | ||
|
||
/** | ||
* Configure the application. | ||
*/ | ||
public function configure() | ||
{ | ||
$this->app->addRoutingMiddleware(); | ||
$this->app->addErrorMiddleware(true, true, true); | ||
|
||
// Add your middleware here | ||
$this->app->add(new AuthGuard($this->app->getContainer()->get('user_repository'))); | ||
|
||
// Add your routes here | ||
$this->mapRoutes(); | ||
} | ||
|
||
/** | ||
* Map the routes. | ||
*/ | ||
private function mapRoutes() | ||
{ | ||
$this->app->get('/', function (ServerRequestInterface $request, ResponseInterface $response) { | ||
return $response->withJson(['message' => 'Hello, world!']); | ||
}); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace App\Http\Middleware; | ||
|
||
use App\Models\User; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Slim\Middleware\JwtAuthentication; | ||
|
||
class AuthGuard | ||
{ | ||
/** | ||
* The user model. | ||
* | ||
* @var \App\Models\User | ||
*/ | ||
protected $user; | ||
|
||
/** | ||
* Create a new auth guard instance. | ||
* | ||
* @param \App\Models\User $user | ||
* @return void | ||
*/ | ||
public function __construct(User $user) | ||
{ | ||
$this->user = $user; | ||
} | ||
|
||
/** | ||
* Handle an incoming request. | ||
* | ||
* @param ServerRequestInterface $request | ||
* @param ResponseInterface $response | ||
* @param callable $next | ||
* @return ResponseInterface | ||
*/ | ||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) | ||
{ | ||
$account = $request->get('account'); | ||
$password = $request->get('password'); | ||
|
||
$user = $this->userService->findUserByAccountAndPassword($account, $password); | ||
|
||
if (!$user) { | ||
abort(401); | ||
} | ||
|
||
// Set the user in the request attributes | ||
$request = $request->withAttribute('user', $user); | ||
|
||
return $next($request, $response); | ||
} | ||
} |
Oops, something went wrong.