Easy build API using Laravel and GraphQL (JWT Authentication) part 3

JWT Authentication GraphQL

Ardani Rohman
Aug 9, 2017 · 2 min read

JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed. JWTs can be signed using a secret (with the HMAC algorithm) or a public/private key pair using RSA

1. Install JWT Package

JWT very useful for API Authentication Stateless and easy to integrate with Laravel using this package

Installation JWT, you can follow this instruction here and for handling error here.

2. Create Controller For Authentication

we use header paramAuthorization: Bearer {yourtokenhere} every request to GraphQL. First create controller for authentication like below

add new endpoint in web.php for login

Route::post(‘graphql/login’, ‘AuthenticateController@authenticate’);

3. Create Query With Authentication

for example we use query myprofile and access to myprofile must have authentication from JWT. First we create MyProfileQuery.php

authentication process happen in function authorize, in there we must check JWT auth is valid to continue process but If authentication invalid will show message unauthorized at result of GraphQL and dont forget append new query in config graphql.php

add mutation class in GraphQL config

4. Demo

demo authentication GraphQL Laravel

don’t worry about full source code, you can access it below :).

Click ❤ and follow me to get other articles. thanks

related articles

  1. Visual Studio Code Environment For Laravel
  2. Easy Build API Using Laravel And GraphQL (Query) part 1
  3. Easy Build API Using Laravel And GraphQL (Mutation) part 2


We help scale up your startup to the next level

Ardani Rohman

Written by

Remote Software Engineer at Agency Analytics. Ready for collaboration 🎯 find me: http://ardani.github.io


We help scale up your startup to the next level

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade