Skip to main content

What if my user wants to authenticate by SMS ?

If your user would like to authenticate by SMS, the authentication process (Registration / Login) is extremely similar to the user authentication by email.

Register a user by SMS​

When you want to start user registration, make sure to call the method withAuthProcessBySmsId() to provide the authenticationProcessBySmsId. For detailed information about those authentication process ids, head to the User Authentication - Init AnonymousMedTechApi how to.

const msgGtwUrl = process.env.ICURE_MSG_GTW_URL
const specId = process.env.SPEC_ID
const authProcessByEmailId = process.env.AUTH_BY_EMAIL_HCP_PROCESS_ID
const authProcessBySmsId = process.env.AUTH_BY_SMS_HCP_PROCESS_ID
const recaptcha = process.env.RECAPTCHA

const anonymousApi = await new AnonymousMedTechApi.Builder()
.withICureBaseUrl(iCureUrl)
.withCrypto(webcrypto as any)
.withMsgGwUrl(msgGtwUrl)
.withMsgGwSpecId(specId)
.withAuthProcessBySmsId(authProcessBySmsId)
.withCryptoStrategies(new SimpleMedTechCryptoStrategies([]))
.build()

Once your AnonymousMedTechApi is initialised, you can start the authentication of your user by calling the authenticationApi.startAuthentication service.

Make sure to provide user's phone number instead of their email.

const authProcess = await anonymousApi.authenticationApi.startAuthentication(
recaptcha,
undefined,
userPhoneNumber, // Phone number of the user who wants to register
'Ned',
'Stark',
masterHcpId,
)
authProcess
{
"requestId": "aea7f4a7-5887-450e-9061-7014bd5e01c2",
"login": "+24511456546",
"bypassTokenCheck": false
}

Your user is now able to create data on their own.

info

If you choose to provide user email AND phone number, they will by default, receives their validation code by email.

Login by SMS​

Use the authenticationApi.startAuthentication service again, by providing the user's phone number. The login process stays very similar to Login By Email.

const anonymousApiForLogin = await new AnonymousMedTechApi.Builder()
.withICureBaseUrl(iCureUrl)
.withCrypto(webcrypto as any)
.withMsgGwUrl(msgGtwUrl)
.withMsgGwSpecId(specId)
.withAuthProcessByEmailId(authProcessByEmailId)
.withAuthProcessBySmsId(authProcessBySmsId)
.withCryptoStrategies(new SimpleMedTechCryptoStrategies([]))
.build()

const authProcessLogin = await anonymousApiForLogin.authenticationApi.startAuthentication(
recaptcha,
undefined,
userPhoneNumber, // The phone number used for user registration
)
const loginResult = await anonymousApiForLogin.authenticationApi.completeAuthentication(
authProcessLogin,
validationCodeForLogin,
)

const loggedUserApi = loginResult.medTechApi

const foundPatientAfterLogin = await loggedUserApi.patientApi.getPatient(createdPatient.id)
foundPatientAfterLogin
{
"id": "1128fc00-21a6-4159-94cf-8018e533a378",
"rev": "1-140f5a2409e6672e16c0502e9ec9e070",
"created": 1700058643665,
"modified": 1700058643665,
"author": "bdb9c89b-ecd0-4a7f-9bd9-e8f1b1333c36",
"responsible": "f075dd66-2ca2-420b-bf71-182cd725ac87",
"firstName": "Robb",
"lastName": "Stark",
"languages": [],
"active": true,
"note": "You must keep one's head",
"parameters": {},
"identifiers": [],
"labels": {},
"codes": {},
"notes": [],
"names": [
{
"lastName": "Stark",
"firstNames": [
"Robb"
],
"prefix": [],
"suffix": [],
"text": "Stark Robb",
"use": "official"
}
],
"addresses": [],
"gender": "male",
"birthSex": "unknown",
"mergedIds": {},
"deactivationReason": "none",
"personalStatus": "unknown",
"partnerships": [],
"patientHealthCareParties": [],
"patientProfessions": [],
"properties": {},
"systemMetaData": {
"hcPartyKeys": {},
"privateKeyShamirPartitions": {},
"secretForeignKeys": [],
"cryptedForeignKeys": {},
"delegations": {},
"encryptionKeys": {},
"aesExchangeKeys": {},
"transferKeys": {},
"securityMetadata": {
"secureDelegations": {},
"keysEquivalences": {}
},
"encryptedSelf": "aTil7RuAx5JSf6ip4bCgmQW0iZVT0v94Ce+gcInKbfHYcfaLiBSKZJ8eu5Yt3V30kvDXRvTorchdUQKJgzl5xQ==",
"publicKeysForOaepWithSha256": [],
"tags": {}
}
}