Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- apis: (originalImplementation) => {
- return {
- ...originalImplementation,
- thirdPartySignInUpPOST: async function (input) {
- try {
- if (
- originalImplementation.thirdPartySignInUpPOST ===
- undefined
- ) {
- throw Error('Should never come here');
- }
- const response =
- await originalImplementation.thirdPartySignInUpPOST(
- input,
- );
- if (response.status === 'OK' && response.createdNewUser) {
- const accessToken =
- response.authCodeResponse.access_token;
- switch (input.provider.id) {
- case 'google':
- const authHeader = `Bearer ${accessToken}`;
- const response = await axios.get(
- 'https://www.googleapis.com/oauth2/v1/userinfo',
- {
- params: {
- alt: 'json',
- },
- headers: {
- Authorization: authHeader,
- },
- },
- );
- const userInfo = response.data;
- const username = userInfo.name;
- const profilePhoto = userInfo.picture;
- (email === undefined || email === null) && { id };
- const isVerified = userInfo.verified_email;
- // await usersService.createUser({
- // pseudonym: accessToken.pseudonym,
- // username: accessToken.pseudonym,
- // profilePhoto: accessToken.profilePhoto,
- // emailpassword_users: {
- // create: undefined,
- // connectOrCreate: {
- // where: {
- // user_id: accessToken.id,
- // email: accessToken.email,
- // },
- // create: undefined,
- // },
- // connect: {
- // user_id: accessToken.id,
- // email: accessToken.email,
- // },
- // },
- // });
- break;
- default:
- return null;
- }
- }
- return await originalImplementation.thirdPartySignInUpPOST?.(
- input,
- );
- } catch (e: any) {
- if (
- e.message === 'Cannot sign up as email already exists'
- ) {
- return {
- status: 'GENERAL_ERROR',
- message:
- 'Seems like you already have an account with another method. Please use that instead.',
- };
- }
- throw e;
- }
- },
- //disable create session for sign up
- emailPasswordSignUpPOST: async function (input) {
- if (
- originalImplementation.emailPasswordSignUpPOST === undefined
- ) {
- throw new Error('Should never come here');
- }
- input.userContext.isSignUp = true;
- return originalImplementation.emailPasswordSignUpPOST(input);
- },
- };
- },
- getProfileInfo: async (accessTokenAPIResponse) => {
- const accessToken = accessTokenAPIResponse.access_token;
- const authHeader = `Bearer ${accessToken}`;
- const response = await axios.get(
- 'https://www.googleapis.com/oauth2/v1/userinfo',
- {
- params: {
- alt: 'json',
- },
- headers: {
- Authorization: authHeader,
- },
- },
- );
- const userInfo = response.data;
- const id = userInfo.id;
- const email = userInfo.email;
- (email === undefined || email === null) && { id };
- const isVerified = userInfo.verified_email;
- return {
- id,
- email: {
- id: email,
- isVerified,
- },
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment