Skip to content

Commit 68c2787

Browse files
committed
Basic login support.
1 parent f6d0e65 commit 68c2787

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

src/Field/Auth/LoginField.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace ProcessWire\GraphQL\Field\Auth;
4+
5+
use Youshido\GraphQL\Field\AbstractField;
6+
use Youshido\GraphQL\Config\Field\FieldConfig;
7+
use Youshido\GraphQL\Type\Scalar\StringType;
8+
use Youshido\GraphQL\Type\NonNullType;
9+
use Youshido\GraphQL\Execution\ResolveInfo;
10+
11+
class LoginField extends AbstractField {
12+
13+
public function getType()
14+
{
15+
return new StringType();
16+
}
17+
18+
public function getName()
19+
{
20+
return 'login';
21+
}
22+
23+
public function getDescription()
24+
{
25+
return 'Allows you to authenticate into the app.';
26+
}
27+
28+
public function build(FieldConfig $config)
29+
{
30+
$config->addArgument('username', new NonNullType(new StringType()));
31+
$config->addArgument('password', new NonNullType(new StringType()));
32+
}
33+
34+
public function resolve($value, array $args, ResolveInfo $info)
35+
{
36+
$session = \ProcessWire\wire('session');
37+
$username = $args['username'];
38+
$password = $args['password'];
39+
$user = $session->login($username, $password);
40+
if (is_null($user)) {
41+
return 'Failed to login';
42+
} else {
43+
return 'Login successful';
44+
}
45+
}
46+
47+
}

src/Schema.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
use Youshido\GraphQL\Config\Schema\SchemaConfig;
44
use Youshido\GraphQL\Schema\AbstractSchema;
5+
use ProcessWire\GraphQL\Settings;
56
use ProcessWire\GraphQL\Field\Pages\PagesField;
67
use ProcessWire\GraphQL\Field\TemplatedPageArray\TemplatedPageArrayField;
78
use ProcessWire\GraphQL\Field\Debug\DbQueryCountField;
8-
use ProcessWire\GraphQL\Settings;
9+
use ProcessWire\GraphQL\Field\Auth\LoginField;
10+
911

1012

1113
class Schema extends AbstractSchema {
@@ -26,10 +28,11 @@ public function build(SchemaConfig $config)
2628

2729
// Debugging
2830
if (Settings::module()->debug) {
29-
$query->addFields([
30-
new DbQueryCountField()
31-
]);
31+
$query->addField(new DbQueryCountField());
3232
}
33+
34+
// Auth
35+
$query->addfield(new LoginField());
3336
}
3437

3538
public function getName()

0 commit comments

Comments
 (0)