Skip to content
This repository was archived by the owner on Oct 11, 2022. It is now read-only.

Commit 372d377

Browse files
authored
Merge pull request #3986 from withspectrum/2.4.40
2.4.40
2 parents 65e5483 + 0e46536 commit 372d377

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

api/authentication.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ const GITHUB_OAUTH_CLIENT_ID = IS_PROD
4646
? '208a2e8684d88883eded'
4747
: 'ed3e924f4a599313c83b';
4848

49+
const isSerializedJSON = (str: string) =>
50+
str[0] === '{' && str[str.length - 1] === '}';
51+
4952
const init = () => {
5053
// Setup use serialization
5154
passport.serializeUser((user, done) => {
@@ -56,19 +59,21 @@ const init = () => {
5659
// to avoid having to go to the db on every single request. We have to handle both
5760
// cases here, as more and more users use Spectrum again we go to the db less and less
5861
passport.deserializeUser((data, done) => {
59-
// Fast path: try to JSON.parse the data if it works, we got the user data, yay!
60-
try {
61-
const user = JSON.parse(data);
62-
// Make sure more than the user ID is in the data by checking any other required
63-
// field for existance
64-
if (user.id && user.createdAt) {
62+
// Fast path: we got the full user data in the cookie
63+
if (isSerializedJSON(data)) {
64+
let user;
65+
// Ignore errors if our isSerializedJSON heuristic is wrong and `data` isn't serialized JSON
66+
try {
67+
user = JSON.parse(data);
68+
} catch (err) {}
69+
70+
if (user && user.id && user.createdAt) {
6571
return done(null, user);
6672
}
67-
// Ignore JSON parsing errors
68-
} catch (err) {}
73+
}
6974

7075
// Slow path: data is just the userID (legacy), so we have to go to the db to get the full data
71-
getUser({ id: data })
76+
return getUser({ id: data })
7277
.then(user => {
7378
done(null, user);
7479
})

api/mutations/user/editUser.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ export default requireAuth(
1313
args: EditUserInput,
1414
{ user, updateCookieUserData }: GraphQLContext
1515
) => {
16-
const dbUser = await getUser({ username: args.input.username });
17-
16+
const currentUser = user;
17+
// If the user is trying to change their username check whether there's a person with that username already
1818
if (args.input.username) {
1919
if (
2020
args.input.username === 'null' ||
@@ -31,6 +31,7 @@ export default requireAuth(
3131
return new UserError('Nice try! 😉');
3232
}
3333

34+
const dbUser = await getUser({ username: args.input.username });
3435
if (dbUser && dbUser.id !== user.id) {
3536
trackQueue.add({
3637
userId: user.id,
@@ -47,8 +48,9 @@ export default requireAuth(
4748
}
4849

4950
await updateCookieUserData({
50-
...dbUser,
51-
...{ ...args.input, file: undefined },
51+
...(await getUser({ id: currentUser.id })),
52+
...args.input,
53+
file: undefined,
5254
});
5355
return editUser(args, user.id);
5456
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "Spectrum",
3-
"version": "2.4.39",
3+
"version": "2.4.40",
44
"license": "BSD-3-Clause",
55
"devDependencies": {
66
"babel-cli": "^6.24.1",

0 commit comments

Comments
 (0)