fix(api): add toLowerCase method to email (#38586)
This commit is contained in:
@ -8,6 +8,12 @@ import { wrapHandledError } from '../../server/utils/create-handled-error.js';
|
||||
|
||||
// const log = debug('fcc:models:userIdent');
|
||||
|
||||
export function ensureLowerCaseEmail(profile) {
|
||||
return typeof profile?.emails?.[0]?.value === 'string'
|
||||
? profile.emails[0].value.toLowerCase()
|
||||
: '';
|
||||
}
|
||||
|
||||
export default function(UserIdent) {
|
||||
UserIdent.on('dataSourceAttached', () => {
|
||||
UserIdent.findOne$ = observeMethod(UserIdent, 'findOne');
|
||||
@ -41,10 +47,8 @@ export default function(UserIdent) {
|
||||
include: 'user'
|
||||
};
|
||||
// get the email from the auth0 (its expected from social providers)
|
||||
const email =
|
||||
profile && profile.emails && profile.emails[0]
|
||||
? profile.emails[0].value
|
||||
: '';
|
||||
const email = ensureLowerCaseEmail(profile);
|
||||
|
||||
if (!isEmail('' + email)) {
|
||||
throw wrapHandledError(
|
||||
new Error('invalid or empty email received from auth0'),
|
||||
|
30
api-server/common/models/User-Identity.test.js
Normal file
30
api-server/common/models/User-Identity.test.js
Normal file
@ -0,0 +1,30 @@
|
||||
/* global expect */
|
||||
import { ensureLowerCaseEmail } from './User-Identity';
|
||||
|
||||
test('returns lowercase email when one exists', () => {
|
||||
const profile = {
|
||||
id: 2,
|
||||
emails: [{ value: 'Example@Mail.com', name: 'John Doe' }]
|
||||
};
|
||||
expect(ensureLowerCaseEmail(profile)).toBe('example@mail.com');
|
||||
});
|
||||
|
||||
test('returns empty string when value is undefined', () => {
|
||||
const profile = {
|
||||
id: 4,
|
||||
emails: []
|
||||
};
|
||||
expect(ensureLowerCaseEmail(profile)).toBe('');
|
||||
});
|
||||
|
||||
test('returns empty string when emails is undefined', () => {
|
||||
const profile = {
|
||||
id: 5
|
||||
};
|
||||
expect(ensureLowerCaseEmail(profile)).toBe('');
|
||||
});
|
||||
|
||||
test('returns empty string when profile is undefined', () => {
|
||||
let profile;
|
||||
expect(ensureLowerCaseEmail(profile)).toBe('');
|
||||
});
|
Reference in New Issue
Block a user