|
|
@@ -178,6 +178,7 @@ module.exports = function(crowi, app) {
|
|
|
}
|
|
|
|
|
|
actions.externalAccounts.disassociate = function(req, res) {
|
|
|
+ const userData = req.user;
|
|
|
|
|
|
const redirectWithFlash = (type, msg) => {
|
|
|
req.flash(type, msg);
|
|
|
@@ -188,19 +189,48 @@ module.exports = function(crowi, app) {
|
|
|
redirectWithFlash('errorMessage', 'Invalid form.');
|
|
|
}
|
|
|
|
|
|
- const providerType = req.body.providerType;
|
|
|
- const accountId = req.body.accountId;
|
|
|
- const userData = req.user;
|
|
|
+ // make sure password set or this user has two or more ExternalAccounts
|
|
|
+ new Promise((resolve, reject) => {
|
|
|
+ if (userData.password != null) {
|
|
|
+ resolve(true);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ ExternalAccount.count({user: userData})
|
|
|
+ .then((count) => {
|
|
|
+ resolve(count > 1)
|
|
|
+ });
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .then((isDisassociatable) => {
|
|
|
+ if (!isDisassociatable) {
|
|
|
+ let e = new Error();
|
|
|
+ e.name = 'couldntDisassociateError';
|
|
|
+ throw e;
|
|
|
+ }
|
|
|
+
|
|
|
+ const providerType = req.body.providerType;
|
|
|
+ const accountId = req.body.accountId;
|
|
|
|
|
|
- ExternalAccount.findOneAndRemove({providerType, accountId, user: userData})
|
|
|
- .then((account) => {
|
|
|
- if (account == null) {
|
|
|
- return redirectWithFlash('errorMessage', 'ExternalAccount not found.');
|
|
|
+ return ExternalAccount.findOneAndRemove({providerType, accountId, user: userData});
|
|
|
+ })
|
|
|
+ .then((account) => {
|
|
|
+ if (account == null) {
|
|
|
+ return redirectWithFlash('errorMessage', 'ExternalAccount not found.');
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ return redirectWithFlash('successMessage', 'Successfully disassociated.');
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .catch((err) => {
|
|
|
+ if (err) {
|
|
|
+ if (err.name == 'couldntDisassociateError') {
|
|
|
+ return redirectWithFlash('couldntDisassociateError', true);
|
|
|
}
|
|
|
else {
|
|
|
- return redirectWithFlash('successMessage', 'Successfully disassociated.');
|
|
|
+ return redirectWithFlash('errorMessage', err.message);
|
|
|
}
|
|
|
- });
|
|
|
+ }
|
|
|
+ });
|
|
|
|
|
|
}
|
|
|
|