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.14 kB
"use strict"; // import axios from "axios"; // import { // InitiateGoogleLogin, // HandleGoogleCallback, // } from "../config/OAuth.config"; Object.defineProperty(exports, "__esModule", { value: true }); // // Function to initiate the Google OAuth flow // export const initiateGoogleLogin: InitiateGoogleLogin = ( // clientId: string, // redirectUri?: string // ) => { // const url = `https://accounts.google.com/o/oauth2/v2/auth?client_id=${clientId}&redirect_uri=${redirectUri}&response_type=code&scope=profile email`; // return url; // }; // // Function to handle the Google OAuth callback // export const handleGoogleCallback: HandleGoogleCallback = async ( // code, // clientId, // clientSecret, // redirectUri, // createUser, // findUserByGoogleId // ) => { // try { // // Exchange authorization code for access token // const { data } = await axios.post("https://oauth2.googleapis.com/token", { // code, // client_id: clientId, // client_secret: clientSecret, // redirect_uri: redirectUri, // grant_type: "authorization_code", // }); // const { access_token } = data; // // Use access_token to fetch user profile // const { data: profile } = await axios.get( // "https://www.googleapis.com/oauth2/v1/userinfo", // { // headers: { Authorization: `Bearer ${access_token}` }, // } // ); // // Check if the user exists in the database by Google ID // const existingUser = await findUserByGoogleId(profile.id); // if (!existingUser) { // // If user doesn't exist, create a new one // const newUser = await createUser({ // googleId: profile.id, // name: profile.name, // email: profile.email, // }); // return { // status: 201, // message: "User created successfully", // user: newUser, // }; // } else { // return { status: 409, message: "User already exists!" }; // } // } catch (err: any) { // console.error("Error during Google OAuth:", err); // return { status: 500, message: "Google login failed" }; // } // };