folkloreinc/laravel-graphql

User passort authentication

PaulJasiul opened this issue · 1 comments

Hello, I have a problem with my RegisterMutation mutation. Any ideas how I can return token information ($response->getBody())?

`

public function type()
{
	return GraphQL::type('user');
}

public function rules(): array
{
	return [
		'first_name' => 'required|string|max:255',
		'last_name' => 'required|string|max:255',
		'email' => 'required|string|email|max:255|unique:users',
		'password' => 'required|string|min:6',
	];
}

public function args(): array
{
	return [
		'first_name' => [
			'name' => 'first_name',
			'type' => Type::nonNull(Type::string()),
		],
		'last_name' => [
			'name' => 'last_name',
			'type' => Type::nonNull(Type::string()),
		],
		'email' => [
			'name' => 'email',
			'type' => Type::nonNull(Type::string()),
		],
		'password' => [
			'name' => 'password',
			'type' => Type::nonNull(Type::string()),
		],
	];
}

public function resolve($root, $args)
{
	try {
		event(new Registered($this->create($args)));

		$http = new Client();

		$response = $http->post(env('APP_URL') . '/oauth/token', [
			'form_params' => [
				'grant_type' => 'password',
				'client_id' => env('PASSWORD_CLIENT_ID'),
				'client_secret' => env('PASSWORD_CLIENT_SECRET'),
				'username' => $args['email'],
				'password' => $args['password'],
				'remember' => false,
				'scope' => '',
			],
		]);

		return json_decode((string)$response->getBody(), true);
	} catch (\Exception $e) {
		return response()->json([
			'error' => 'invalid_credentials',
			'message' => 'The user credentials were incorrect.'
		], 401);
	}
}

private function create(array $data): User
{
	return User::create([
		'first_name' => $data['first_name'],
		'last_name' => $data['last_name'],
		'email' => $data['email'],
		'password' => $data['password'],
	]);
}

`

I had the same problem and resolved it by this way :

$params = [
  'grant_type'    => 'password',
  'client_id'     => 'client-id',
  'client_secret' => 'client-secret',
  'username'      => 'mohammadfouladgarphp@gmail.com',
  'password'      => 'my-password',
];

$request = app('request');
$request->request->add($params);
$proxy = \Illuminate\Http\Request::create('oauth/token', 'POST');
$response = \Illuminate\Support\Facades\Route::dispatch($proxy);

if (200 === $response->status()) {
    return json_decode($response->getcontent(), true);
}