我已经通过移动应用程序的API为登录用户设置了Passport路由。但是,移动应用程序的一部分使用WebView显示封闭内容,另一部分则从API中提取其他内容。
一旦用户使用API登录到应用程序,我需要他们同时登录到Web内容。
但是,没有使用API创建会话。在显示WebView时,如何通过API同时登录和注销用户?
我试过在我的
API\LoginController.php
要执行登录:
protected function sendLoginResponse(Request $request)
{
if ($request->hasSession()) {
$request->session()->regenerate();
} else {
// Login user for PWA pages.
\Session::start();
\Auth::login($this->guard()->user());
}
$this->clearLoginAttempts($request);
return $this->authenticated($request, $this->guard()->user());
}
protected function authenticated(Request $request, User $user): ?JsonResponse
{
return response()->json(['token' => $user->createToken(config('app.name'))->accessToken], 200);
}
这扩展了基本的laravel默认值
LoginController.php
但重写这些方法以支持JSON响应。
关联的路由:
Route::post('login')->name('api.auth.login')->uses('API\\Auth\\LoginController@login');
当通过API调用登录时,登录工作正常,但不会将会话持久化到WebView中。