UNPKG

login-auth-services

Version:

Authentication services for Google, GitHub, Microsoft, okta and multi-factor authentication using OTP.

62 lines (61 loc) 3.17 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.handleGithubCallback = exports.getUserInfoGithub = exports.getTokenGithub = exports.getAuthURLGithub = void 0; const axios_1 = __importDefault(require("axios")); const query_string_1 = __importDefault(require("query-string")); const databaseService_1 = require("../services/databaseService"); const dbService = databaseService_1.DatabaseService.getInstance(); const getAuthURLGithub = (options) => { const params = query_string_1.default.stringify({ client_id: options.clientId, redirect_uri: options.redirectUri, client_secret: options.clientSecret, }); return `https://github.com/login/oauth/authorize?${params}`; }; exports.getAuthURLGithub = getAuthURLGithub; const getTokenGithub = (code, options) => __awaiter(void 0, void 0, void 0, function* () { const response = yield axios_1.default.post("https://github.com/login/oauth/access_token", query_string_1.default.stringify({ client_id: options.clientId, client_secret: options.clientSecret, code, redirect_uri: options.redirectUri, }), { headers: { "Content-Type": "application/x-www-form-urlencoded", Accept: "application/json" } }); return response.data.access_token; }); exports.getTokenGithub = getTokenGithub; const getUserInfoGithub = (accessToken) => __awaiter(void 0, void 0, void 0, function* () { const response = yield axios_1.default.get("https://api.github.com/user", { headers: { Authorization: `Bearer ${accessToken}` }, }); return response.data; }); exports.getUserInfoGithub = getUserInfoGithub; const handleGithubCallback = (code, options) => __awaiter(void 0, void 0, void 0, function* () { try { const accessToken = yield (0, exports.getTokenGithub)(code, options); const profile = yield (0, exports.getUserInfoGithub)(accessToken); // Now update the user's GitHub ID in the database const user = yield dbService.findUserByEmail(profile.email); if (user) { yield dbService.updateUser(user.id, { githubId: profile.id.toString() }); } return profile; } catch (err) { throw err; } }); exports.handleGithubCallback = handleGithubCallback;