Skip to content

Commit fa18f0b

Browse files
committed
Add AuthResponseType for login/logout fields.
1 parent 113c26e commit fa18f0b

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

src/Field/Auth/LoginField.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
use Youshido\GraphQL\Type\Scalar\StringType;
88
use Youshido\GraphQL\Type\NonNullType;
99
use Youshido\GraphQL\Execution\ResolveInfo;
10+
use ProcessWire\GraphQL\Type\Object\AuthResponseType;
11+
use ProcessWire\WireData;
1012

1113
class LoginField extends AbstractField {
1214

1315
public function getType()
1416
{
15-
return new StringType();
17+
return new NonNullType(new AuthResponseType());
1618
}
1719

1820
public function getName()
@@ -37,11 +39,15 @@ public function resolve($value, array $args, ResolveInfo $info)
3739
$username = $args['username'];
3840
$password = $args['password'];
3941
$user = $session->login($username, $password);
42+
$response = new WireData();
4043
if (is_null($user)) {
41-
return 'Failed to login';
44+
$response->statusCode = 401;
45+
$response->message = 'Wrong username and/or password.';
4246
} else {
43-
return 'Login successful';
47+
$response->statusCode = 200;
48+
$response->message = 'Successful login!';
4449
}
50+
return $response;
4551
}
4652

4753
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace ProcessWire\GraphQL\Type\Object;
4+
5+
use Youshido\GraphQL\Type\Object\AbstractObjectType;
6+
use Youshido\GraphQL\Type\NonNullType;
7+
use Youshido\GraphQL\Type\Scalar\IntType;
8+
use Youshido\GraphQL\Type\Scalar\StringType;
9+
10+
class AuthResponseType extends AbstractObjectType {
11+
12+
public function getName()
13+
{
14+
return 'AuthResponse';
15+
}
16+
17+
public function getDescription()
18+
{
19+
return 'Object type that represents the authentication response.';
20+
}
21+
22+
public function build($config)
23+
{
24+
$config->addField('statusCode', [
25+
'type' => new NonNullType(new IntType()),
26+
'description' => 'The authentication status code. E.g. 200 if successful.',
27+
'resolve' => function ($value) {
28+
return (integer) $value->statusCode;
29+
}
30+
]);
31+
32+
$config->addField('message', [
33+
'type' => new NonNullType(new StringType()),
34+
'description' => 'Homan readable message of the authentication reponse. E.g. "successful login!"',
35+
'resolve' => function ($value) {
36+
return (string) $value->message;
37+
}
38+
]);
39+
}
40+
41+
}

0 commit comments

Comments
 (0)