Skip to content

Commit 3b4d5af

Browse files
fix: avoid assuming Email will be present on JWT token. Using ID instead as email might not be in if using disableLocalStrategy (#789)
1 parent 3a3026c commit 3b4d5af

File tree

1 file changed

+4
-26
lines changed

1 file changed

+4
-26
lines changed

src/auth/strategies/jwt.ts

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import passportJwt, { StrategyOptions } from 'passport-jwt';
22
import { Strategy as PassportStrategy } from 'passport-strategy';
33
import { Payload } from '../..';
4-
import find from '../../collections/operations/find';
4+
import findByID from '../../collections/operations/findByID';
55
import getExtractJWT from '../getExtractJWT';
66

77
const JwtStrategy = passportJwt.Strategy;
@@ -21,41 +21,19 @@ export default ({ secret, config, collections }: Payload): PassportStrategy => {
2121
try {
2222
const collection = collections[token.collection];
2323

24-
const where: { [key: string]: any } = {};
25-
if (collection.config.auth.verify) {
26-
where.and = [
27-
{
28-
email: {
29-
equals: token.email,
30-
},
31-
},
32-
{
33-
_verified: {
34-
not_equals: false,
35-
},
36-
},
37-
];
38-
} else {
39-
where.email = {
40-
equals: token.email,
41-
};
42-
}
43-
4424
const isGraphQL = (req.url || '').replace(/\/$/, '') === config.routes.graphQL.replace(/\/$/, '');
4525

46-
const userQuery = await find({
47-
where,
26+
const user = await findByID({
27+
id: token.id,
4828
collection,
4929
req,
5030
overrideAccess: true,
5131
depth: isGraphQL ? 0 : collection.config.auth.depth,
5232
});
5333

54-
if (userQuery.docs && userQuery.docs.length > 0) {
55-
const user = userQuery.docs[0];
34+
if(user && (!collection.config.auth.verify || user._verified)) {
5635
user.collection = collection.config.slug;
5736
user._strategy = 'local-jwt';
58-
5937
done(null, user);
6038
} else {
6139
done(null, false);

0 commit comments

Comments
 (0)