-
-
Notifications
You must be signed in to change notification settings - Fork 546
feat(core): enhanced user lookup by phone number #7382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bcf3bcf
feat(core): enhenced user lookup by phone number
simeng-li 34a4862
chore: update content
simeng-li 12008ba
fix: clean up typos
simeng-li 9fd3f60
fix(test): remove test case skip statement
simeng-li 03d4b47
chore: fix typo
simeng-li File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
"@logto/core": minor | ||
--- | ||
|
||
enhenced user lookup by phone with phone number normalization. | ||
|
||
In some countries, local phone numbers are often entered with a leading '0'. However, in the context of international format this leading '0' should be stripped. E.g. +61 (0)2 1234 5678 should be normalized to +61 2 1234 5678. | ||
|
||
In the previous implementation, Logto did not normalize the user's phone number during the user sign-up process. Both 61021345678 and 61212345678 were considered as valid phone numbers, and we do not normalize them before storing them in the database. This could lead to confusion when users try to sign-in with their phone numbers, as they may not remember the exact format they used during sign-up. Users may also end up with different accounts for the same phone number, depending on how they entered it during sign-up. | ||
|
||
To address this issue, especially for legacy users, we have added a new enhenced user lookup by phone with either format (with or without leading '0') to the user sign-in process. This means that users can now sign-in with either format of their phone number, and Logto will try to match it with the one stored in the database, even if they might have different formats. This will help to reduce confusion and improve the user experience when logging in with phone numbers. | ||
|
||
For example: | ||
|
||
- If a user signs up with the phone number +61 2 1234 5678, they can now sign-in with either +61 2 1234 5678 or +61 02 1234 5678. | ||
- The same applies to the phone number +61 02 1234 5678, which can be used to sign-in with either +61 2 1234 5678 or +61 02 1234 5678. | ||
|
||
For users who might have created two different counts with the same phone number but different formats. The look up process will alway return the one with exact match. This means that if a user has two accounts with the same phone number but different formats, they will still be able to sign-in with either format, but they will only be able to access the account that matches the format they used during sign-up. | ||
|
||
For example: | ||
|
||
- If a user has two accounts with the phone numbers +61 2 1234 5678 and +61 02 1234 5678. They will need to sign-in each account using the exact format they used during sign-up. | ||
|
||
related github issue [#7371](https://github.com/logto-io/logto/issues/7371). |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
|
||
import { parsePhoneNumber, PhoneNumberParser } from './phone.js'; | ||
|
||
describe('parsePhoneNumber', () => { | ||
it('parsePhoneNumber should return if the phone number is valid', () => { | ||
const phoneNumber = '12025550123'; | ||
expect(parsePhoneNumber(phoneNumber, true)).toEqual('12025550123'); | ||
}); | ||
|
||
it('parsePhoneNumber should strip the leading +', () => { | ||
const phoneNumber = '+12025550123'; | ||
expect(parsePhoneNumber(phoneNumber)).toEqual('12025550123'); | ||
}); | ||
|
||
it('parsePhoneNumber should srtip the leading 0', () => { | ||
simeng-li marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const phoneNumber = '610412345678'; | ||
expect(parsePhoneNumber(phoneNumber)).toEqual('61412345678'); | ||
}); | ||
|
||
it('parsePhoneNumber should strip non-digit characters', () => { | ||
const phoneNumber = '+61 (0) 412 345 678'; | ||
expect(parsePhoneNumber(phoneNumber)).toEqual('61412345678'); | ||
}); | ||
|
||
it('should return the original phone number if it is invalid', () => { | ||
const phoneNumber = '0123'; | ||
expect(parsePhoneNumber(phoneNumber)).toEqual(phoneNumber); | ||
}); | ||
|
||
it('should throw an error if the phone number is invalid and throwError is true', () => { | ||
const phoneNumber = '0123'; | ||
expect(() => parsePhoneNumber(phoneNumber, true)).toThrowError(); | ||
}); | ||
}); | ||
|
||
describe('PhoneNumberParser', () => { | ||
type TestCase = { | ||
phone: string; | ||
isValid: boolean; | ||
countryCode?: string; | ||
nationalNumber?: string; | ||
internationalNumber?: string; | ||
internationalNumberWithLeadingZero?: string; | ||
}; | ||
const testCases: TestCase[] = [ | ||
{ | ||
phone: '12025550123', | ||
isValid: true, | ||
countryCode: '1', | ||
nationalNumber: '2025550123', | ||
internationalNumber: '12025550123', | ||
internationalNumberWithLeadingZero: '102025550123', | ||
}, | ||
{ | ||
phone: '+61 (0) 412 345 678', | ||
isValid: true, | ||
countryCode: '61', | ||
nationalNumber: '412345678', | ||
internationalNumber: '61412345678', | ||
internationalNumberWithLeadingZero: '610412345678', | ||
}, | ||
{ | ||
phone: '61 412 345 678', | ||
isValid: true, | ||
countryCode: '61', | ||
nationalNumber: '412345678', | ||
internationalNumber: '61412345678', | ||
internationalNumberWithLeadingZero: '610412345678', | ||
}, | ||
{ | ||
phone: '456', | ||
isValid: false, | ||
countryCode: undefined, | ||
nationalNumber: undefined, | ||
internationalNumber: undefined, | ||
internationalNumberWithLeadingZero: undefined, | ||
}, | ||
]; | ||
|
||
it.each(testCases)( | ||
'parsePhoneNumber should return $phone if the phone number is valid', | ||
({ | ||
phone, | ||
isValid, | ||
countryCode, | ||
nationalNumber, | ||
internationalNumber, | ||
internationalNumberWithLeadingZero, | ||
}) => { | ||
const parser = new PhoneNumberParser(phone); | ||
expect(parser.raw).toEqual(phone); | ||
expect(parser.isValid).toEqual(isValid); | ||
expect(parser.countryCode).toEqual(countryCode); | ||
expect(parser.nationalNumber).toEqual(nationalNumber); | ||
expect(parser.internationalNumber).toEqual(internationalNumber); | ||
expect(parser.internationalNumberWithLeadingZero).toEqual(internationalNumberWithLeadingZero); | ||
} | ||
); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.