Piotoru

override thirdPartySignInUpPOST and turning off create session for sign up

Mar 22nd, 2023 (edited)
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.                         apis: (originalImplementation) => {
  2.               return {
  3.                 ...originalImplementation,
  4.                 thirdPartySignInUpPOST: async function (input) {
  5.                   try {
  6.                     if (
  7.                       originalImplementation.thirdPartySignInUpPOST ===
  8.                       undefined
  9.                     ) {
  10.                       throw Error('Should never come here');
  11.                     }
  12.  
  13.                     const response =
  14.                       await originalImplementation.thirdPartySignInUpPOST(
  15.                         input,
  16.                       );
  17.  
  18.                     if (response.status === 'OK' && response.createdNewUser) {
  19.                       const accessToken =
  20.                         response.authCodeResponse.access_token;
  21.  
  22.                       switch (input.provider.id) {
  23.                         case 'google':
  24.                           const authHeader = `Bearer ${accessToken}`;
  25.                           const response = await axios.get(
  26.                             'https://www.googleapis.com/oauth2/v1/userinfo',
  27.                             {
  28.                               params: {
  29.                                 alt: 'json',
  30.                               },
  31.                               headers: {
  32.                                 Authorization: authHeader,
  33.                               },
  34.                             },
  35.                           );
  36.  
  37.                        
  38.                           const userInfo = response.data;
  39.                           const username = userInfo.name;
  40.                           const profilePhoto = userInfo.picture;
  41.  
  42.                           (email === undefined || email === null) && { id };
  43.  
  44.                           const isVerified = userInfo.verified_email;
  45.                           // await usersService.createUser({
  46.                           //   pseudonym: accessToken.pseudonym,
  47.                           //   username: accessToken.pseudonym,
  48.                           //   profilePhoto: accessToken.profilePhoto,
  49.                           //   emailpassword_users: {
  50.                           //     create: undefined,
  51.                           //     connectOrCreate: {
  52.                           //       where: {
  53.                           //         user_id: accessToken.id,
  54.                           //         email: accessToken.email,
  55.                           //       },
  56.                           //       create: undefined,
  57.                           //     },
  58.                           //     connect: {
  59.                           //       user_id: accessToken.id,
  60.                           //       email: accessToken.email,
  61.                           //     },
  62.                           //   },
  63.                           // });
  64.                           break;
  65.                         default:
  66.                           return null;
  67.                       }
  68.                     }
  69.  
  70.                     return await originalImplementation.thirdPartySignInUpPOST?.(
  71.                       input,
  72.                     );
  73.                   } catch (e: any) {
  74.                     if (
  75.                       e.message === 'Cannot sign up as email already exists'
  76.                     ) {
  77.                       return {
  78.                         status: 'GENERAL_ERROR',
  79.                         message:
  80.                           'Seems like you already have an account with another method. Please use that instead.',
  81.                       };
  82.                     }
  83.                     throw e;
  84.                   }
  85.                 },
  86.                 //disable create session for sign up
  87.                 emailPasswordSignUpPOST: async function (input) {
  88.                   if (
  89.                     originalImplementation.emailPasswordSignUpPOST === undefined
  90.                   ) {
  91.                     throw new Error('Should never come here');
  92.                   }
  93.                   input.userContext.isSignUp = true;
  94.                   return originalImplementation.emailPasswordSignUpPOST(input);
  95.                 },
  96.               };
  97.             },
  98.  
  99. getProfileInfo: async (accessTokenAPIResponse) => {
  100.    const accessToken = accessTokenAPIResponse.access_token;
  101.    const authHeader = `Bearer ${accessToken}`;
  102.    const response = await axios.get(
  103.       'https://www.googleapis.com/oauth2/v1/userinfo',
  104.       {
  105.         params: {
  106.            alt: 'json',
  107.         },
  108.         headers: {
  109.           Authorization: authHeader,
  110.         },
  111.        },
  112.      );
  113.  
  114.    const userInfo = response.data;
  115.    const id = userInfo.id;
  116.      const email = userInfo.email;
  117.  
  118.      (email === undefined || email === null) && { id };
  119.  
  120.      const isVerified = userInfo.verified_email;
  121.       return {
  122.         id,
  123.         email: {
  124.           id: email,
  125.           isVerified,
  126.         },
  127.       };
  128.  }
Advertisement
Add Comment
Please, Sign In to add comment