私はPHPとLaravelを初めて使用しますが、次の問題があります。
Laravelは、次のステートメントを使用して作成された、すぐに使用できるログインシステムを提供していることを知っています。
php artisan make:auth
問題は、このシステムがデータベースと直接対話することです。
私のLaravelアプリケーションはフロントエンドのみを実装しているため、私の状況は異なります。すべてのビジネスロジックは、RESTWebサービスを公開するJavaバックエンドアプリケーションによって処理されます。
基本的に、Laravelフロントエンドアプリケーションは次のようなものです。
1)ビューを示すログインフォーム(ユーザー名とパスワード)。
2)前のフォームから送信を受信し、バックエンドアプリケーションのREST Webサービスを呼び出す(挿入されたユーザー名とパスワードを含む認証ヘッダーを持つ要求を送信する)メソッドを含むコントローラークラス。
バックエンドアプリケーションは、このように(ユーザーが許可されている場合)応答としてユーザー情報を含むJSONオブジェクトを(以前のLaravelコントローラーメソッドに)返します...
{
"userName": "Painkiller",
"email": "[email protected]",
"enabled": true
}
...または、ユーザーが許可されていない場合は、次のようになります...
{
"timestamp": 1485183649134,
"status": 401,
"error": "Unauthorized",
"message": "Credenziali non valide",
"path": "/Extranet/login"
}
私の最初のアイデアは、これらの操作を実行するカスタムコントローラーを作成することでした。
私はフロントエンド開発にはそれほど興味がありませんが、それはうまくいくはずだと思いますが...私はLaravelアーキテクチャとLaravelロジックから離れています。
そのため、私のLaravelアプリケーションはデータベースと直接通信できませんが、Laravelアーキテクチャを採用することはできるのではないかと考えています。
そのため、Laravelプロジェクトには、デフォルトで、標準のLaravelログインシステムを表す\ app \ Http \ Controllers \ Auth \ LoginControllerクラスがあると思っていました。
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
}
}
次の行が含まれています。
use AuthenticatesUsers;
私はPHPにあまり興味がありませんが、この行は正確に何をしますか?最初は、AuthenticatesUsers機能がLoginControllerに追加されたと思いましたが、拡張と継承の概念に関連する動作です。
とにかく、AuthenticatesUsersクラスには、ログインを処理するロジックの実装が含まれているようです。このメソッドは次のとおりです。
public function login(Request $request)
{
$this->validateLogin($request);
// If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
if ($this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
if ($this->attemptLogin($request)) {
return $this->sendLoginResponse($request);
}
// If the login attempt was unsuccessful we will increment the number of attempts
// to login and redirect the user back to the login form. Of course, when this
// user surpasses their maximum number of attempts they will get locked out.
$this->incrementLoginAttempts($request);
return $this->sendFailedLoginResponse($request);
}
だから私は考えています:このメソッドをオーバーライドしてWebサービスの呼び出しを実行し、ログインしたユーザーに関連するJSONオブジェクトを取得し、この情報をセッションに入れてユーザーページにリダイレクトできますか?
これは私の目的にとって賢い解決策でしょうか?
ルートを作成し、ログインメソッドを上書きするか、独自のルートを作成するだけで、そうする必要はありません。
ルート:
// authentication routes
Route::get('/login', 'Auth\LoginController@showLoginForm')->name('adminGetLogin');
Route::post('/login', 'Auth\LoginController@login')->name('adminPostLogin');
Route::get('/logout', 'Auth\LoginController@logout')->name('adminLogout');
// reset password routes
Route::get('/password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('showResetForm');
Route::post('/password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('sendResetEmail');
Route::get('/password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('showResetForm');
Route::post('/password/reset', 'Auth\ResetPasswordController@reset')->name('resetPassword');
ウェブ
public function login(Request $request){
if (! Auth::attempt(['email' => $request->email, 'password' => $request->password, 'is_active' => 1])) {
// do whatever yo desire
return redirect('/home');
}
REST API
public function login(Request $request){
if (Auth::attempt(['email' => $request->email, 'password' => $request->password, 'is_active' => 1])) {
// do whatever yo desire
$user = Auth::user();
return response()->json([
'Error' => ['type' => 'success', 'desc' => 'Login successful', 'code' => 0],
'Response' => $user
]);
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加