Skip to content

Commit affaf55

Browse files
authored
Merge pull request #3093 from processing/fix/editor-crash
Addresses various issues for current editor crash
2 parents adf9056 + 8022ee1 commit affaf55

File tree

4 files changed

+77
-70
lines changed

4 files changed

+77
-70
lines changed

client/modules/User/actions.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -91,24 +91,23 @@ export function validateAndSignUpUser(formValues) {
9191
}
9292

9393
export function getUser() {
94-
return (dispatch) => {
95-
apiClient
96-
.get('/session')
97-
.then((response) => {
98-
dispatch(authenticateUser(response.data));
99-
dispatch({
100-
type: ActionTypes.SET_PREFERENCES,
101-
preferences: response.data.preferences
102-
});
103-
setLanguage(response.data.preferences.language, {
104-
persistPreference: false
105-
});
106-
})
107-
.catch((error) => {
108-
const { response } = error;
109-
const message = response.message || response.data.error;
110-
dispatch(authError(message));
94+
return async (dispatch) => {
95+
try {
96+
const response = await apiClient.get('/session');
97+
const { data } = response;
98+
99+
dispatch(authenticateUser(data));
100+
dispatch({
101+
type: ActionTypes.SET_PREFERENCES,
102+
preferences: data.preferences
111103
});
104+
setLanguage(data.preferences.language, { persistPreference: false });
105+
} catch (error) {
106+
const message = error.response
107+
? error.response.data.error || error.response.message
108+
: 'Unknown error.';
109+
dispatch(authError(message));
110+
}
112111
};
113112
}
114113

server/config/passport.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,31 @@ passport.deserializeUser((id, done) => {
3535
* Sign in using Email/Username and Password.
3636
*/
3737
passport.use(
38-
new LocalStrategy({ usernameField: 'email' }, (email, password, done) => {
39-
User.findByEmailOrUsername(email)
40-
.then((user) => {
38+
new LocalStrategy(
39+
{ usernameField: 'email' },
40+
async (email, password, done) => {
41+
try {
42+
const user = await User.findByEmailOrUsername(email);
43+
4144
if (!user) {
42-
done(null, false, { msg: `Email ${email} not found.` });
43-
return;
45+
return done(null, false, { msg: `Email ${email} not found.` });
4446
} else if (user.banned) {
45-
done(null, false, { msg: accountSuspensionMessage });
46-
return;
47+
return done(null, false, { msg: 'Your account has been suspended.' });
4748
}
48-
user.comparePassword(password).then((isMatch) => {
49-
if (isMatch) {
50-
done(null, user);
51-
} else {
52-
done(null, false, { msg: 'Invalid email or password.' });
53-
}
54-
});
55-
})
56-
.catch((err) => done(null, false, { msg: err }));
57-
})
49+
50+
const isMatch = await user.comparePassword(password);
51+
52+
if (isMatch) {
53+
return done(null, user);
54+
} else { // eslint-disable-line
55+
return done(null, false, { msg: 'Invalid email or password' });
56+
}
57+
} catch (err) {
58+
console.error(err);
59+
return done(null, false, { msg: err });
60+
}
61+
}
62+
)
5863
);
5964

6065
/**

server/controllers/user.controller.js

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ export async function createUser(req, res) {
4343
try {
4444
const { username, email, password } = req.body;
4545
const emailLowerCase = email.toLowerCase();
46+
const existingUser = await User.findByEmailAndUsername(email, username);
47+
if (existingUser) {
48+
const fieldInUse =
49+
existingUser.email.toLowerCase() === emailLowerCase
50+
? 'Email'
51+
: 'Username';
52+
res.status(422).send({ error: `${fieldInUse} is in use` });
53+
return;
54+
}
55+
4656
const EMAIL_VERIFY_TOKEN_EXPIRY_TIME = Date.now() + 3600000 * 24; // 24 hours
4757
const token = await generateToken();
4858
const user = new User({
@@ -53,34 +63,35 @@ export async function createUser(req, res) {
5363
verifiedToken: token,
5464
verifiedTokenExpires: EMAIL_VERIFY_TOKEN_EXPIRY_TIME
5565
});
56-
const existingUser = await User.findByEmailAndUsername(email, username);
57-
if (existingUser) {
58-
const fieldInUse =
59-
existingUser.email.toLowerCase() === emailLowerCase
60-
? 'Email'
61-
: 'Username';
62-
res.status(422).send({ error: `${fieldInUse} is in use` });
63-
return;
64-
}
66+
6567
await user.save();
66-
req.logIn(user, (loginErr) => {
68+
69+
req.logIn(user, async (loginErr) => {
6770
if (loginErr) {
68-
throw loginErr;
71+
console.error(loginErr);
72+
res.status(500).json({ error: 'Failed to log in user.' });
73+
return;
6974
}
70-
});
7175

72-
const protocol = process.env.NODE_ENV === 'production' ? 'https' : 'http';
73-
const mailOptions = renderEmailConfirmation({
74-
body: {
75-
domain: `${protocol}://${req.headers.host}`,
76-
link: `${protocol}://${req.headers.host}/verify?t=${token}`
77-
},
78-
to: req.user.email
79-
});
76+
const protocol = process.env.NODE_ENV === 'production' ? 'https' : 'http';
77+
const mailOptions = renderEmailConfirmation({
78+
body: {
79+
domain: `${protocol}://${req.headers.host}`,
80+
link: `${protocol}://${req.headers.host}/verify?t=${token}`
81+
},
82+
to: req.user.email
83+
});
8084

81-
await mail.send(mailOptions);
82-
res.json(userResponse(req.user));
85+
try {
86+
await mail.send(mailOptions);
87+
res.json(userResponse(user));
88+
} catch (mailErr) {
89+
console.error(mailErr);
90+
res.status(500).json({ error: 'Failed to send verification email.' });
91+
}
92+
});
8393
} catch (err) {
94+
console.error(err);
8495
res.status(500).json({ error: err });
8596
}
8697
}

server/utils/mail.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,21 @@ class Mail {
1818
};
1919
}
2020

21-
sendMail(mailOptions) {
22-
return new Promise((resolve, reject) => {
23-
this.client.sendMail(mailOptions, (err, info) => {
24-
resolve(err, info);
25-
});
26-
});
21+
async sendMail(mailOptions) {
22+
const response = await this.client.sendMail(mailOptions);
23+
return response;
2724
}
2825

29-
dispatchMail(data, callback) {
26+
async send(data) {
3027
const mailOptions = {
3128
to: data.to,
3229
subject: data.subject,
3330
from: this.sendOptions.from,
3431
html: data.html
3532
};
3633

37-
return this.sendMail(mailOptions).then((err, res) => {
38-
callback(err, res);
39-
});
40-
}
41-
42-
send(data, callback) {
43-
return this.dispatchMail(data, callback);
34+
const response = await this.sendMail(mailOptions);
35+
return response;
4436
}
4537
}
4638

0 commit comments

Comments
 (0)