Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: AdminJSExpress.buildAuthenticatedRouter is not a function #111

Open
dickorydock opened this issue May 12, 2023 · 3 comments
Open

Comments

@dickorydock
Copy link

dickorydock commented May 12, 2023

I've tried implementing the example of an authenticated router provided here but I am running into an error message.

I have changed to use my DB_URI and I am able to access DB_URI both locally and remotely on my fly.io app, but in both cases I run into this error:


  const adminRouter = AdminJSExpress.buildAuthenticatedRouter(
                                     ^

TypeError: AdminJSExpress.buildAuthenticatedRouter is not a function
    at start (etc)

Here is the code I am running, based off that adminjsexpress example:


import AdminJS from 'adminjs'
import * as AdminJSExpress from '@adminjs/express'
import express from 'express'
import Connect from 'connect-pg-simple'
import session from 'express-session'
// import  from 'express-session'
import * as dotenv from 'dotenv' // see https://github.com/motdotla/dotenv#how-do-i-use-dotenv-with-import
dotenv.config()

const PORT = 3000

const DEFAULT_ADMIN = {
  email: '[email protected]',
  password: 'password',
}

const authenticate = async (email, password) => {
  if (email === DEFAULT_ADMIN.email && password === DEFAULT_ADMIN.password) {
    return Promise.resolve(DEFAULT_ADMIN)
  }
  return null
}
console.log("hi");
console.log(process.env.DB_URI);
const start = async () => {
  const app = express()

  const admin = new AdminJS({})

  const ConnectSession = Connect(session)
  const sessionStore = new ConnectSession({
    conObject: {
      connectionString: process.env.DB_URI,
      ssl: process.env.NODE_ENV === 'production',
    },
    tableName: 'session',
    createTableIfMissing: true,
  })

  const adminRouter = AdminJSExpress.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()
@davyjones3392
Copy link

davyjones3392 commented May 17, 2023

I am having the exact same issue. Any help?

@dziraf
Copy link
Contributor

dziraf commented May 17, 2023

Change:

import * as AdminJSExpress from '@adminjs/express'

to

import AdminJSExpress from '@adminjs/express'

This was updated only in one snippet of the docs, I'll fix it later.

dickorydock added a commit to dickorydock/adminjs-typeorm that referenced this issue May 17, 2023
Removed '*' from AdminJSExpress import. See: SoftwareBrothers/adminjs-expressjs#111
@dfioravanti
Copy link

Change:

import * as AdminJSExpress from '@adminjs/express'

to

import AdminJSExpress from '@adminjs/express'

This was updated only in one snippet of the docs, I'll fix it later.

Just as a FYI, you should update the import in https://docs.adminjs.co/installation/plugins/express too. It looks me a while to track down why the base example was not compiling

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants