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

NotAuthenticated response when including @feathersjs/authentication #13

Closed
cdrandin opened this issue Apr 14, 2018 · 1 comment
Closed

Comments

@cdrandin
Copy link

Most of the project setup was using feathers-cli.

> feathers g authentication

? What authentication providers do you want to use? Other PassportJS strategies not in this list can still be configured manually. Username + Password (Local)
? What is the name of the user (entity) service? users
    force config/default.json
   create src/authentication.js
    force src/app.js
// users.models.js
//
const {Model} = require('objection');

/* eslint-disable no-console */

// messages-model.js - A Objection.js/KnexJS
//
// See https://vincit.github.io/objection.js and http://knexjs.org/
// for more of what you can do here.

class Users extends Model {
  static get tableName() {
    return 'users';
  }

  static get jsonSchema() {
    return {
      type: 'object',
      required: [
        'email', 'password'
      ],

      properties: {
        id: {
          type: 'integer'
        },
        email: {
          type: 'string',
          pattern: String.raw `^[^@\s]+@[^@\s]+\.[^@\s]+$`
        },
        password: {
          type: 'string',
          'minLength': 4
        }
      }
    };
  }
}

module.exports = function(app) {
  const db = app.get('knexClient');
  const tableName = 'users';

  db.schema.hasTable(tableName).then(exists => {
    if (!exists) {
      db.schema.createTable(tableName, table => {
        table.increments();
        table.string('email').unique().notNullable();
        table.string('password', 100).notNullable();
        table.timestamps(true, true);
      }).then(() => console.log(`Created ${tableName} table`)).catch(e => console.error(`Error creating ${tableName} table`, e));
    }
  });

  return Users
};
// users.services.js
//
// Initializes the `users` service on path `/users`
const objectionService = require('feathers-objection');
const createModel = require('../../models/users.model');
const hooks = require('./users.hooks');

module.exports = function(app) {
  const Model = createModel(app);
  const paginate = app.get('paginate');

  const options = {
    model: Model,
    id: 'id',
    paginate
  };

  // Initialize our service with any options it requires
  app.use('/users', objectionService(options));

  // Get our initialized service so that we can register hooks and filters
  const service = app.service('users');

  service.hooks(hooks);
};

It took me a bit of time to get it working properly with this setup. Feathers out of the box is pretty intense when not using NeDB (which apparently makes everything instantly work, had to manually setup a lot of other stuff).

In the app I tried

app.service('authentication')
.create({strategy: 'local', email: '', password: ''})
.then(console.log);

Which processed just fine. Which leaves me to believe possibly I have it setup a bit incorrecting using objection to find my query. Something like app.service('users').get(1).then(console.log); works properly.

@cdrandin
Copy link
Author

Figured it out. When using feathers generate authentication with files that already existed it had trouble setting authenticate hooks into users.hooks.js

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

1 participant