Skip to content

Addresses various issues for current editor crash #3093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions client/modules/User/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,23 @@ export function validateAndSignUpUser(formValues) {
}

export function getUser() {
return (dispatch) => {
apiClient
.get('/session')
.then((response) => {
dispatch(authenticateUser(response.data));
dispatch({
type: ActionTypes.SET_PREFERENCES,
preferences: response.data.preferences
});
setLanguage(response.data.preferences.language, {
persistPreference: false
});
})
.catch((error) => {
const { response } = error;
const message = response.message || response.data.error;
dispatch(authError(message));
return async (dispatch) => {
try {
const response = await apiClient.get('/session');
const { data } = response;

dispatch(authenticateUser(data));
dispatch({
type: ActionTypes.SET_PREFERENCES,
preferences: data.preferences
});
setLanguage(data.preferences.language, { persistPreference: false });
} catch (error) {
const message = error.response
? error.response.data.error || error.response.message
: 'Unknown error.';
dispatch(authError(message));
}
};
}

Expand Down
39 changes: 22 additions & 17 deletions server/config/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,31 @@ passport.deserializeUser((id, done) => {
* Sign in using Email/Username and Password.
*/
passport.use(
new LocalStrategy({ usernameField: 'email' }, (email, password, done) => {
User.findByEmailOrUsername(email)
.then((user) => {
new LocalStrategy(
{ usernameField: 'email' },
async (email, password, done) => {
try {
const user = await User.findByEmailOrUsername(email);

if (!user) {
done(null, false, { msg: `Email ${email} not found.` });
return;
return done(null, false, { msg: `Email ${email} not found.` });
} else if (user.banned) {
done(null, false, { msg: accountSuspensionMessage });
return;
return done(null, false, { msg: 'Your account has been suspended.' });
}
user.comparePassword(password).then((isMatch) => {
if (isMatch) {
done(null, user);
} else {
done(null, false, { msg: 'Invalid email or password.' });
}
});
})
.catch((err) => done(null, false, { msg: err }));
})

const isMatch = await user.comparePassword(password);

if (isMatch) {
return done(null, user);
} else { // eslint-disable-line
return done(null, false, { msg: 'Invalid email or password' });
}
} catch (err) {
console.error(err);
return done(null, false, { msg: err });
}
}
)
);

/**
Expand Down
55 changes: 33 additions & 22 deletions server/controllers/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ export async function createUser(req, res) {
try {
const { username, email, password } = req.body;
const emailLowerCase = email.toLowerCase();
const existingUser = await User.findByEmailAndUsername(email, username);
if (existingUser) {
const fieldInUse =
existingUser.email.toLowerCase() === emailLowerCase
? 'Email'
: 'Username';
res.status(422).send({ error: `${fieldInUse} is in use` });
return;
}

const EMAIL_VERIFY_TOKEN_EXPIRY_TIME = Date.now() + 3600000 * 24; // 24 hours
const token = await generateToken();
const user = new User({
Expand All @@ -53,34 +63,35 @@ export async function createUser(req, res) {
verifiedToken: token,
verifiedTokenExpires: EMAIL_VERIFY_TOKEN_EXPIRY_TIME
});
const existingUser = await User.findByEmailAndUsername(email, username);
if (existingUser) {
const fieldInUse =
existingUser.email.toLowerCase() === emailLowerCase
? 'Email'
: 'Username';
res.status(422).send({ error: `${fieldInUse} is in use` });
return;
}

await user.save();
req.logIn(user, (loginErr) => {

req.logIn(user, async (loginErr) => {
if (loginErr) {
throw loginErr;
console.error(loginErr);
res.status(500).json({ error: 'Failed to log in user.' });
return;
}
});

const protocol = process.env.NODE_ENV === 'production' ? 'https' : 'http';
const mailOptions = renderEmailConfirmation({
body: {
domain: `${protocol}://${req.headers.host}`,
link: `${protocol}://${req.headers.host}/verify?t=${token}`
},
to: req.user.email
});
const protocol = process.env.NODE_ENV === 'production' ? 'https' : 'http';
const mailOptions = renderEmailConfirmation({
body: {
domain: `${protocol}://${req.headers.host}`,
link: `${protocol}://${req.headers.host}/verify?t=${token}`
},
to: req.user.email
});

await mail.send(mailOptions);
res.json(userResponse(req.user));
try {
await mail.send(mailOptions);
res.json(userResponse(user));
} catch (mailErr) {
console.error(mailErr);
res.status(500).json({ error: 'Failed to send verification email.' });
}
});
} catch (err) {
console.error(err);
res.status(500).json({ error: err });
}
}
Expand Down
20 changes: 6 additions & 14 deletions server/utils/mail.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,21 @@ class Mail {
};
}

sendMail(mailOptions) {
return new Promise((resolve, reject) => {
this.client.sendMail(mailOptions, (err, info) => {
resolve(err, info);
});
});
async sendMail(mailOptions) {
const response = await this.client.sendMail(mailOptions);
return response;
}

dispatchMail(data, callback) {
async send(data) {
const mailOptions = {
to: data.to,
subject: data.subject,
from: this.sendOptions.from,
html: data.html
};

return this.sendMail(mailOptions).then((err, res) => {
callback(err, res);
});
}

send(data, callback) {
return this.dispatchMail(data, callback);
const response = await this.sendMail(mailOptions);
return response;
}
}

Expand Down
Loading