如何通过HTTP向同一服务器请求访问令牌?

时间:2019-03-23 13:34:29

标签: laravel laravel-passport

虽然我想在请求后获取访问令牌,但遇到了僵局。用户创建部分中没有问题。当我处理数据库的死锁时,新用户就是在这里编写的。死锁之后,我必须清理缓存并重新启动服务器,否则,该项目将无法运行。我该怎么办?

控制器:

public function first_step_register(FirstRegisterRequest $request){

    try {
        $user = User::firstOrCreate([
            'name' => $request->name,
            'email' => $request->email,
            'surname' => $request->surname,
            'phone' => $request->phone,
            'role_id' => $request->role,
            'password' => Hash::make($request->password),
        ]);
        if ($user){

            $http = new Client();

            $response = $http->post(route('passport.token'), [
                'form_params' => [
                    'grant_type' => 'password',
                    'client_id' => '2',
                    'client_secret' => 'T8X7xNsgmw7LEqkGDwk1PhVnWO2NepqT7y48SodE',
                    'username' => $request->email,
                    'password' => $request->password,
                    'scope' => '',
                ],
            ]);

            return response([
                'data' => new FirstRegisterResource($user),
                'token' => json_decode((string) $response->getBody(), true),
            ], Response::HTTP_CREATED);
        }
        else{
            return response([
                'error' => 'Something went wrong, not created'
            ], Response::HTTP_NOT_IMPLEMENTED);
        }
    } catch (\Exception $e) {
        report($e);
        return response([
            'error' => $e
        ],Response::HTTP_NOT_IMPLEMENTED);
    }
}

1 个答案:

答案 0 :(得分:0)

 public function login(LoginRequest $request){
    try{
        $user = User::where('email', $request->email)->first();
        if ($user){
            if(Hash::check($request->password, $user->password)){
                return response([
                    'data' => new EmailVerificationResource($user)
                ],Response::HTTP_ACCEPTED);
            }
            else {
                return response([
                    'error' => 'These password does not match with email.'
                ],Response::HTTP_NOT_ACCEPTABLE);
            }
        }else{
            return response([
                'error' => 'These email does not match our records.'
            ],Response::HTTP_NOT_ACCEPTABLE);
        }
    }
    catch (\Exception $e){
        report($e);
        return response([
            'error' => $e
        ],Response::HTTP_UNAVAILABLE_FOR_LEGAL_REASONS);
    }
}