DEV Community

Discussion on: Intro: Fullstack JS User Roles & Management

Collapse
 
lturel profile image
lturel

Hi Rachel,

Thanks for this 3 parts series of posting (User Roles & Management), It is very concise and complete.

I am trying to implement your example with knex and Postgres.
I download and run your code (server/Mongoose and client) and it works fine, super.
Changed the server part, I used knex/postgres instead of mongoose/mongodb.

Progressing step by step, skipping other details, in (server) user.hooks.js

// --------------------------------------
module.exports = {
before: {
all: [],
find: [authenticate("jwt")],
get: [authenticate("jwt")],
create: [hashPassword("password"), verifyHooks.addVerification()],
// create: [hashPassword("password")],
update: [hashPassword("password"), authenticate("jwt")],
patch: [hashPassword("password"), authenticate("jwt")],
remove: [authenticate("jwt")],
},
// ------------------------------------------------

in "before / create" hook if I run only

create: [hashPassword("password")], it works fine and sends verification e-mail.

but if I add "verifyHooks.addVerification()" parameter,
create: [hashPassword("password"), verifyHooks.addVerification()],

it throws error as:
POST localhost:3030/users 400 (Bad Request) QAjaxBar.js?7ea5:65
and does NOT create a record in postgres database.

Another trial with knex, but SQLITE as the database;
it works fine with "verifyHooks.addVerification()" parameter, sends e-mail and CREATES a record in database, having "isVerified" as 0 (zero). But when I click the link in the e-mail, it doesn't complete verification steps and gives another error.

I used the same (knex) user model for both postgres and sqlite database cases.
// -----------------------------------------------------
db.schema
.dropTableIfExists("users")
.then(function () {

  // Initialize your table
  return db.schema.createTable("users", function (table) {
    table.increments("id");
    table.string("email").unique();
    table.string("password");

    table.string("permissions");
    table.timestamp("lastLoggedIn");
    table.integer("isVerified");
    table.string("verifyToken");
    table.string("verifyShortToken");
    table.timestamp("verifyExpires");
    table.json("verifyChanges");
    table.string("resetToken");
    table.string("resetShortToken");
    table.timestamp("resetExpires");
    table.integer("resetAttempts");
  });
})
.then(function () {
  console.log("CREATED USERS table");
})
.catch((e) => console.error(`Error creating ${tableName} table`, e));
Enter fullscreen mode Exit fullscreen mode

// -----------------------------------------------------

Is that a problem because of postgres' camelcase column name limitations?
What do you think about the source of problem would be?
Did you create any example with knex/Postgres similar to your example?

Thanks in advance for helping ideas on this issue.
Levent