AuthenticatesUsers login()メソッドをオーバーライドして、このLaravelアプリケーションでREST Webサービスの呼び出しを含むカスタムログインを実装できますか?

アンドレアノビリ

私はPHPLaravelを初めて使用しますが、次の問題があります。

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"
}

私の最初のアイデアは、これらの操作を実行するカスタムコントローラーを作成することでした。

  1. コントローラは、ログインフォームの送信(ユーザーが入力した資格情報を含む)を処理します。
  2. コントローラはバックエンドWebサービスを呼び出し、JSONオブジェクトを取得します。これは、JSONオブジェクトが承認されたユーザーを表す場合、ユーザーを表すPHPモデルオブジェクトに変換します。
  3. コントローラはこのモデルオブジェクトをセッションに配置し、この情報をセッションから取得できる次のユーザーページにリダイレクトします。

私はフロントエンド開発にはそれほど興味がありませんが、それはうまくいくはずだと思いますが...私は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オブジェクトを取得し、この情報をセッションに入れてユーザーページにリダイレクトできますか?

これは私の目的にとって賢い解決策でしょうか?

Amr El Mahdy

ルートを作成し、ログインメソッドを上書きするか、独自のルートを作成するだけで、そうする必要はありません。

ルート:

// 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]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ