laravel - Laravel 和 vue,使用 JWT 注册后自动登录
问题描述
我正在开发一个以 vue 作为前端和 laravel 8 api 的项目。我正在使用 JWT 进行身份验证。我知道必须在用户登录之前生成令牌,但是有没有办法在用户注册后自动登录?
解决方案
用户注册成功后返回token:
/*This is your register function, this is after you succesfully saved the user in the database.
Remember to use JWTAuth*/
/*YOUR REGISTER LOGIC HERE, THEN*/
if($user->save()){
$credentials = ["email"=>$user->email,"password"=>password_from_request];
try{
if(JWTAuth::attempt($credentials)){
return $this->respondWithToken($token);
}
}
catch (JWTException $e) {
// something went wrong whilst attempting to encode the token
return response()->json(['error' => $e], 500);
}
}else
return $this->userRegisterFailedResponse();
/*Your custom response in case the register fails*/
您可以在JWT 文档中查看 respondWithToken 函数的实现
推荐阅读
- php - Yii2 Date I/O Generator 他们没有 DatePicker 形式
- javascript - 如何将圆形标记的面积设置为与属性值成比例?
- python - 使用 Postgresql 数据库在 Django 中运行迁移命令时出错
- python - 在python或c++中使用sql根据输入数据输出输出样本
- python - 将 scikit-learn 管道保存到文件
- javascript - 显示 div 时启动函数
- javascript - 检测类的静态和实例方法的参数和返回值
- python - 如何在终端上更改我的 python 版本?
- c++ - 为每个顶点绘制一个立方体
- sql - 获取第一个值和最后一个值