mbnoimi
mbnoimi

Reputation: 440

How to save users using PostgreSQL

I followed up LoopBack JWT tutorial, but it didn't mention how to store credentials in a real datasource (ex. RDBM datasource)

To do that I simply created a datasource with connector: 'postgresql' but it didn't work because I always get this error message!

Request POST /signup failed with status code 500. error: relation "public.user" does not exist
    at Parser.parseErrorMessage (/home/laptop/LoopBack/multitenancy/node_modules/pg-protocol/src/parser.ts:357:11)
    at Parser.handlePacket (/home/laptop/LoopBack/multitenancy/node_modules/pg-protocol/src/parser.ts:186:21)
    at Parser.parse (/home/laptop/LoopBack/multitenancy/node_modules/pg-protocol/src/parser.ts:101:30)
    at Socket.<anonymous> (/home/laptop/LoopBack/multitenancy/node_modules/pg-protocol/src/index.ts:7:48)
    at Socket.emit (events.js:315:20)
    at addChunk (internal/streams/readable.js:309:12)
    at readableAddChunk (internal/streams/readable.js:284:9)
    at Socket.Readable.push (internal/streams/readable.js:223:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:188:23)

You can find the full source code here

BTW, When I run npm run migrate I expect User table to be generated because user.controller.ts contains on an extending of User class, but it didn't!

May you please guide me how to store credentials in RDBM datasource.

Upvotes: 0

Views: 731

Answers (1)

mbnoimi
mbnoimi

Reputation: 440

For the historical record; This issue has been resolved by Diana Lau as following:

  1. Create User & UserCredentials for integrating JWT
$ lb4 model User
? Please select the model base class Entity (A persisted model with an ID)
? Allow additional (free-form) properties? No
Model User will be created in src/models/user.model.ts

Let's add a property to User
Enter an empty property name when done

? Enter the property name:
   create src/models/user.model.ts
   update src/models/index.ts

Model User was/were created in src/models
$ lb4 model UserCredentials
? Please select the model base class Entity (A persisted model with an ID)
? Allow additional (free-form) properties? No
Model UserCredentials will be created in src/models/user-credentials.model.ts

Let's add a property to UserCredentials
Enter an empty property name when done

? Enter the property name:
   create src/models/user-credentials.model.ts
   update src/models/index.ts

Model UserCredentials was/were created in src/models
  1. Replace models/user.model.ts & models/user-credentials.model.ts by user.model.ts & user-credentials.model.ts

  2. Create repositories for User and UserCredentials

$ lb4 repository
? Please select the datasource PostgresqlDatasource
? Select the model(s) you want to generate a repository for UserCredentials, User
? Please select the repository base class DefaultCrudRepository (Juggler bridge)
? Please enter the name of the ID property for UserCredentials: id
? Please enter the name of the ID property for User: id
   create src/repositories/user-credentials.repository.ts
   create src/repositories/user.repository.ts
   update src/repositories/index.ts
   update src/repositories/index.ts

Repositories UserCredentialsRepository, UserRepository was/were created in src/repositories
  1. Run npm run build

  2. Modify datasources/postgresql.datasource.ts to meet the proper PostgreSQL configurations then create a new PostgreSQL database depending on your configurations.

  3. Run npm run migrate

  4. Delete the following files:

  • models/user.model.ts
  • models/user-credentials.model.ts
  • repositories/user.repository.ts
  • repositories/user-credentials.repository.ts
  1. Modify models/index.ts by deleting the following lines:
export * from './user.model';
export * from './user-credentials.model';
  1. Modify repositories/index.ts by deleting the following lines:
export * from './user-credentials.repository';
export * from './user.repository';

NOTE: I suggested a new feature request for a better JWT integration in this issue.

Upvotes: 2

Related Questions