UNPKG

universal_authentication

Version:

Seamless and Secure Authentication for Modern Web Applications: Easily integrate OTP-based email verification, Google OAuth, GitHub, Microsoft, and Okta login into your Node.js app. Modular, flexible, and database-agnostic, this package simplifies user au

64 lines (63 loc) 2.18 kB
"use strict"; // import axios from "axios"; Object.defineProperty(exports, "__esModule", { value: true }); // // Function to generate GitHub OAuth URL // export const initiateGithubLogin = (clientId: string): string => { // return `https://github.com/login/oauth/authorize?client_id=${clientId}`; // }; // // Function to handle GitHub OAuth callback // export const handleGithubCallback = async ( // code: string, // clientId: string, // clientSecret: string, // createUser: Function, // findUserByGithubId: Function // ): Promise<any> => { // try { // if (!code) { // return { status: 400, message: "Authorization code is missing." }; // } // // Exchange authorization code for access token // const { data: tokenData } = await axios.post( // "https://github.com/login/oauth/access_token", // { // client_id: clientId, // client_secret: clientSecret, // code, // }, // { // headers: { Accept: "application/json" }, // } // ); // const accessToken = tokenData.access_token; // if (!accessToken) { // return { status: 400, message: "Failed to retrieve access token." }; // } // // Fetch user profile from GitHub // const { data: profile } = await axios.get("https://api.github.com/user", { // headers: { Authorization: `Bearer ${accessToken}` }, // }); // // Extract relevant user details // const { id: githubId, login: username, name } = profile; // // Check if the user exists in the database // const existingUser = await findUserByGithubId(githubId); // if (!existingUser) { // // If user doesn't exist, create a new one // const newUser = await createUser({ // githubId, // username, // name, // }); // return { // status: 201, // message: "User created successfully", // user: newUser, // }; // } else { // return { status: 409, message: "User already exists!" }; // } // } catch (err) { // console.error("Error during GitHub OAuth:", err); // return { status: 500, message: "GitHub login failed" }; // } // };