UNPKG

shogun-core

Version:

SHOGUN SDK - Core library for Shogun SDK

162 lines (161 loc) 5.3 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const gun_1 = __importDefault(require("gun")); const oauthConnector_1 = require("./oauthConnector"); const oauthChain = () => { const oauth = new oauthConnector_1.OAuthConnector(); // Initialize the oauth chain object if it doesn't exist if (!gun_1.default.chain.oauth) { gun_1.default.chain.oauth = {}; } /** * Check if OAuth is supported */ gun_1.default.chain.oauth.isSupported = function () { return oauth.isSupported(); }; /** * Get available OAuth providers */ gun_1.default.chain.oauth.getAvailableProviders = function () { return oauth.getAvailableProviders(); }; /** * Initiate OAuth flow with a provider */ gun_1.default.chain.oauth.initiateOAuth = async function (provider) { return await oauth.initiateOAuth(provider); }; /** * Complete OAuth flow */ gun_1.default.chain.oauth.completeOAuth = async function (provider, authCode, state) { return await oauth.completeOAuth(provider, authCode, state); }; /** * Generate credentials from OAuth user info */ gun_1.default.chain.oauth.generateCredentials = async function (userInfo, provider) { return await oauth.generateCredentials(userInfo, provider); }; // === CONVENIENCE METHODS FOR SPECIFIC PROVIDERS === /** * Google OAuth flow */ gun_1.default.chain.oauth.google = { initiate: async function () { return await oauth.initiateOAuth("google"); }, complete: async function (authCode, state) { return await oauth.completeOAuth("google", authCode, state); }, }; /** * GitHub OAuth flow */ gun_1.default.chain.oauth.github = { initiate: async function () { return await oauth.initiateOAuth("github"); }, complete: async function (authCode, state) { return await oauth.completeOAuth("github", authCode, state); }, }; /** * Discord OAuth flow */ gun_1.default.chain.oauth.discord = { initiate: async function () { return await oauth.initiateOAuth("discord"); }, complete: async function (authCode, state) { return await oauth.completeOAuth("discord", authCode, state); }, }; /** * Twitter OAuth flow */ gun_1.default.chain.oauth.twitter = { initiate: async function () { return await oauth.initiateOAuth("twitter"); }, complete: async function (authCode, state) { return await oauth.completeOAuth("twitter", authCode, state); }, }; /** * Setup OAuth for a specific provider with configuration */ gun_1.default.chain.oauth.setup = function (provider, config) { try { console.log(`Setting up OAuth for ${provider}`); // Store configuration securely const configKey = `oauth_config_${provider}`; sessionStorage.setItem(configKey, JSON.stringify(config)); return { success: true, provider, message: `OAuth configured for ${provider}`, }; } catch (error) { console.error(`Error setting up OAuth for ${provider}:`, error); return { success: false, error: error.message, }; } }; /** * Get configuration for a provider */ gun_1.default.chain.oauth.getConfig = function (provider) { try { const configKey = `oauth_config_${provider}`; const config = sessionStorage.getItem(configKey); if (!config) { return { success: false, error: `No configuration found for ${provider}`, }; } return { success: true, config: JSON.parse(config), }; } catch (error) { console.error(`Error getting config for ${provider}:`, error); return { success: false, error: error.message, }; } }; /** * Cleanup OAuth resources */ gun_1.default.chain.oauth.cleanup = function () { try { oauth.cleanup(); // Clear all OAuth related session storage const keysToRemove = []; for (let i = 0; i < sessionStorage.length; i++) { const key = sessionStorage.key(i); if (key && key.startsWith("oauth_")) { keysToRemove.push(key); } } keysToRemove.forEach((key) => sessionStorage.removeItem(key)); return { success: true, message: "OAuth cleanup completed" }; } catch (error) { console.error("Error during OAuth cleanup:", error); return { success: false, error: error.message }; } }; }; exports.default = oauthChain;