Authentication with AccountsJS & GraphQL Modules

Arda TANRIKULU
The Guild
Published in
3 min readNov 16, 2018
AccountsJS just started to use GraphQL-Modules internally

When starting a backend project, two of the biggest concerns will usually be the right structure of the project and authentication. If you could skip thinking and planning about these two, starting a new backend project can be much easier.

If you haven’t checked out our blog post about authentication and authorization in GraphQL Modules, please read that before!

Internally, we use GraphQL-Modules and AccountsJS to help us with those two decisions, GraphQL-Modules helps us solve our architectural problems in modular, schema-first approaches with the power of GraphQL and AccountsJS helps us create our authentication solutions by providing a simple API together with client and server libraries that saves us a lot of the ground work around authentication.

If you haven’t heard about AccountsJS before, it is a set of libraries to provide a full-stack authentication and accounts-management solutions for Javascript.

It is really customizable; so you can write any plugins for your own authentication methods or use the already existing email-password or the Facebook and Twitter OAuth integration packages.

AccountsJS has connector libraries for MongoDB and Redis, but you can write your own database handler by implementing a simple interface.

Accounts JS provide a ready to use GraphQL API if you install their GraphQL library and we are happy to announce that the GraphQL library is now internally built using GraphQL-Modules!

It doesn’t affect people who are not using GraphQL Modules, but it helps the maintainers of AccountsJS and simplifies the integration for GraphQL-Modules-based projects.

How To Implement Server-Side using AccountsJS, GraphQL-Modules and Apollo-Server

First install required dependencies from npm or yarn;

yarn add mongodb @accounts/server @accounts/password @accounts/database-manager @accounts/mongo @accounts/graphql-api @graphql-modules/core apollo-server graphql-import-node

Let’s assume that we’re using MongoDB as our database, password-based authentication and ApolloServer;

Example for using AccountsModule in your GraphQL-Modules project

And we can extend User type with custom fields in our schema, and add a mutation which is restricted to authenticated clients.

Finally let’s define some resolvers for it;

Resolvers for new fields and types

When you print the whole app’s schema, you would see something like above;

Merged Schema generated by GraphQL-Modules

How To Implement Client-Side using AccountsJS, React and Apollo-Client

Now we can create a simple frontend app by using Apollo-Client and AccountsJS client for this backend app. The example below shows some example code that works on these two.

Single App Component that shows AccountsJS flows and some authenticated mutations

As you can see from the example, it can be really easy to create an application that has authentication in modular and future proof approach.

You can learn more about AccountsJS from the docs of this great library for more features such as Two-Factor Authentication and Facebook and Twitter integration using OAuth.

Also you can learn more about GraphQL-Modules on the website and see how you can add GraphQL Modules features into your system in a gradual and selective way.

If you want strict types based on GraphQL Schema, for each module, GraphQL Code Generator has built-in support for GraphQL-Modules based projects. See the docs for more details.

You can check out our example about this integration;
https://github.com/ardatan/graphql-modules-accountsjs-boilerplate

All posts about GraphQL Modules

  1. GraphQL Modules — Feature based GraphQL Modules at scale
  2. Why is True Modular Encapsulation So Important in Large-Scale GraphQL Projects?
  3. Why did we implement our own Dependency Injection library for GraphQL-Modules?
  4. Scoped Providers in GraphQL-Modules Dependency Injection
  5. Writing a GraphQL TypeScript project w/ GraphQL-Modules and GraphQL-Code-Generator
  6. Authentication and Authorization in GraphQL (and how GraphQL-Modules can help)
  7. Authentication with AccountsJS & GraphQL Modules
  8. Manage Circular Imports Hell with GraphQL-Modules

Follow us on GitHub and Medium, we are planning to release many more posts in the next couple of weeks about what we’ve learned using GraphQL in recent years.

--

--