Created
February 1, 2022 15:57
-
-
Save rishabhpoddar/878ada3e91b039a798e88aa311d9c846 to your computer and use it in GitHub Desktop.
Passwordless add phone number to access token
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
Passwordless.init({ | |
..., | |
override: { | |
apis: (oI) => { | |
return { | |
...oI, | |
consumeCodePOST: async function (input) { | |
// first we get the phone number from the preAuthSessionId. | |
let deviceInfo = await Passwordless.listCodesByPreAuthSessionId({ | |
preAuthSessionId: input.preAuthSessionId | |
}); | |
if (deviceInfo === undefined) { | |
return { | |
status: "RESTART_FLOW_ERROR" | |
}; | |
} | |
let phoneNumber = deviceInfo.phoneNumber!; | |
// the code below is copied from https://github.com/supertokens/supertokens-node/blob/master/lib/ts/recipe/passwordless/api/implementation.ts#L6 | |
let response = await input.options.recipeImplementation.consumeCode( | |
"deviceId" in input | |
? { | |
preAuthSessionId: input.preAuthSessionId, | |
deviceId: input.deviceId, | |
userInputCode: input.userInputCode, | |
userContext: input.userContext, | |
} | |
: { | |
preAuthSessionId: input.preAuthSessionId, | |
linkCode: input.linkCode, | |
userContext: input.userContext, | |
} | |
); | |
if (response.status !== "OK") { | |
return response; | |
} | |
let user = response.user; | |
const session = await Session.createNewSession(input.options.res, user.id, { | |
// we are adding the phoneNumber to the access token payload. | |
// This will also be accessible in all API calls + on the frontend. | |
phoneNumber | |
}, {}); | |
return { | |
status: "OK", | |
createdNewUser: response.createdNewUser, | |
user: response.user, | |
session, | |
}; | |
} | |
}; | |
}, | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment