OneGraph Auth Helpers

4.0.2 · active · verified Wed Apr 22

The `onegraph-auth` library provides essential client-side authentication helpers for integrating web applications with a multitude of third-party services via the OneGraph platform. It streamlines the complex OAuth login and logout flows, manages session state, and securely handles token storage in the browser. The package is currently at version 4.0.2, indicating a mature and actively maintained library. Its primary differentiator is its ability to abstract away the intricacies of individual OAuth providers, offering a unified authentication experience through OneGraph's single GraphQL endpoint. This simplifies development by reducing the need to manage multiple API keys and authentication mechanisms for services like GitHub, Stripe, and others. It is specifically designed for browser environments, leveraging the global `window` object for its operations.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the OneGraphAuth client, check login status for GitHub, and initiate the OAuth login flow if the user is not authenticated. It also shows how to retrieve the necessary authorization headers after a successful login, suitable for use with a GraphQL client.

import OneGraphAuth from 'onegraph-auth';

const APP_ID: string = process.env.NEXT_PUBLIC_ONEGRAPH_APP_ID ?? ''; // Ensure APP_ID is loaded from environment variables

if (!APP_ID) {
  console.error('OneGraph APP_ID is not configured. Please set NEXT_PUBLIC_ONEGRAPH_APP_ID.');
} else {
  const auth = new OneGraphAuth({
    appId: APP_ID,
  });

  async function handleGitHubAuth() {
    try {
      let isLoggedIn = await auth.isLoggedIn('github');

      if (!isLoggedIn) {
        console.log('Not logged in to GitHub, initiating login...');
        await auth.login('github');
        isLoggedIn = await auth.isLoggedIn('github');
      }

      if (isLoggedIn) {
        console.log('Successfully logged in to GitHub!');
        // Example: Get auth headers for an Apollo Client or other API calls
        const headers = auth.authHeaders();
        console.log('Auth Headers:', headers);
      } else {
        console.log('GitHub login failed or user did not grant consent.');
      }
    } catch (error) {
      console.error('Error during GitHub authentication:', error);
    }
  }

  handleGitHubAuth();
}

view raw JSON →