mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-01 01:00:36 -04:00
* fix: remove circular dependency redux depended on templates/Challenges/redux and vice versa. This meant that import order mattered and confusing bugs could arise. (cherry picked from commit 7d67a4e70922bbb3051f2f9982dcc69e240d43dc) * feat: require imports to be in alphabetical order Import order generally does not matter, but there are edge cases (circular imports and css imports, for example) where changing order changes behaviour (cherry picked from commit b8d1393a91ec6e068caf8e8498a5c95df68c2b2c) * chore: order imports * fix: lift up challenge description + title comps This brings the classic Show closer to the others as they now all create the description and title components * fix: remove donation-saga/index circular import (cherry picked from commit 51a44ca668a700786d2744feffeae4fdba5fd207) * refactor: extract action-types from settings (cherry picked from commit 25e26124d691c84a0d0827d41dafb761c686fadd) * fix: lint errors * feat: prevent useless renames
159 lines
4.7 KiB
JavaScript
159 lines
4.7 KiB
JavaScript
import dedent from 'dedent';
|
|
import { Observable } from 'rx';
|
|
// import debug from 'debug';
|
|
import { isEmail } from 'validator';
|
|
|
|
import { wrapHandledError } from '../../server/utils/create-handled-error.js';
|
|
import { observeMethod, observeQuery } from '../../server/utils/rx';
|
|
|
|
// 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 initializeUserIdent(UserIdent) {
|
|
UserIdent.on('dataSourceAttached', () => {
|
|
UserIdent.findOne$ = observeMethod(UserIdent, 'findOne');
|
|
});
|
|
|
|
UserIdent.login = function (
|
|
_provider,
|
|
authScheme,
|
|
profile,
|
|
credentials,
|
|
options,
|
|
cb
|
|
) {
|
|
const User = UserIdent.app.models.User;
|
|
const AccessToken = UserIdent.app.models.AccessToken;
|
|
options = options || {};
|
|
if (typeof options === 'function' && !cb) {
|
|
cb = options;
|
|
options = {};
|
|
}
|
|
|
|
// get the social provider data and the external id from auth0
|
|
profile.id = profile.id || profile.openid;
|
|
const auth0IdString = '' + profile.id;
|
|
const [provider, socialExtId] = auth0IdString.split('|');
|
|
const query = {
|
|
where: {
|
|
provider: provider,
|
|
externalId: socialExtId
|
|
},
|
|
include: 'user'
|
|
};
|
|
// get the email from the auth0 (its expected from social providers)
|
|
const email = ensureLowerCaseEmail(profile);
|
|
|
|
if (!isEmail('' + email)) {
|
|
throw wrapHandledError(
|
|
new Error('invalid or empty email received from auth0'),
|
|
{
|
|
message: dedent`
|
|
${provider} did not return a valid email address.
|
|
Please try again with a different account that has an
|
|
email associated with it your update your settings on ${provider}, for us to be able to retrieve your email.
|
|
`,
|
|
type: 'info',
|
|
redirectTo: '/'
|
|
}
|
|
);
|
|
}
|
|
|
|
if (provider === 'email') {
|
|
return User.findOne$({ where: { email } })
|
|
.flatMap(user => {
|
|
return user
|
|
? Observable.of(user)
|
|
: User.create$({ email }).toPromise();
|
|
})
|
|
.flatMap(user => {
|
|
if (!user) {
|
|
throw wrapHandledError(
|
|
new Error('could not find or create a user'),
|
|
{
|
|
message: dedent`
|
|
We could not find or create a user with that email address.
|
|
`,
|
|
type: 'info',
|
|
redirectTo: '/'
|
|
}
|
|
);
|
|
}
|
|
const createToken = observeQuery(AccessToken, 'create', {
|
|
userId: user.id,
|
|
created: new Date(),
|
|
ttl: user.constructor.settings.ttl
|
|
});
|
|
const updateUserPromise = new Promise((resolve, reject) =>
|
|
user.updateAttributes(
|
|
{
|
|
emailVerified: true,
|
|
emailAuthLinkTTL: null,
|
|
emailVerifyTTL: null
|
|
},
|
|
err => {
|
|
if (err) {
|
|
return reject(err);
|
|
}
|
|
return resolve();
|
|
}
|
|
)
|
|
);
|
|
return Observable.combineLatest(
|
|
Observable.of(user),
|
|
createToken,
|
|
Observable.fromPromise(updateUserPromise),
|
|
(user, token) => ({ user, token })
|
|
);
|
|
})
|
|
.subscribe(({ user, token }) => cb(null, user, null, token), cb);
|
|
} else {
|
|
return UserIdent.findOne$(query)
|
|
.flatMap(identity => {
|
|
return identity
|
|
? Observable.of(identity.user())
|
|
: User.findOne$({ where: { email } }).flatMap(user => {
|
|
return user
|
|
? Observable.of(user)
|
|
: User.create$({ email }).toPromise();
|
|
});
|
|
})
|
|
.flatMap(user => {
|
|
const createToken = observeQuery(AccessToken, 'create', {
|
|
userId: user.id,
|
|
created: new Date(),
|
|
ttl: user.constructor.settings.ttl
|
|
});
|
|
const updateUser = new Promise((resolve, reject) =>
|
|
user.updateAttributes(
|
|
{
|
|
email: email,
|
|
emailVerified: true,
|
|
emailAuthLinkTTL: null,
|
|
emailVerifyTTL: null
|
|
},
|
|
err => {
|
|
if (err) {
|
|
return reject(err);
|
|
}
|
|
return resolve();
|
|
}
|
|
)
|
|
);
|
|
return Observable.combineLatest(
|
|
Observable.of(user),
|
|
createToken,
|
|
Observable.fromPromise(updateUser),
|
|
(user, token) => ({ user, token })
|
|
);
|
|
})
|
|
.subscribe(({ user, token }) => cb(null, user, null, token), cb);
|
|
}
|
|
};
|
|
}
|