如何将JWT添加到浏览器上的会话存储?



我正试图将JWT添加到会话存储中,我正在使用PHPLaravel(我对这两种技术都很陌生),并且我正在使用Auth路由进行基本注册和登录。我已经创建了成功登录后使用用户信息生成令牌的部分。尽管如此,我还是想将生成的令牌添加到web浏览器会话存储中。之后,我们的想法是从会话存储中获取它,解码它并获得JWT.

中的信息。我在AuthenticatesUsers类的authenticated方法上添加了JWT令牌生成。

protected function authenticated(Request $request, $user)
{
// gets the email from the user
$credentials = request(['email']);

// generate the token with the email
if (! $token = JWTAuth::attempt($credentials)) {
return response()->json(['error' => 'Unauthorized'], 401);
}
// add the token to session storage?
}

我不知道该怎么做。如有任何帮助,不胜感激。

您可以使用以下任何一种方法

// Retrieve a piece of data from the session...
$value = session('key');
// Specifying a default value...
$value = session('key', 'default');
// Store a piece of data in the session...
session(['key' => 'value']);
// Via a request instance...
$request->session()->put('key', 'value');
// Via the global "session" helper...
session(['key' => 'value']);

最新更新