|
@@ -250,50 +250,51 @@ module.exports = function(crowi, app) {
|
|
|
})(req, res);
|
|
})(req, res);
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- const loginPassportGoogleCallback = function(req, res, next) {
|
|
|
|
|
- passport.authenticate('google', {failureRedirect: '/login'}, (request, response) => {
|
|
|
|
|
- ExternalAccount.findOrRegister(
|
|
|
|
|
- 'google',
|
|
|
|
|
|
|
+ const loginPassportGoogleCallback = async function(req, res, next) {
|
|
|
|
|
+ const provider = 'google';
|
|
|
|
|
+ const response = await __promisifiedPassportAuthentication(req, res, provider);
|
|
|
|
|
+ let externalAccount;
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ externalAccount = await ExternalAccount.findOrRegister(
|
|
|
|
|
+ provider,
|
|
|
response.id,
|
|
response.id,
|
|
|
response.displayName,
|
|
response.displayName,
|
|
|
`${response.name.givenName} ${response.name.familyName}`
|
|
`${response.name.givenName} ${response.name.familyName}`
|
|
|
- )
|
|
|
|
|
- .catch((err) => {
|
|
|
|
|
- if (err.name === 'DuplicatedUsernameException') {
|
|
|
|
|
- // get option
|
|
|
|
|
- const isSameUsernameTreatedAsIdenticalUser = Config.isSameUsernameTreatedAsIdenticalUser(config, 'google');
|
|
|
|
|
- if (isSameUsernameTreatedAsIdenticalUser) {
|
|
|
|
|
- // associate to existing user
|
|
|
|
|
- debug(`ExternalAccount '${response.displayName}' will be created and bound to the exisiting User account`);
|
|
|
|
|
- return ExternalAccount.associate('google', response.id, err.user);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- throw err;
|
|
|
|
|
- })
|
|
|
|
|
- .then((externalAccount) => {
|
|
|
|
|
- return externalAccount.getPopulatedUser();
|
|
|
|
|
- })
|
|
|
|
|
- .then((user) => {
|
|
|
|
|
- // login
|
|
|
|
|
- req.logIn(user, err => {
|
|
|
|
|
- if (err) {
|
|
|
|
|
- return next();
|
|
|
|
|
- }
|
|
|
|
|
- else {
|
|
|
|
|
- return loginSuccess(req, res, user);
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
- })
|
|
|
|
|
- .catch((err) => {
|
|
|
|
|
- if (err.name === 'DuplicatedUsernameException') {
|
|
|
|
|
- req.flash('provider-DuplicatedUsernameException', 'Google');
|
|
|
|
|
- return next();
|
|
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (err) {
|
|
|
|
|
+ if (err.name === 'DuplicatedUsernameException') {
|
|
|
|
|
+ // get option
|
|
|
|
|
+ const isSameUsernameTreatedAsIdenticalUser = Config.isSameUsernameTreatedAsIdenticalUser(config, provider);
|
|
|
|
|
+ if (isSameUsernameTreatedAsIdenticalUser) {
|
|
|
|
|
+ // associate to existing user
|
|
|
|
|
+ debug(`ExternalAccount '${response.displayName}' will be created and bound to the exisiting User account`);
|
|
|
|
|
+ return ExternalAccount.associate(provider, response.id, err.user);
|
|
|
}
|
|
}
|
|
|
else {
|
|
else {
|
|
|
- return next(err);
|
|
|
|
|
|
|
+ req.flash('provider-DuplicatedUsernameException', provider);
|
|
|
|
|
+ return next();
|
|
|
}
|
|
}
|
|
|
- });
|
|
|
|
|
- })(req, res, next);
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const user = await externalAccount.getPopulatedUser();
|
|
|
|
|
+
|
|
|
|
|
+ // login
|
|
|
|
|
+ await req.logIn(user, err => {
|
|
|
|
|
+ if (err) { return next(err) };
|
|
|
|
|
+ return loginSuccess(req, res, user);
|
|
|
|
|
+ });
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ const __promisifiedPassportAuthentication = (req, res, provider) => {
|
|
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
|
|
+ passport.authenticate(provider, (err, user, info) => {
|
|
|
|
|
+ if (err) reject(err);
|
|
|
|
|
+ if (user) resolve(user);
|
|
|
|
|
+ })(req, res);
|
|
|
|
|
+ });
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
return {
|