Source

adminjs-koa/src/index.ts

  1. /**
  2. * @module @adminjs/koa
  3. * @description
  4. *
  5. * This is an official plugin allowing you to run AdminJS on
  6. * [koa framework](https://koajs.com/).
  7. *
  8. * ## installation
  9. *
  10. * Assuming you have koa installed, you have to also install this
  11. * package along with its peerDependencies:
  12. *
  13. * ```
  14. * yarn add adminjs @adminjs/koa @koa/router koa2-formidable
  15. * ```
  16. *
  17. * now you can use either {@link module:@adminjs/koa.buildRouter buildRouter} or
  18. * {@link module:@adminjs/koa.buildAuthenticatedRouter buildAuthenticatedRouter} functions.
  19. *
  20. * ## Usage
  21. *
  22. * ```javascript
  23. * const { buildRouter } = require('@adminjs/koa')
  24. * // or
  25. * const { buildAuthenticatedRouter } = require('@adminjs/koa')
  26. * ```
  27. *
  28. * As you can see it exposes 2 methods that create an Koa Router, which can be attached
  29. * to a given url in the API. Each method takes a pre-configured instance
  30. * of {@link AdminJS}.
  31. *
  32. * If you want to use a router you have already created - not a problem. Just pass it
  33. * as a `predefinedRouter` parameter.
  34. *
  35. * You may want to use this option when you want to include
  36. * some custom auth middleware for you AdminJS routes.
  37. *
  38. * ## Example without an authentication
  39. *
  40. * ```
  41. * const AdminJS = require('adminjs')
  42. * const { buildRouter } = require('@adminjs/koa')
  43. *
  44. * const Koa = require('koa');
  45. * const app = new Koa();
  46. *
  47. * const adminJs = new AdminJS({
  48. * databases: [],
  49. * rootPath: '/admin',
  50. * })
  51. *
  52. * const router = buildRouter(adminJs, app)
  53. *
  54. * app
  55. * .use(router.routes())
  56. * .use(router.allowedMethods())
  57. *
  58. * app.listen(3000)
  59. * ```
  60. *
  61. * ## Using build in authentication
  62. *
  63. * Plugin gives you a second method:
  64. * {@link module:@adminjs/koa.buildAuthenticatedRouter buildAuthenticatedRouter}. In order
  65. * to have sign in logic out of the box - you can use it.
  66. *
  67. * ### Example with build in authentication
  68. *
  69. * Build in authentication is using cookie. So in order to make it work you have to set
  70. * set koa [app.keys](https://koajs.com/#app-keys-):
  71. *
  72. * ```
  73. * const app = new Koa();
  74. * app.keys = ['super-secret1', super-'secret2']
  75. * ```
  76. *
  77. * And this is how {@link module:@adminjs/koa.buildAuthenticatedRouter buildAuthenticatedRouter}
  78. * might look like:
  79. *
  80. * ```
  81. * const router = buildAuthenticatedRouter(AdminJS, app, {
  82. * authenticate: async (email, password) => {
  83. * const user = await User.findOne({ email })
  84. * if (password && user && await argon2.verify(user.encryptedPassword, password)){
  85. * return user.toJSON()
  86. * }
  87. * return null
  88. * },
  89. * })
  90. * ```
  91. *
  92. * - We used [argon2](https://www.npmjs.com/package/argon2) to decrypt the password.
  93. * - In the example User is a [mongoose](https://mongoosejs.com/) model.
  94. *
  95. *
  96. * ## Adding custom authentication
  97. *
  98. * You can add your custom authentication setup by firstly creating the router and then
  99. * passing it via the `predefinedRouter` option.
  100. *
  101. * In this predefined router you can protect the routes with a session
  102. * authentication where you can use any middleware you want.
  103. * Furthermore, you can create your own sign-in/sign-up form.
  104. */
  105. import buildRouter from './buildRouter'
  106. import buildAuthenticatedRouter from './buildAuthenticatedRouter'
  107. // eslint-disable-next-line @typescript-eslint/no-var-requires
  108. const { version } = require('../package.json')
  109. const name = 'AdminJSKoa'
  110. const defaultExport = {
  111. buildRouter,
  112. buildAuthenticatedRouter,
  113. version,
  114. name,
  115. }
  116. export {
  117. buildRouter,
  118. buildAuthenticatedRouter,
  119. version,
  120. name,
  121. defaultExport as default,
  122. }