Last active
November 18, 2021 10:50
-
-
Save jscyo/4fd0e6024db1fab9472ab4aaa31f7134 to your computer and use it in GitHub Desktop.
Init code
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
supertokens.init({ | |
supertokens: { | |
connectionURI: stConnectionURI, | |
}, | |
appInfo: { | |
appName: "SuperTokens Demo App", | |
apiDomain, | |
websiteDomain | |
}, | |
recipeList: [ | |
ThirdPartyEmailPassword.init({ | |
override: { | |
functions: (orignalImpl) => { | |
return { | |
...orignalImpl, | |
getUsersByEmail: async function (input) { | |
let users = await orignalImpl.getUsersByEmail(input) | |
// if the ST userId in reponse is mapped to a Auth0 userId return that | |
for (let i = 0; i < users.length; i++) { | |
users[i].id = getAuth0UserIdFromSTUserId(users[i].id) | |
} | |
return users; | |
}, | |
getUserByThirdPartyInfo: async function (input) { | |
let response = await orignalImpl.getUserByThirdPartyInfo(input) | |
// if the ST userId in reponse is mapped to a Auth0 userId return that | |
response.id = getAuth0UserIdFromSTUserId(response.id) | |
return response; | |
}, | |
getUserById: async function (input) { | |
// check if input userid is an auth0 userid, if so get mapped ST userId otherwise return the input userId | |
let userId = getSTUserIdFromAuth0UserId(input.userId) | |
// call orignal implementation | |
let response = await orignalImpl.getUserById({ userId }) | |
// if the ST userId in reponse is mapped to a Auth0 userId return that | |
response.id = getAuth0UserIdFromSTUserId(response.id) | |
return response; | |
}, | |
createResetPasswordToken: async function (input) { | |
// check if input userid is an auth0 userid, if so get mapped ST userId otherwise return the input userId | |
let userId = getSTUserIdFromAuth0UserId(input.userId) | |
return orignalImpl.createResetPasswordToken({ userId }) | |
}, | |
updateEmailOrPassword: async function (input) { | |
input.userId = getSTUserIdFromAuth0UserId(input.userId) | |
return orignalImpl.updateEmailOrPassword(input) | |
}, | |
signInUp: async function (input) { | |
let response = await orignalImpl.signInUp(input) | |
if (response.createdNewUser) { | |
let auth0SocialUserId = await doesThirdPartyUserIdExistInAuth0Database(response.user.thirdParty.userId) | |
if (auth0SocialUserId) { | |
mapUserData(auth0SocialUserId, response.user.id) | |
response.user.id = auth0SocialUserId | |
} | |
} | |
return response | |
}, | |
signIn: async function (input) { | |
// check if user exists in supertokens | |
let users = await this.getUsersByEmail({ email: input.email }); | |
let emailPasswordUserExists = false; | |
for (let i = 0; i < users.length; i++) { | |
if (users[i].thirdParty === undefined) { | |
emailPasswordUserExists = true | |
} | |
} | |
if (((await this.getUsersByEmail({ email: input.email })).length === 0) || !emailPasswordUserExists) { | |
// authenticate user with the auth0 authorization endpoint | |
let auth0UserData = await doesUserExistInAuth0DataBase(input.email, input.password) | |
// user does not exist | |
if (auth0UserData === undefined) { | |
return { | |
status: 'WRONG_CREDENTIALS_ERROR' | |
} | |
} | |
// add the users info to the supertokens db. | |
let response = await this.signUp(input) | |
// map supertokens id to Auth0 id | |
mapUserData(auth0UserData.sub, response.user.id) | |
return response | |
} else { | |
return orignalImpl.signIn(input) | |
} | |
} | |
} | |
} | |
}, | |
providers: [ | |
Google({ | |
clientId: "client_id", | |
clientSecret: "client_secret", | |
}) | |
] | |
}), | |
Session.init() | |
] | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment