SHARE
TWEET

Untitled

a guest Jun 18th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Request, Response } from "express";
  2. import { successResponse } from "../../Helpers/Responses/successResponse";
  3. import { ApiConfig } from "../../Configs/Api.Config";
  4. import { errorResponse } from "../../Helpers/Responses";
  5. const { OAuth2Client } = require("google-auth-library");
  6. const { google } = require("googleapis");
  7.  
  8.  
  9. export default async function googleLogin(request: Request, response: Response) {
  10.   console.log(ApiConfig.THIRD_PARTY_LOGIN_API.GOOGLE.SCOPES);
  11.   const code = request.query.code;
  12.   const KEYS = require("../../Configs/" + ApiConfig.THIRD_PARTY_LOGIN_API.GOOGLE.KEY);
  13.   const CLIENT_SECRET = KEYS.web.client_secret;
  14.   const CLIENT_ID = KEYS.web.client_id;
  15.   const REDIRECT_URIS = KEYS.web.redirect_uris[1];
  16.  
  17.   const oAuth2Client = new OAuth2Client(
  18.     CLIENT_ID,
  19.     CLIENT_SECRET,
  20.     REDIRECT_URIS
  21.   );
  22.  
  23.   if (!code) {
  24.     const authorizeUrl = oAuth2Client.generateAuthUrl({
  25.       access_type: "online",
  26.       scope: ApiConfig.THIRD_PARTY_LOGIN_API.GOOGLE.SCOPES,
  27.     });
  28.     // const theResponse = successResponse({
  29.     //   isRedirect: true,
  30.     //   url: authorizeUrl
  31.     // }, "success redirect");
  32.     response.redirect(authorizeUrl);
  33.   } else {
  34.     async function main() {
  35.       oAuth2Client.getToken(code, async (err, toekenResponse) => {
  36.         if (!err) {
  37.           oAuth2Client.setCredentials(toekenResponse);
  38.           const data = await getGoogleData(oAuth2Client);
  39.           return response.json(data);
  40.         } else {
  41.           console.log(err);
  42.         }
  43.       });
  44.     }
  45.     main().catch(console.error);
  46.   }
  47. }
  48.  
  49.  
  50. async function getGoogleData(auth): Promise<object> {
  51.   const service = google.people({ version: "v1", auth });
  52.   return new Promise((resolve, reject) => {
  53.     service.people.get({
  54.       resourceName: "people/me",
  55.       personFields: ["names", "emailAddresses", "photos", "birthdays", "genders"],
  56.     }, (err, res) => {
  57.       if (err) {
  58.         console.log(err);
  59.         reject(errorResponse(err, "error happpend"));
  60.       }
  61.       resolve(successResponse(res.data, "success"));
  62.     });
  63.   });
  64. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top