adminJS buildAuthenticatedRouter login fails

huangapple go评论68阅读模式
英文:

adminJS buildAuthenticatedRouter login fails

问题

我正在尝试将adminJS连接到MongoDB数据库并实现已验证的adminJS门户。我正在使用此处的已验证示例作为起点,该示例基于postgres,然后根据此教程的代码进行了修改,以便使用MongoDB。

问题在于,当我运行 node server.js 时,我能够在localhost:3000/admin看到登录门户,我可以正确输入用户名和密码,然后看到消息 "登录成功!",但然后它只是重新加载登录页面。我认为身份验证路由出了问题,因为当我移除身份验证并只使用buildRouter时,它可以正常工作。

以下是来自index.js的我的代码:

import AdminJS from 'adminjs'
import * as AdminJSExpress from '@adminjs/express'
import express from 'express'
import mongoose from 'mongoose'
import * as AdminJSMongoose from "@adminjs/mongoose"
import session from 'express-session'
import MongoStore from 'connect-mongo'

import dotenv from 'dotenv'
dotenv.config()

import Users   from './app/models/user.js';
const PORT = 3000

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

AdminJS.registerAdapter({
  Resource: AdminJSMongoose.Resource,
  Database: AdminJSMongoose.Database,
})


const authenticate = async (email, password) => {
  if (email === DEFAULT_ADMIN.email && password === DEFAULT_ADMIN.password) {
    console.log("登录成功!");
    return Promise.resolve(DEFAULT_ADMIN)
  }
  return null
}

const start = async () => {
  const app = express()

mongoose.connect(process.env.DB_URI,{
  useNewUrlParser: true,
  useUnifiedTopology: true
});

const sessionStore =  MongoStore.create({
    client: mongoose.connection.getClient(),
    collectionName: "session",
    stringify: false,
    autoRemove: "interval",
    autoRemoveInterval: 1
    });
const adminOptions = {
  resources:[Users],
  rootPath:"/admin"
}

const admin = new AdminJS(adminOptions);

const adminRouter = AdminJSExpress.default.buildAuthenticatedRouter(
    admin,
    {
      authenticate,
      cookieName: 'adminjs',
      cookiePassword: 'sessionsecret',
    },
    null,
    {
      store: sessionStore,
      resave: true,
      saveUninitialized: true,
      secret: 'sessionsecret',
      cookie: {
        httpOnly: process.env.NODE_ENV === 'production',
        secure: process.env.NODE_ENV === 'production',
      },
      name: 'adminjs',
    }
  )
  app.use(admin.options.rootPath, adminRouter)

  app.listen(PORT, () => {
    console.log(`AdminJS 在 http://localhost:${PORT}${admin.options.rootPath} 上启动`)
  })
}

start()
英文:

What I'm trying to do

I am trying to connect adminJS to a MongoDB database and implement an authenticated adminJS portal. I am using the authenticated example here to start, which is based off postgres, and then I have modified it based off code from this tutorial in order to use mongoDB instead.

The problem

The problem is, when I run node server.js I am able to see the login portal at localhost:3000/admin and I can correctly put in username and password, and I see the message "Login worked!" but then it just reloads the login page. I think something is going wrong with the authentication routing, because when I remove authentication and just use buildRouter instead, it works fine.

My code from index.js

import AdminJS from 'adminjs'
import * as AdminJSExpress from '@adminjs/express'
import express from 'express'
import mongoose from 'mongoose'
import * as AdminJSMongoose from "@adminjs/mongoose"
import session from 'express-session'
import MongoStore from 'connect-mongo'
import dotenv from 'dotenv'
dotenv.config()
import Users   from './app/models/user.js';
const PORT = 3000
const DEFAULT_ADMIN = {
email: 'admin@example.com',
password: 'password',
}
AdminJS.registerAdapter({
Resource: AdminJSMongoose.Resource,
Database: AdminJSMongoose.Database,
})
const authenticate = async (email, password) => {
if (email === DEFAULT_ADMIN.email && password === DEFAULT_ADMIN.password) {
console.log("Login worked!");
return Promise.resolve(DEFAULT_ADMIN)
}
return null
}
const start = async () => {
const app = express()
mongoose.connect(process.env.DB_URI,{
useNewUrlParser: true,
useUnifiedTopology: true
// useFindAndModify: false,
// useCreateIndex: true
});
const sessionStore =  MongoStore.create({
client: mongoose.connection.getClient(),
collectionName: "session",
stringify: false,
autoRemove: "interval",
autoRemoveInterval: 1
});
const adminOptions = {
resources:[Users],
rootPath:"/admin"
}
const admin = new AdminJS(adminOptions);
// const adminRouter = AdminJSExpress.default.buildRouter(admin);;
const adminRouter = AdminJSExpress.default.buildAuthenticatedRouter(
admin,
{
authenticate,
cookieName: 'adminjs',
cookiePassword: 'sessionsecret',
},
null,
{
store: sessionStore,
resave: true,
saveUninitialized: true,
secret: 'sessionsecret',
cookie: {
httpOnly: process.env.NODE_ENV === 'production',
secure: process.env.NODE_ENV === 'production',
},
name: 'adminjs',
}
)
app.use(admin.options.rootPath, adminRouter)
app.listen(PORT, () => {
console.log(`AdminJS started on http://localhost:${PORT}${admin.options.rootPath}`)
})
}
start()

答案1

得分: 0

问题出在使用了错误的DB_URL值。一旦我修复了这个问题,这段代码基本上就可以工作了。

英文:

The issue here had to do with using the wrong value for DB_URL. Once I fixed that, this code pretty much worked.

huangapple
  • 本文由 发表于 2023年5月15日 10:13:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76250486.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定