Skip to content

Commit f22f56e

Browse files
authored
feat: use provided auth strategy name or strategy.name (#797)
* feat: use provided auth strategy name or strategy.name * fix: require strategy name when providing custom auth
1 parent 3b4d5af commit f22f56e

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

src/collections/config/schema.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import joi from 'joi';
22
import { componentSchema } from '../../utilities/componentSchema';
33

4+
const strategyBaseSchema = joi.object().keys({
5+
refresh: joi.boolean(),
6+
logout: joi.boolean(),
7+
});
8+
49
const collectionSchema = joi.object().keys({
510
slug: joi.string().required(),
611
labels: joi.object({
@@ -88,15 +93,18 @@ const collectionSchema = joi.object().keys({
8893
}),
8994
maxLoginAttempts: joi.number(),
9095
disableLocalStrategy: joi.boolean().valid(true),
91-
strategies: joi.array().items(joi.object().keys({
92-
name: joi.string(),
93-
strategy: joi.alternatives().try(
94-
joi.func().maxArity(1).required(),
95-
joi.object()
96-
),
97-
refresh: joi.boolean(),
98-
logout: joi.boolean(),
99-
})),
96+
strategies: joi.array().items(joi.alternatives().try(
97+
strategyBaseSchema.keys({
98+
name: joi.string().required(),
99+
strategy: joi.func()
100+
.maxArity(1)
101+
.required(),
102+
}),
103+
strategyBaseSchema.keys({
104+
name: joi.string(),
105+
strategy: joi.object().required(),
106+
}),
107+
)),
100108
}),
101109
joi.boolean(),
102110
),

src/express/middleware/authenticate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ export default (config: SanitizedConfig): PayloadAuthenticate => {
1212
const collectionMethods = [...enabledMethods];
1313

1414
if (Array.isArray(collection.auth.strategies)) {
15-
collection.auth.strategies.forEach(({ name }, index) => {
16-
collectionMethods.unshift(`${collection.slug}-${name ?? index}`);
15+
collection.auth.strategies.forEach(({ name, strategy }) => {
16+
collectionMethods.unshift(`${collection.slug}-${name ?? strategy.name}`);
1717
});
1818
}
1919

0 commit comments

Comments
 (0)