Registry / auth-security / onegraph-auth

onegraph-auth

JSON →
library4.0.2jsnpmunverified

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.

npm install onegraph-auth
INSTALL
IMPORT
SIG · ONEGRAPH-AUTH
O
onegraph-auth
auth-securityjavascriptv4.0.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

OneGraphAuth
import OneGraphAuth from 'onegraph-auth';
const OneGraphAuth = require('onegraph-auth');
The library primarily uses ES Module syntax for imports. Direct CommonJS `require` might lead to issues, especially in modern build environments, though it could sometimes work with bundlers.
OneGraphAuthOptions
import OneGraphAuth, { OneGraphAuthOptions } from 'onegraph-auth';
import { OneGraphAuthOptions } from 'onegraph-auth/dist/types';
When using TypeScript, the `OneGraphAuthOptions` interface can be imported as a named export for type-checking the constructor options. Avoid importing directly from internal `dist/types` paths.
AuthClientInstance
const auth: OneGraphAuth = new OneGraphAuth({ appId: 'YOUR_APP_ID' });
While `OneGraphAuth` is the class name, the instantiated client object is often referred to as an 'auth client instance'. TypeScript users can explicitly type the instance for better tooling.

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(); }
Debug
Known issues
gotchaThe `onegraph-auth` library is designed exclusively for client-side (browser) environments. It relies on the global `window` object for its operations, including handling OAuth redirects and storing tokens. Attempting to use it in a Node.js or server-side rendering (SSR) context without appropriate shims or checks will result in runtime errors related to `window` being undefined.
fix
Ensure `OneGraphAuth` instances are only created and methods are called within a `useEffect` hook in React, or within `window.onload` in vanilla JavaScript, to guarantee a browser environment. For SSR, conditionally import or initialize the library client-side.
affects: >=1.0.0
gotchaA valid OneGraph `appId` is absolutely required for the `OneGraphAuth` client to function. This `appId` links your application to your OneGraph project and its configured services. If the `appId` is missing or incorrect, authentication flows will fail silently or with generic errors, preventing users from logging in.
fix
Always provide your `appId` during `OneGraphAuth` instantiation. It is recommended to load this from environment variables (e.g., `process.env.NEXT_PUBLIC_ONEGRAPH_APP_ID` in Next.js) and include a runtime check to ensure it's defined before initialization.
affects: >=1.0.0
gotchaOneGraph handles OAuth flows via popup windows or redirects. Browser popup blockers can interfere with the `auth.login('service')` method, preventing the authentication window from appearing. Users might not understand why login isn't working, leading to a poor user experience.
fix
Instruct users to disable popup blockers for your domain, or provide clear UI feedback if a popup is blocked (e.g., by catching the promise rejection if the browser API allows detecting it, or by observing if `isLoggedIn` doesn't change after an expected login attempt).
affects: >=1.0.0
gotchaWhen integrating `onegraph-auth` with GraphQL clients like Apollo, it's crucial to ensure that the `authHeaders()` method is called dynamically for each request. The authentication token can change (e.g., after a user logs in or out of a new service), and static headers will quickly become stale, leading to unauthorized requests.
fix
For Apollo Client, use a `request` handler to dynamically set context headers, as shown in the README example: `request: (operation) => operation.setContext({headers: auth.authHeaders()})`. Alternatively, use `onegraph-apollo-client` which handles this automatically.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: onegraph_auth_1.default is not a constructor
Attempting to import `onegraph-auth` using CommonJS `require` syntax (`const OneGraphAuth = require('onegraph-auth');`) in an environment expecting ES Modules, or when a bundler fails to correctly transpile.
fix
Use ES Module import syntax: `import OneGraphAuth from 'onegraph-auth';`. Ensure your build configuration (e.g., `tsconfig.json`'s `module` field, Webpack/Rollup config) correctly handles ES Modules.
ReferenceError: window is not defined
The `onegraph-auth` library is being initialized or used in a non-browser environment, such as a Node.js server during server-side rendering (SSR) or in a build process, where the global `window` object is not available.
fix
Wrap `OneGraphAuth` instantiation and method calls in client-side checks. For React, use `useEffect` or dynamic imports. For frameworks like Next.js, ensure components using `onegraph-auth` are only rendered client-side or explicitly wrap in `typeof window !== 'undefined'` checks.
Auth popup was blocked by the browser.
The browser's built-in popup blocker prevented the OAuth login window initiated by `auth.login('service')` from appearing, disrupting the authentication flow.
fix
This is a user-side issue. Provide clear instructions to users to allow popups from your domain. In some cases, frameworks might offer ways to detect if a popup was blocked, allowing for custom error messages or fallback UI.
403 Forbidden: Missing authentication token
GraphQL requests made to OneGraph are missing the `Authorization` header, or the token provided in the header is invalid or expired. This often happens if `auth.authHeaders()` is not called dynamically or if the user is not logged in.
fix
Verify that `auth.login('service')` has been successfully completed and that `auth.authHeaders()` is correctly used to populate the `Authorization` header for all outgoing API requests. If using Apollo Client, ensure the `request` function dynamically calls `auth.authHeaders()` for each operation.
Upgrade
Version history
4.0.2latest on npm
Audit
Dependencies
onegraph-apollo-clientoptionalOptional dependency for simplified integration with Apollo Client, handling authentication headers automatically.
Agent activity
37 hits · last 30 days
node
34
OpenAI (training)
1
Resources