UNPKG

@zestic/oauth-core

Version:

Framework-agnostic OAuth authentication library with support for multiple OAuth flows

235 lines 6.58 kB
"use strict"; /** * Example implementation showing how to use OAuth Core with GraphQL */ Object.defineProperty(exports, "__esModule", { value: true }); exports.exampleVariables = exports.exampleQueries = void 0; exports.createExampleAdapters = createExampleAdapters; exports.createExampleMagicLinkConfig = createExampleMagicLinkConfig; exports.createExampleGraphQLSetup = createExampleGraphQLSetup; const index_1 = require("./index"); /** * Example implementation of UserAdapter */ class ExampleUserAdapter { constructor() { this.users = new Map(); } async registerUser(email, additionalData) { if (this.users.has(email)) { return { success: false, error: 'User already exists' }; } const user = { id: `user_${Date.now()}`, email, additionalData, createdAt: new Date(), updatedAt: new Date() }; this.users.set(email, user); return { success: true, userId: user.id, message: 'User registered successfully' }; } async userExists(email) { return this.users.has(email); } async getUserByEmail(email) { return this.users.get(email) || null; } } /** * Example implementation of GraphQLAdapter */ class ExampleGraphQLAdapter { async sendMagicLinkMutation(email, magicLinkUrl) { // In a real implementation, you would make a GraphQL mutation to your server // which would then handle sending the email via your email service console.log(`Triggering magic link sending for ${email}: ${magicLinkUrl}`); return { success: true, messageId: `msg_${Date.now()}`, message: 'Magic link mutation triggered successfully' }; } async sendRegistrationConfirmationMutation(email) { console.log(`Triggering registration confirmation for ${email}`); return { success: true, messageId: `msg_${Date.now()}`, message: 'Registration confirmation mutation triggered successfully' }; } } /** * Example storage adapter (in-memory) */ class ExampleStorageAdapter { constructor() { this.storage = new Map(); } async setItem(key, value) { this.storage.set(key, value); } async getItem(key) { return this.storage.get(key) || null; } async removeItem(key) { this.storage.delete(key); } async removeItems(keys) { keys.forEach(key => this.storage.delete(key)); } } /** * Example HTTP adapter */ class ExampleHttpAdapter { async post(url, data, headers) { // In a real implementation, you would make actual HTTP requests console.log(`POST ${url}`, data, headers); return { status: 200, data: { success: true }, headers: {} }; } async get(url, headers) { console.log(`GET ${url}`, headers); return { status: 200, data: { success: true }, headers: {} }; } } /** * Example PKCE adapter */ class ExamplePKCEAdapter { async generateCodeChallenge() { // In a real implementation, you would use proper cryptographic functions const codeVerifier = this.generateRandomString(128); const codeChallenge = this.base64URLEncode(codeVerifier); // Simplified return { codeChallenge, codeChallengeMethod: 'S256', codeVerifier }; } async generateState() { return this.generateRandomString(32); } generateRandomString(length) { const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~'; let result = ''; for (let i = 0; i < length; i++) { result += chars.charAt(Math.floor(Math.random() * chars.length)); } return result; } base64URLEncode(str) { // Simplified base64 encoding - use proper crypto in production return Buffer.from(str).toString('base64') .replace(/\+/g, '-') .replace(/\//g, '_') .replace(/=/g, ''); } } /** * Create example adapters */ function createExampleAdapters() { return { storage: new ExampleStorageAdapter(), http: new ExampleHttpAdapter(), pkce: new ExamplePKCEAdapter(), user: new ExampleUserAdapter(), graphql: new ExampleGraphQLAdapter() }; } /** * Create example magic link configuration */ function createExampleMagicLinkConfig() { return { baseUrl: 'https://your-app.com/auth/callback', tokenEndpoint: '/oauth/token', expirationMinutes: 15, customParams: { source: 'magic_link' } }; } /** * Example GraphQL server setup (pseudo-code) */ function createExampleGraphQLSetup() { const adapters = createExampleAdapters(); const magicLinkConfig = createExampleMagicLinkConfig(); const context = (0, index_1.createGraphQLContext)(adapters, magicLinkConfig); return { typeDefs: index_1.typeDefs, resolvers: index_1.resolvers, context }; } /** * Example GraphQL queries and mutations */ exports.exampleQueries = { register: ` mutation Register($input: RegistrationInput!) { register(input: $input) { success message code } } `, sendMagicLink: ` mutation SendMagicLink($input: SendMagicLinkInput!) { sendMagicLink(input: $input) { success message code } } ` }; /** * Example variables for the mutations */ exports.exampleVariables = { register: { input: { email: 'user@example.com', additionalData: { firstName: 'John', lastName: 'Doe', preferences: { newsletter: true } }, codeChallenge: 'example_code_challenge', codeChallengeMethod: 'S256', redirectUri: 'https://your-app.com/auth/callback', state: 'example_state_value' } }, sendMagicLink: { input: { email: 'user@example.com', codeChallenge: 'example_code_challenge', codeChallengeMethod: 'S256', redirectUri: 'https://your-app.com/auth/callback', state: 'example_state_value' } } }; //# sourceMappingURL=example.js.map