php Laravel返回null token,但正在添加用户

jtoj6r0c  于 2023-05-05  发布在  PHP
关注(0)|答案(3)|浏览(132)

我在Laravel项目中使用postman添加用户。我得到空令牌,但用户被添加,为什么?
{“token”:null }
如何修复此错误?我使用Laravel 5.6,这是我的用户模型:

<?php

    namespace App;

    use Tymon\JWTAuth\Contracts\JWTSubject;
    use Illuminate\Notifications\Notifiable;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    class User extends Authenticatable
    {
        use Notifiable;

        /**
         * The attributes that are mass assignable.
         *
         * @var array
         */
        protected $fillable = [
            'name', 'email', 'password','username','lastname','tel','tel',
        ];

        /**
         * The attributes that should be hidden for arrays.
         *
         * @var array
         */
        protected $hidden = [
            'password', 'remember_token',
        ];

        public function getJWTIdentifier()
        {
            return $this->getKey();
        }

        /**
         * Return a key value array, containing any custom claims to be added to the JWT.
         *
  * @return array
         */
        public function getJWTCustomClaims()
        {
            return [];
        }
    }

这是我的寄存器控制器:

<?php
    
    namespace App\Http\Controllers;
    use App\Http\Controllers\Controller;
    use Illuminate\Http\Request;
    use App\User;
    use JWTFactory;
    use JWTAuth;
    use Validator;
    use Response;
    
    class APIRegisterController extends Controller
    {
        //
        public function register( Request $request){
            $validator = Validator::make($request -> all(),[
             'email' => 'required|string|email|max:255|unique:users',
             'username' =>'required',
             'tel' => 'required',
             'name' => 'required',
             'lastname' => 'required',
             'adress' => 'required',
             'password'=> 'required'
            ]);
    
            if ($validator -> fails()) {
                # code...
                return response()->json($validator->errors());
    
            }
    
            User::create([
                'name' => $request->get('name'),
                'email' => $request->get('email'),
                'tel' => $request->get('tel'),
                'username' => $request->get('username'),
                'lastname' => $request->get('lastname'),
                'adress' => $request->get('adress'),
                'password'=> bcrypt($request->get('password'))
            ]);
            $user = User::first();
            $token = JWTAuth::fromUser($user);
    
            return Response::json( compact('token'));
    
    
        }
    }

我已经得到一个错误,这是我的问题question,我修复了它。

cwtwac6a

cwtwac6a1#

我以前已经遇到过类似的问题,但我意识到我的$user->password保存的是加密的密码,而不是login()所需的纯文本。更改您的代码以调用

$token = JWTAuth::fromUser($user);

其中$user保存来自$request的纯文本密码。

mklgxw1f

mklgxw1f2#

我解决了我的问题添加如果条件后注册功能创建用户

if ($this->loginAfterSignUp) {
    $token = $this->login($request);
}
return $this->respondWithToken($token);
vh0rcniy

vh0rcniy3#

laravel jwt auth token not recive
$token = Auth::login($user);
$token = null;

相关问题