在前端,Laravel用于创建基本模板,但由Angular控制.在后端,laravel用于创建一个宁静的API.
我有几条这样的路线:
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网址.
在我的后端,我为我的API路由实现了OAuth,这确保访问这些路由的用户是真实的. OAuth库使用Laravel的Auth :: once()来确保用户凭据正确,然后生成access_token.由于Auth :: once()是一个“无状态”函数,因此不使用会话或cookie,我不能使用Auth :: check()来确保在呈现仪表板页面之前对用户进行身份验证.
我该如何检查用户是否尝试访问domain.com/dashboard?当我将用户从/ login转发到/ dashboard时,我应该在标题中发送access_token吗?或者我应该实现Laravel基于会话/ cookie的身份验证?
编辑:根据这个:Adding http headers to window.location.href in Angular app我无法使用Authorization标头将用户转发到仪表板页面.
为了重用我的移动应用程序的API,我强烈希望使用某种基于令牌的身份验证.
解决方法
我认为有几个教程可供Lavarel和AngularJS使用.我更喜欢Python并使用Flask,但以下内容看起来很有趣:
> Simple AngularJS Authentication with JWT:AngularJS配置
> Token-Based Authentication for AngularJS and Laravel Apps:与Laravel的联系
> JSON Web Token Tutorial: An Example in Laravel and AngularJS