Skip to content

Instantly share code, notes, and snippets.

@jscyo
Last active September 19, 2022 12:57
Show Gist options
  • Save jscyo/1e8784d77dc784a5c4d42c0e9c5f4fa1 to your computer and use it in GitHub Desktop.
Save jscyo/1e8784d77dc784a5c4d42c0e9c5f4fa1 to your computer and use it in GitHub Desktop.
ThirdPartyPasswordless.init({
contactMethod: "EMAIL_OR_PHONE",
flowType: "USER_INPUT_CODE_AND_MAGIC_LINK",
override: {
functions: (oI) => {
return{
...oI,
consumeCode: async (input) => {
// use listCodesByPreAuthSessionId to retrieve details about the current user
let codeResponse = await ThirdPartyPasswordless.listCodesByPreAuthSessionId({
preAuthSessionId: input.preAuthSessionId
})
// if both email and phoneNumber are undefined throw RESTART_FLOW_ERROR
if (codeResponse?.email === undefined && codeResponse?.phoneNumber === undefined) {
return {
status: "RESTART_FLOW_ERROR"
}
}
// get the associated customId for the user, this is a function defined by you.
let userInfo = await getUserInfo({
email: codeResponse?.email,
phoneNumber: codeResponse?.phoneNumber
})
// user with this email/phoneNumber exists in your db
if(userInfo !== null){
let response = await originalImplementation.consumeCode(input);
if (response.status != "OK") {
return response
}
if (response.createdNewUser) {
// create the mapping between the custom userId and the supertokens userId
await SuperTokens.createUserIdMapping({ superTokensUserId: response.user.id, externalUserId: userInfo.user_id})
// set that this is not a newly created user
response.createdNewUser = false
// set the customId in the response
response.user.id = userInfo.user_id
}
return response;
}
// user is not stored in your db call default implementation
return originalImplementation.consumeCode(input);
},
thirdPartySignInUp: async (input) => {
// this is a function defined by you
let userInfo = await getUserInfoForThirdParty(input.user.thirdParty.userId);
if (userInfo !== null){
// call the Supertokens signInUp implementation
let response = await originalImpl.thirdPartySignInUp(input)
if (response.status !== "OK") {
return response;
}
// check if a new SuperTokens user is created
if (response.createdNewUser) {
// create a mapping between the supertokens userId and the customId
await SuperTokens.createUserIdMapping({ superTokensUserId: response.user.id, externalUserId: userInfo.user_id});
// set that this is not a newly created user
response.createdNewUser = false
// update the mapping in the response
response.user.id = auth0UserInfo.user_id
}
return response;
}
// user does not exist in your db, call the default implementation
return await originalImpl.thirdPartySignInUp(input)
}
}
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment