Skip to content

Commit

Permalink
fix: allow jwt to work without csrf in config
Browse files Browse the repository at this point in the history
  • Loading branch information
DanRibbens committed Feb 16, 2022
1 parent 56c16d5 commit 4048734
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
20 changes: 10 additions & 10 deletions demo/payload.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,16 @@ export default buildConfig({
NavigationArray,
GlobalWithStrictAccess,
],
cors: [
'http://localhost',
'http://localhost:3000',
'http://localhost:8080',
'http://localhost:8081',
],
csrf: [
'http://localhost:3000',
'https://other-app-here.com',
],
// cors: [
// 'http://localhost',
// 'http://localhost:3000',
// 'http://localhost:8080',
// 'http://localhost:8081',
// ],
// csrf: [
// 'http://localhost:3000',
// 'https://other-app-here.com',
// ],
routes: {
api: '/api',
admin: '/admin',
Expand Down
2 changes: 1 addition & 1 deletion src/auth/getExtractJWT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const getExtractJWT = (config: SanitizedConfig) => (req: Request): string | null
const tokenCookieName = `${config.cookiePrefix}-token`;

if (cookies && cookies[tokenCookieName]) {
if (!origin || (config.csrf && config.csrf.indexOf(origin) > -1)) {
if (!origin || config.csrf.length === 0 || config.csrf.indexOf(origin) > -1) {
return cookies[tokenCookieName];
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/config/sanitize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ const sanitizeConfig = (config: Config): SanitizedConfig => {
sanitizedConfig.globals = sanitizeGlobals(sanitizedConfig.collections, sanitizedConfig.globals);
}

sanitizedConfig.csrf = [
...sanitizedConfig.csrf,
config.serverURL,
];
if (sanitizedConfig.serverURL !== '') {
sanitizedConfig.csrf.push(sanitizedConfig.serverURL);
}

return sanitizedConfig as SanitizedConfig;
};
Expand Down

0 comments on commit 4048734

Please sign in to comment.