Authentication Laravel + GraphQL

In my previous post I published a basic example with Laravel + GraphQL. The current post is to create the login query.

Authentication Laravel + GraphQL
Authentication Laravel + GraphQL

1. Install Passport#

This example works with Laravel Passport, you can go here to see the instruction to install it.

2. Create the UserType#

I'm working with user_id as primary key for the table user. So, the first step is create the GraphQL UserType.

// app/Base/GraphQL/Type/UserType.php
class UserType extends GraphQLType
{
protected $attributes = [
'name' => 'UserType',
'description' => 'A type of User',
];
public function fields() : array
{
return [
'userID' => [
'type' => Type::nonNull(Type::int()),
'description' => 'User identification, primary key',
'alias' => 'user_id',
],
'langID' => [
'type' => Type::nonNull(Type::string()),
'description' => 'User language',
'alias' => 'lang_id',
],
'name' => [
'type' => Type::nonNull(Type::string()),
'rules' => ['min:3'],
'description' => 'User name',
],
'email' => [
'type' => Type::nonNull(Type::string()),
'rules' => ['min:3'],
'description' => 'User email',
],
'accessToken' => [
'type' => Type::string(),
'description' => 'Session api token passport',
],
...
];
}
}

3. Create the login query#

This query receive two params email and password. And return the user DB values + accesToken.

// app/Base/GraphQL/Publics/Query/UserLoginQuery.php
class UserLoginQuery extends Query
{
protected $attributes = [
'name' => 'login',
'description' => 'A query to login a user, return a user + accessToken',
];
public function type(): Type
{
return GraphQL::type('UserType');
}
public function args(): array
{
return [
'email' => [
'name' => 'email',
'type' => Type::string(),
'rules' => [
'required',
'email',
],
],
'password' => [
'name' => 'password',
'type' => Type::string(),
'rules' => [
'required',
'string',
],
],
];
}
public function resolve($root, $args)
{
return (new LoginLogic(
$args['email'],
$args['password']
))->login();
}
}

4. The Login class#

This is a basic login class, in the code it was optimized to make it more scalable.

// app/Base/Logic/User/LoginLogic.php
class LoginLogic
{
private string $email;
private string $password;
public function __construct(string $email, string $password)
{
$this->email = $email;
$this->password = $password;
}
public function login()
{
if (Auth::check()) {
throw new MessageError(__('user.logged_already'));
}
$user = User::where(['email' => $this->email])->first();
if (! $user || ! Hash::check($this->password, $user->password)) {
throw new MessageError(__('user.login_wrong'));
}
if (! UserStatus::ACTIVE()->isEqual($user->user_status_id)) {
throw new MessageError(__('user.no_active'));
}
$user['accessToken'] = $user->createToken('Albertcito.com')->accessToken;
return $user;
}
}

5. Add the query and type#

In order to make it available, we need to add the query and the type in the graphql configuration file.

// config/graphql.php
'schemas' => [
'default' => [
'query' => [
App\Base\GraphQL\Publics\Query\LangsQuery::class,
App\Base\GraphQL\Publics\Query\LoginQuery::class,
],
'mutation' => [],
'middleware' => ['api'],
'method' => ['post'],
],
],
'types' => [
\App\Base\GraphQL\Type\LangType::class,
\App\Base\GraphQL\Type\UserType::class,
]

6. Run the query#

Now you can go to http://127.0.0.1:8000/graphiql/ and run the query and see the DB values in JSON format.

query login($email: String, $password: String) {
login(email: $email, password: $password) {
userID
name
accessToken
}
}

Variables:

{
"email": "me@albertcito.com",
"password": 123456
}

This is just a login query. I will post soon how to use it. If you would like to see the code, please go to my github. If you would like to see it live please go here.