Skip to content

Commit 01ecafe

Browse files
committed
Add registration types
joselfonseca#17
1 parent e59101c commit 01ecafe

File tree

4 files changed

+105
-8
lines changed

4 files changed

+105
-8
lines changed

graphql/auth.graphql

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,20 @@ type ForgotPasswordResponse {
3939
}
4040

4141
input ForgotPasswordInput {
42-
email: String!
42+
email: String! @rules(apply: ["required", "email"])
4343
}
4444

4545
input NewPasswordWithCodeInput {
46-
email: String!
47-
token: String!
48-
password: String!
46+
email: String! @rules(apply: ["required", "email"])
47+
token: String! @rules(apply: ["required", "string"])
48+
password: String! @rules(apply: ["required", "confirmed", "min:8"])
49+
password_confirmation: String!
50+
}
51+
52+
input RegisterInput {
53+
name: String! @rules(apply: ["required", "string"])
54+
email: String! @rules(apply: ["required", "email"])
55+
password: String! @rules(apply: ["required", "confirmed", "min:8"])
4956
password_confirmation: String!
5057
}
5158

@@ -55,4 +62,5 @@ extend type Mutation {
5562
logout: LogoutResponse! @field(resolver: "Joselfonseca\\LighthouseGraphQLPassport\\GraphQL\\Mutations\\Logout@resolve")
5663
forgotPassword(input: ForgotPasswordInput! @spread): ForgotPasswordResponse! @field(resolver: "Joselfonseca\\LighthouseGraphQLPassport\\GraphQL\\Mutations\\ForgotPassword@resolve")
5764
updateForgottenPassword(input: NewPasswordWithCodeInput @spread): ForgotPasswordResponse! @field(resolver: "Joselfonseca\\LighthouseGraphQLPassport\\GraphQL\\Mutations\\ResetPassword@resolve")
65+
register(input: RegisterInput @spread): AuthPayload! @field(resolver: "Joselfonseca\\LighthouseGraphQLPassport\\GraphQL\\Mutations\\Register@resolve")
5866
}

readme.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ Lighthouse GraphQL Passport Auth (Laravel 5.8 / Lighthouse ^3.2)
88

99
GraphQL mutations for Laravel Passport using Lighthouse PHP ^3.2.
1010

11+
## Tutorial
12+
13+
You can see [this tutorial](https://ditecnologia.com/2019/06/24/graphql-auth-with-passport-and-lighthouse-php/) for installation and usage.
14+
1115
## Installation
1216

1317
**Make sure you have [Laravel Passport](https://laravel.com/docs/5.8/passport) installed.**
@@ -77,13 +81,20 @@ type ForgotPasswordResponse {
7781
}
7882

7983
input ForgotPasswordInput {
80-
email: String!
84+
email: String! @rules(apply: ["required", "email"])
8185
}
8286

8387
input NewPasswordWithCodeInput {
84-
email: String!
85-
token: String!
86-
password: String!
88+
email: String! @rules(apply: ["required", "email"])
89+
token: String! @rules(apply: ["required", "string"])
90+
password: String! @rules(apply: ["required", "confirmed", "min:8"])
91+
password_confirmation: String!
92+
}
93+
94+
input RegisterInput {
95+
name: String! @rules(apply: ["required", "string"])
96+
email: String! @rules(apply: ["required", "email"])
97+
password: String! @rules(apply: ["required", "confirmed", "min:8"])
8798
password_confirmation: String!
8899
}
89100

@@ -93,6 +104,7 @@ extend type Mutation {
93104
logout: LogoutResponse! @field(resolver: "Joselfonseca\\LighthouseGraphQLPassport\\GraphQL\\Mutations\\Logout@resolve")
94105
forgotPassword(input: ForgotPasswordInput!): ForgotPasswordResponse! @field(resolver: "Joselfonseca\\LighthouseGraphQLPassport\\GraphQL\\Mutations\\ForgotPassword@resolve")
95106
updateForgottenPassword(input: NewPasswordWithCodeInput): ForgotPasswordResponse! @field(resolver: "Joselfonseca\\LighthouseGraphQLPassport\\GraphQL\\Mutations\\ResetPassword@resolve")
107+
register(input: RegisterInput @spread): AuthPayload! @field(resolver: "Joselfonseca\\LighthouseGraphQLPassport\\GraphQL\\Mutations\\Register@resolve")
96108
}
97109
```
98110

src/GraphQL/Mutations/Register.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Joselfonseca\LighthouseGraphQLPassport\GraphQL\Mutations;
4+
5+
use GraphQL\Type\Definition\ResolveInfo;
6+
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
7+
8+
class Register extends BaseAuthResolver
9+
{
10+
/**
11+
* @param $rootValue
12+
* @param array $args
13+
* @param \Nuwave\Lighthouse\Support\Contracts\GraphQLContext|null $context
14+
* @param \GraphQL\Type\Definition\ResolveInfo $resolveInfo
15+
* @return array
16+
* @throws \Exception
17+
*/
18+
public function resolve($rootValue, array $args, GraphQLContext $context = null, ResolveInfo $resolveInfo)
19+
{
20+
$model = app(config('auth.providers.users.model'));
21+
$input = collect($args)->except('password_confirmation')->toArray();
22+
$input['password'] = bcrypt($input['password']);
23+
$model->fill($input);
24+
$model->save();
25+
$credentials = $this->buildCredentials([
26+
'username' => $args['email'],
27+
'password' => $args['password'],
28+
]);
29+
$user = $model->where('email', $args['email'])->first();
30+
$response = $this->makeRequest($credentials);
31+
$response['user'] = $user;
32+
return $response;
33+
}
34+
35+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Joselfonseca\LighthouseGraphQLPassport\Tests\Integration\GraphQL\Mutations;
4+
5+
use Joselfonseca\LighthouseGraphQLPassport\Tests\User;
6+
use Joselfonseca\LighthouseGraphQLPassport\Tests\TestCase;
7+
8+
class Register extends TestCase
9+
{
10+
11+
function test_it_registers_a_user()
12+
{
13+
$this->createClient();
14+
$response = $this->postGraphQL([
15+
'query' => 'mutation {
16+
register(input: {
17+
name: "My Name",
18+
email: "jose@example.com",
19+
password: "123456789qq",
20+
password_confirmation: "123456789qq"
21+
}) {
22+
access_token
23+
refresh_token
24+
user {
25+
id
26+
name
27+
email
28+
}
29+
}
30+
}'
31+
]);
32+
$responseBody = json_decode($response->getContent(), true);
33+
$this->assertArrayHasKey('register', $responseBody['data']);
34+
$this->assertArrayHasKey('access_token', $responseBody['data']['register']);
35+
$this->assertArrayHasKey('refresh_token', $responseBody['data']['register']);
36+
$this->assertArrayHasKey('user', $responseBody['data']['register']);
37+
$this->assertArrayHasKey('id', $responseBody['data']['register']['user']);
38+
$this->assertArrayHasKey('name', $responseBody['data']['register']['user']);
39+
$this->assertArrayHasKey('email', $responseBody['data']['register']['user']);
40+
}
41+
42+
}

0 commit comments

Comments
 (0)