Skip to content

Commit

Permalink
fix: fix buildAuthenticatedRouter
Browse files Browse the repository at this point in the history
  • Loading branch information
dziraf committed Apr 20, 2023
1 parent baee6c3 commit ef27e18
Show file tree
Hide file tree
Showing 8 changed files with 4,535 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
node_modules
.github
coommitlint.config.js
example-app
example
47 changes: 47 additions & 0 deletions example/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import AdminJS from 'adminjs'
import AdminJSKoa from '@adminjs/koa'
import Koa from 'koa'

const PORT = 3000

const DEFAULT_ADMIN = {
email: 'admin@example.com',
password: 'password',
}

const authenticate = async (email, password) => {
if (email === DEFAULT_ADMIN.email && password === DEFAULT_ADMIN.password) {
return Promise.resolve(DEFAULT_ADMIN)
}
return null
}

const start = async () => {
const app = new Koa()

const admin = new AdminJS({
resources: [],
rootPath: '/admin',
})

app.keys = ['your secret for koa cookie']
const router = AdminJSKoa.buildAuthenticatedRouter(admin, app, {
authenticate,
sessionOptions: {
// You may configure your Koa session here
httpOnly: process.env.NODE_ENV === 'production',
secure: process.env.NODE_ENV === 'production',
renew: true,
},
})

app.use(router.routes()).use(router.allowedMethods())

app.listen(PORT, () => {
console.log(
`AdminJS available at http://localhost:${PORT}${admin.options.rootPath}`,
)
})
}

start()
22 changes: 22 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "example",
"version": "1.0.0",
"type": "module",
"license": "MIT",
"scripts": {
"start:auth": "node auth.js",
"dev:auth": "nodemon auth.js",
"start:simple": "node simple.js",
"dev:simple": "nodemon simple.js"
},
"dependencies": {
"@adminjs/koa": "file:../",
"@koa/router": "^12.0.0",
"adminjs": "^7.0.0",
"koa": "^2.14.2",
"koa2-formidable": "^1.0.3"
},
"devDependencies": {
"nodemon": "^2.0.22"
}
}
25 changes: 25 additions & 0 deletions example/simple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import AdminJS from 'adminjs'
import AdminJSKoa from '@adminjs/koa'
import Koa from 'koa'

const PORT = 3000

const start = async () => {
const app = new Koa()
const admin = new AdminJS({
resources: [],
rootPath: '/',
})

const router = AdminJSKoa.buildRouter(admin, app)

app
.use(router.routes())
.use(router.allowedMethods())

app.listen(PORT, () => {
console.log(`AdminJS available at http://localhost:${PORT}${admin.options.rootPath}`)
})
}

start()
Loading

0 comments on commit ef27e18

Please sign in to comment.