I'm using adminJS with Postgres & AdminJSExpress.buildAuthenticatedRouter data isn't saved into the database rather it's saved & read in session

136 views Asked by At

I'm using adminJS with Postgres & AdminJSExpress.buildAuthenticatedRouter The problem is that the resource data is not reading from the database tables, rather it's reading from session records. Also the save is saving into session

import AdminJS from 'adminjs'
import AdminJSExpress from '@adminjs/express'
import express from 'express'
import Connect from 'connect-pg-simple'
import session from 'express-session'

import { Adapter, Resource, Database } from '@adminjs/sql'




const PORT = 3000
const DEFAULT_ADMIN = {email: 'email',password: 'password',}
AdminJS.registerAdapter({Database,Resource,Adapter})



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 = express()
  
  const db = await new Adapter('postgresql', {
    connectionString: 'postgres://user:pwd@localhist:5432/',
    database: 'myDB',
  }).init();
  
  const admin = new AdminJS({
    resources: [
      {resource: db.table('user'), options: {id: 'Users',},},
      {resource: db.table('admin'),
                options: {
                  id: 'Admins',  
                  properties: {
                    id: { isVisible: { list: true, filter: false, show: true, edit: false } },
                    email: { isVisible: { list: true, filter: true, show: true, edit: true } },
                    firstName: { isVisible: { list: true, filter: true, show: true, edit: true } },
                    lastName: { isVisible: { list: true, filter: true, show: true, edit: true } },
                    password: { isVisible: { list: false, filter: false, show: false, edit: false } },
                    photoUrl: { isVisible: { list: true, filter: false, show: true, edit: false } },
                    lastLoginDate: { isVisible: { list: true, filter: false, show: false, edit: false } },
                  },
              },
    },
    ],
  })
  admin.watch()

 
  const ConnectSession = Connect(session)
  const sessionStore = new ConnectSession({
    conObject: {
      connectionString: 'postgres://user:pwd@localhist:5432/myDB',
      ssl: process.env.NODE_ENV === 'production',
    },
    tableName: 'session',
    createTableIfMissing: true,
  })

  const adminRouter = AdminJSExpress.buildAuthenticatedRouter(
    admin,
    {
      authenticate,
      cookieName: 'cookieName',
      cookiePassword: 'secret',
    },
    null,
    {
      store: sessionStore,
      resave: true,
      saveUninitialized: true,
      secret: 'secret',
      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()`

Expecting data to be saved in database tables rather than session table

0

There are 0 answers