-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
70 lines (58 loc) · 1.79 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
'use strict'
const authRoutes = require('../../@strapi/plugin-users-permissions/server/routes/content-api/auth')
const userRoutes = require('../../@strapi/plugin-users-permissions/server/routes/content-api/user')
const roleRoutes = require('../../@strapi/plugin-users-permissions/server/routes/content-api/role')
const permissionsRoutes = require('../../@strapi/plugin-users-permissions/server/routes/content-api/permissions')
const jwtCookieGetter = require('./middlewares/getter')
const jwtCookieSetter = require('./middlewares/setter')
const { COOKIE_NAME, destroyCookieOpts } = require('./config')
const authRoutesWithCookieMiddleware = authRoutes.map((r) => {
const middlewares = r.config?.middlewares ?? []
const middlewaresWithJwtCookies = [
...middlewares,
'plugin::users-permissions.jwtCookieSetter'
]
return {
...r,
config: {
...r.config,
middlewares: middlewaresWithJwtCookies
}
}
})
const logout = {
route: {
method: 'POST',
path: '/auth/logout',
handler: 'auth.logout',
config: { auth: false, prefix: '' }
},
controller: (ctx) => {
ctx.cookies.set(COOKIE_NAME.PAYLOAD, '', destroyCookieOpts)
ctx.cookies.set(COOKIE_NAME.HEADER_SIGNATURE, '', destroyCookieOpts)
ctx.response.status = 204
}
}
module.exports = (userConfig) => (plugin) => {
plugin.middlewares = {
...plugin.middlewares,
jwtCookieGetter,
jwtCookieSetter
}
plugin.controllers.auth = {
...plugin.controllers.auth,
logout: logout.controller
}
plugin.routes['content-api'].routes = [
...authRoutesWithCookieMiddleware,
logout.route,
...userRoutes,
...roleRoutes,
...permissionsRoutes
]
// apply user custom config strapi-server
if (typeof userConfig === 'function') {
return userConfig(plugin)
}
return plugin
}