| 
1 | 1 | <?php  | 
 | 2 | +/**  | 
 | 3 | + * Register the login mutation to the WPGraphQL Schema  | 
 | 4 | + *  | 
 | 5 | + * @package WPGraphQL\JWT_Authentication  | 
 | 6 | + * @since 0.0.1  | 
 | 7 | + */  | 
 | 8 | + | 
2 | 9 | namespace WPGraphQL\JWT_Authentication;  | 
3 | 10 | 
 
  | 
4 | 11 | use GraphQL\Type\Definition\ResolveInfo;  | 
5 | 12 | use WPGraphQL\AppContext;  | 
6 | 13 | 
 
  | 
 | 14 | +/**  | 
 | 15 | + * Class - Login  | 
 | 16 | + */  | 
7 | 17 | class Login {  | 
8 |  | - | 
9 | 18 | 	/**  | 
10 |  | -	 * Register the login mutation to the WPGraphQL Schema  | 
 | 19 | +	 * Register the mutation.  | 
11 | 20 | 	 */  | 
12 | 21 | 	public static function register_mutation() {  | 
13 |  | - | 
14 |  | -		register_graphql_mutation( 'login', [  | 
15 |  | -			'description' => __( 'Login a user. Request for an authToken and User details in response', 'wp-graphql-jwt-authentication' ),  | 
16 |  | -			'inputFields' => [  | 
17 |  | -				'username' => [  | 
18 |  | -					'type' => [ 'non_null' => 'String' ],  | 
19 |  | -					'description' => __( 'The username used for login. Typically a unique or email address depending on specific configuration', 'wp-graphql-jwt-authentication' ),  | 
20 |  | -				],  | 
21 |  | -				'password' => [  | 
22 |  | -					'type' => [ 'non_null' => 'String' ],  | 
23 |  | -					'description' => __( 'The plain-text password for the user logging in.', 'wp-graphql-jwt-authentication' ),  | 
24 |  | -				],  | 
25 |  | -			],  | 
26 |  | -			'outputFields' => [  | 
27 |  | -				'authToken' => [  | 
28 |  | -					'type' => 'String',  | 
29 |  | -					'description' => __( 'JWT Token that can be used in future requests for Authentication', 'wp-graphql-jwt-authentication' ),  | 
 | 22 | +		register_graphql_mutation(  | 
 | 23 | +			'login',  | 
 | 24 | +			[  | 
 | 25 | +				'description'         => __( 'Login a user. Request for an authToken and User details in response', 'wp-graphql-jwt-authentication' ),  | 
 | 26 | +				'inputFields'         => [  | 
 | 27 | +					'username' => [  | 
 | 28 | +						'type'        => [ 'non_null' => 'String' ],  | 
 | 29 | +						'description' => __( 'The username used for login. Typically a unique or email address depending on specific configuration', 'wp-graphql-jwt-authentication' ),  | 
 | 30 | +					],  | 
 | 31 | +					'password' => [  | 
 | 32 | +						'type'        => [ 'non_null' => 'String' ],  | 
 | 33 | +						'description' => __( 'The plain-text password for the user logging in.', 'wp-graphql-jwt-authentication' ),  | 
 | 34 | +					],  | 
30 | 35 | 				],  | 
31 |  | -				'refreshToken' => [  | 
32 |  | -					'type' => 'String',  | 
33 |  | -					'description' => __( 'A JWT token that can be used in future requests to get a refreshed jwtAuthToken. If the refresh token used in a request is revoked or otherwise invalid, a valid Auth token will NOT be issued in the response headers.', 'wp-graphql-jwt-authentication' ),  | 
 | 36 | +				'outputFields'        => [  | 
 | 37 | +					'authToken'    => [  | 
 | 38 | +						'type'        => 'String',  | 
 | 39 | +						'description' => __( 'JWT Token that can be used in future requests for Authentication', 'wp-graphql-jwt-authentication' ),  | 
 | 40 | +					],  | 
 | 41 | +					'refreshToken' => [  | 
 | 42 | +						'type'        => 'String',  | 
 | 43 | +						'description' => __( 'A JWT token that can be used in future requests to get a refreshed jwtAuthToken. If the refresh token used in a request is revoked or otherwise invalid, a valid Auth token will NOT be issued in the response headers.', 'wp-graphql-jwt-authentication' ),  | 
 | 44 | +					],  | 
 | 45 | +					'user'         => [  | 
 | 46 | +						'type'        => 'User',  | 
 | 47 | +						'description' => __( 'The user that was logged in', 'wp-graphql-jwt-authentication' ),  | 
 | 48 | +					],  | 
34 | 49 | 				],  | 
35 |  | -				'user' => [  | 
36 |  | -					'type' => 'User',  | 
37 |  | -					'description' => __( 'The user that was logged in', 'wp-graphql-jwt-authentication' ),  | 
38 |  | -				],  | 
39 |  | -			],  | 
40 |  | -			'mutateAndGetPayload' => function( $input, AppContext $context, ResolveInfo $info ) {  | 
41 |  | - | 
42 |  | -				/**  | 
43 |  | -				 * Login the user in and get an authToken and user in response  | 
44 |  | -				 */  | 
45 |  | -				return Auth::login_and_get_token( sanitize_user( $input['username'] ), trim( $input['password'] ) );  | 
46 |  | - | 
47 |  | -			},  | 
48 |  | -		]);  | 
49 |  | - | 
 | 50 | +				'mutateAndGetPayload' => function( $input, AppContext $context, ResolveInfo $info ) {  | 
 | 51 | +					// Login the user in and get an authToken and user in response.  | 
 | 52 | +					return Auth::login_and_get_token( sanitize_user( $input['username'] ), trim( $input['password'] ) );  | 
 | 53 | +				},  | 
 | 54 | +			]  | 
 | 55 | +		);  | 
50 | 56 | 	}  | 
51 |  | - | 
52 | 57 | }  | 
0 commit comments