mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-19 01:40:15 +01:00
* implemented Apple Auth login. Closes: #3438 TODO: - write config Doc * removed some comments * removed comment * Add unit tests for Apple login strategy Introduce comprehensive tests for the Apple login strategy, covering new user creation, existing user updates, and error handling scenarios during the authentication flow. Mocks implemented for external dependencies to ensure isolated testing. * Remove unnecessary blank line in socialLogins.js
40 lines
1.2 KiB
JavaScript
40 lines
1.2 KiB
JavaScript
const { createSocialUser, handleExistingUser } = require('./process');
|
|
const { isEnabled } = require('~/server/utils');
|
|
const { findUser } = require('~/models');
|
|
const { logger } = require('~/config');
|
|
|
|
const socialLogin =
|
|
(provider, getProfileDetails) => async (accessToken, refreshToken, idToken, profile, cb) => {
|
|
try {
|
|
const { email, id, avatarUrl, username, name, emailVerified } = getProfileDetails({
|
|
idToken, profile,
|
|
});
|
|
|
|
const oldUser = await findUser({ email: email.trim() });
|
|
const ALLOW_SOCIAL_REGISTRATION = isEnabled(process.env.ALLOW_SOCIAL_REGISTRATION);
|
|
|
|
if (oldUser) {
|
|
await handleExistingUser(oldUser, avatarUrl);
|
|
return cb(null, oldUser);
|
|
}
|
|
|
|
if (ALLOW_SOCIAL_REGISTRATION) {
|
|
const newUser = await createSocialUser({
|
|
email,
|
|
avatarUrl,
|
|
provider,
|
|
providerKey: `${provider}Id`,
|
|
providerId: id,
|
|
username,
|
|
name,
|
|
emailVerified,
|
|
});
|
|
return cb(null, newUser);
|
|
}
|
|
} catch (err) {
|
|
logger.error(`[${provider}Login]`, err);
|
|
return cb(err);
|
|
}
|
|
};
|
|
|
|
module.exports = socialLogin;
|