Created
November 14, 2021 18:57
-
-
Save rishabhpoddar/07de224632f32ea78b58b1203426dd57 to your computer and use it in GitHub Desktop.
customSignInUpPOST.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ThirdPartyEmailPassword.init({ | |
override: { | |
apis: (originalImplementation) => { | |
return { | |
...originalImplementation, | |
signInUpPOST: async (input) => { | |
if (input.type === "emailpassword") { | |
let { formFields, options } = input; | |
if (input.isSignIn) { | |
let email = formFields.filter((f) => f.id === "email")[0].value; | |
let password = formFields.filter((f) => f.id === "password")[0].value; | |
let response = await options.recipeImplementation.signIn({ email, password }); | |
if (response.status === "WRONG_CREDENTIALS_ERROR") { | |
return { | |
type: "emailpassword", | |
...response | |
}; | |
} | |
let user = response.user; | |
await Session.createNewSession(options.res, user.id, {}, {}); | |
return { | |
type: "emailpassword", | |
status: "OK", | |
createdNewUser: false, | |
user, | |
}; | |
} else { | |
let email = formFields.filter((f) => f.id === "email")[0].value; | |
let password = formFields.filter((f) => f.id === "password")[0].value; | |
let response = await options.recipeImplementation.signUp({ email, password }); | |
if (response.status === "EMAIL_ALREADY_EXISTS_ERROR") { | |
return { | |
type: "emailpassword", | |
...response | |
}; | |
} | |
let user = response.user; | |
await Session.createNewSession(options.res, user.id, {}, {}); | |
return { | |
type: "emailpassword", | |
status: "OK", | |
createdNewUser: true, | |
user, | |
}; | |
} | |
} else { | |
let { provider, redirectURI, code, options } = input | |
let userInfo; | |
let accessTokenAPIResponse: any; | |
let providerInfo = await provider.get(redirectURI, code); | |
accessTokenAPIResponse = await axios.default({ | |
method: "post", | |
url: providerInfo.accessTokenAPI.url, | |
data: qs.stringify(providerInfo.accessTokenAPI.params), | |
headers: { | |
"content-type": "application/x-www-form-urlencoded", | |
accept: "application/json", // few providers like github don't send back json response by default | |
}, | |
}); | |
userInfo = await providerInfo.getProfileInfo(accessTokenAPIResponse.data); | |
let emailInfo = userInfo.email; | |
if (emailInfo === undefined) { | |
return { | |
type: "thirdparty", | |
status: "NO_EMAIL_GIVEN_BY_PROVIDER", | |
}; | |
} | |
let response = await options.recipeImplementation.signInUp({ | |
thirdPartyId: provider.id, | |
thirdPartyUserId: userInfo.id, | |
email: emailInfo, | |
}); | |
if (response.status === "FIELD_ERROR") { | |
return { type: "thirdparty", ...response }; | |
} | |
// we set the email as verified if already verified by the OAuth provider. | |
// This block was added because of https://github.com/supertokens/supertokens-core/issues/295 | |
if (emailInfo.isVerified) { | |
const tokenResponse = await options.emailVerificationRecipeImplementation.createEmailVerificationToken({ | |
userId: response.user.id, | |
email: response.user.email, | |
}); | |
if (tokenResponse.status === "OK") { | |
await options.emailVerificationRecipeImplementation.verifyEmailUsingToken({ | |
token: tokenResponse.token, | |
}); | |
} | |
} | |
await Session.createNewSession(options.res, response.user.id, {}, {}); | |
return { | |
type: "thirdparty", | |
status: "OK", | |
createdNewUser: response.createdNewUser, | |
user: response.user, | |
authCodeResponse: accessTokenAPIResponse.data, | |
}; | |
} | |
} | |
} | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment