LaravelとAngularを使用してWebアプリを作成しています。
フロントエンドでは、Laravelを使用して基本的なテンプレートを作成しますが、それ以外の場合はAngularによって制御されます。バックエンドでは、laravelを使用してRESTfulAPIを作成します。
私はこのようないくつかのルートを持っています:
Route::group(['domain' => 'domain.com'], function() {
Route::get('/', ['as' => 'home', function () {
return view('homepage');
}]);
Route::get('/login', ['as' => 'login', function () {
return view('login');
}]);
//users should be authenticated before accessing this page
Route::get('/dashboard', ['as' => 'dashboard', function () {
return view('dashboard');
}]);
});
Route::group(['domain' => 'api.domain.com', 'middleware' => ['oauth']], function() {
Route::post('/post/create', ['uses' => 'PostController@store']);
Route::get('/post/{id}', ['uses' => 'PostController@show']);
//other API endpoints
// ...
});
domain.com/dashboard
認証されたユーザーのみが自分のURLにアクセスできるようにしたい。
私のバックエンドでは、APIルートにOAuthを実装しており、これらのルートにアクセスするユーザーが本物であることを確認しています。LaravelAuth::once()
はOAuthライブラリによって使用され、ユーザーの資格情報が正しいことを確認してから、を生成しaccess_token
ます。以来はAuth::once()
何のセッションやクッキーが利用されていない「ステートレス」機能であり、私が使用できないAuth::check()
ダッシュボードページがレンダリングされる前に必ずユーザーが認証されていることを確認します。
アクセスしようとしているユーザーdomain.com/dashboard
が認証されているかどうかを確認するにはどうすればよいですか?私は、送信する必要がありaccess_token
、私はからユーザーを転送するとき、ヘッダーに/login
しますか/dashboard
?または、Laravelのセッション/ Cookieベースの認証を実装する必要がありますか?
編集:これによると:Angularアプリのwindow.location.hrefにhttpヘッダーを追加するAuthorization
ヘッダー付きのダッシュボードページにユーザーを転送できません。
APIをモバイルアプリに再利用するために、ある種のトークンベースの認証を使用することを強く好みます。
@Pierre Cordier @Mr_Antivius回答ありがとうございます。問題を洞察し、JWTをいじくり回すことができましたが、最終的には解決策が得られませんでした。
認証されたユーザーのみがアクセスできるようにするdomain.com/dashboard
には、ハイブリッドセッションとOAuth認証システムを実装する必要がありました。アプリの他の場所で必要なユーザー権限システムがあるため、(Laravelのすぐに使用できる認証システムではなく)Sentinelを使用することにしました。このライブラリをOAuthサーバーに使用します。
これが私のコントローラーで行うことです:
POST domain.com/auth/authenticate
:
public function processLogin(Request $request)
{
$credentials = [
'email' => $request->input('username'),
'password' => $request->input('password'),
];
try
{
$sentinel = Sentinel::authenticate($credentials);
}
catch (\Cartalyst\Sentinel\Checkpoints\ThrottlingException $e)
{
$response = ['error' => [$e->getMessage()]];
$httpStatus = 429;
return response()->json($response, $httpStatus);
}
catch (\Cartalyst\Sentinel\Checkpoints\NotActivatedException $e)
{
$response = ['error' => [$e->getMessage()]];
$httpStatus = 401;
return response()->json($response, $httpStatus);
}
if ($sentinel) //user credentials correct
{
//get oauth token
$oauthToken = Authorizer::issueAccessToken();
$response = ['success' => true, 'user' => ['id' => $sentinel->id, 'email' => $sentinel->email]] + $oauthToken;
$httpStatus = 200;
}
else
{
$response = ['success' => false, 'error' => ['Incorrect credentials']];
$httpStatus = 401;
}
return response()->json($response, $httpStatus);
}
OAuthライブラリがユーザーを認証するために調べる方法は次のとおりです。
public function verifyAuth($email, $password)
{
$credentials = [
'email' => $email,
'password' => $password,
];
if ($user = Sentinel::stateless($credentials))
{
return $user->id;
}
else
{
return false;
}
}
これにより、次のような応答が作成されます。
{
"success": true,
"user": {
"id": 1,
"email": "[email protected]"
},
"access_token": "6a204bd89f3c8348afd5c77c717a097a",
"token_type": "Bearer",
"expires_in": 28800,
"refresh_token": "092a8e1a7025f700af39e38a638e199b"
}
これが誰かを助けることを願っています
補足:に投稿した場合、センチネルのCookieを認識できなかったためPOST
、domain.com/auth/authenticate
ではなくにリクエストを送信しています。に変更してみましたが、まだ何もありません。多分私は何か間違ったことをしているのですか?api.domain.com/auth/authenticate
domain.com/dashboard
api.domain.com
domain
config/session.php
.domain.com
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加