Skip to content
This repository was archived by the owner on Mar 22, 2022. It is now read-only.

Error authenticating! Error: Token provided to verifyJWT is missing or not a string ? #584

Closed
ghost opened this issue Oct 10, 2017 · 9 comments

Comments

@ghost
Copy link

ghost commented Oct 10, 2017

I work with reactjs frontend. After create my server.js (feathers ) , i try to send my (username and password) to my route post . there is my code .

axios.post('http://localhost:4200/#', {
  strategy: 'local',
  username: 'k0',//self.state.username, password:'kk'//
  self.state.password
}).then(response => {
  console.log('Authenticated!', response);
  return app.passport.verifyJWT(response.accessToken);
}).then(payload => {
  console.log('JWT Payload', payload);
  return app.service('users').get(payload.userId);
}).then(user => {
  app.set('user', user);
  console.log('User', app.get('user'));
}).catch(function(error) {
  console.error('Error authenticating!', error);
});

When i click my button , there is my error in console "Error authenticating! Error: Token provided to verifyJWT is missing or not a string".

@daffl
Copy link
Member

daffl commented Oct 10, 2017

What is the /# route? What versions are you using? Unless you share your complete setup (which does not seem to be the standard generator structure) there unfortunately is not much we can do to help.

@ghost
Copy link
Author

ghost commented Oct 10, 2017

This is my route post where i will verify authenticate . I feel i did an big mistake
app.post('/#', auth.express.authenticate('local', { successRedirect: '/app', failureRedirect: '/#' }));

@daffl
Copy link
Member

daffl commented Oct 10, 2017

I meant the entire application setup. The minimum complete amount of code we need to reproduce your issue.

@ghost
Copy link
Author

ghost commented Oct 10, 2017

server.js

var Users=[
{_id:"1",email:"k0",password:"kk"},
{_id:"2",email:"k1",password:"kk"},
{_id:"3",email:"k2",password:"kk"},
{_id:"4",email:"k3",password:"kk"},
{_id:"5",email:"k4",password:"kk"}];
const feathers = require('feathers');
const bodyParser = require('body-parser');
const errors = require('feathers-errors');
const errorHandler = require('feathers-errors/handler');
const rest = require('feathers-rest');
const hooks = require('feathers-hooks');
const auth = require('feathers-authentication');
const local = require('feathers-authentication-local');
const memory = require('feathers-memory');
const jwt = require('feathers-authentication-jwt');
const app = feathers();
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }));
var cors = require('cors');
var port= process.env.PORT || 4200;
app.use(cors());
app.configure(rest())
.configure(hooks())
.use(bodyParser.json())
.use(bodyParser.urlencoded({ extended: true }))
.configure(auth({ secret: 'supersecret' }))
.configure(jwt())
.configure(local())
//.use('/users', memory());
.use('/users', {
   find(params) {
     return Promise.resolve(Users);
   }
 })
 .use(errorHandler());
app.service('authentication').hooks({
  before: {
    create: [
      auth.hooks.authenticate('local'),
      customizeJWTPayload()
    ],
    remove: [
      auth.hooks.authenticate('jwt')
    ]
  }
  });
  app.service('users').hooks({
  before: {

    create: [
      local.hooks.hashPassword({ passwordField: 'password' })
    ]
  }
});
  /////////////////
  app.post('/#', auth.express.authenticate('local', { successRedirect: '/app', failureRedirect: '/#' }));

  app.get('/app', (req, res, next) => {
  res.json({ success: true });
  });

  app.get('/#', (req, res, next) => {
  res.json({ success: "faux" });
  });
app.listen(port);

@ghost
Copy link
Author

ghost commented Oct 10, 2017

@daffl the error in the route /# ?

@ghost
Copy link
Author

ghost commented Oct 10, 2017

in my login.js
const feathers = require('feathers/client'); const rest = require('feathers-rest/client'); const hooks = require('feathers-hooks'); const localStorage = require('localstorage-memory'); //const jwt = require('feathers-authentication-jwt'); const auth = require('feathers-authentication-client'); const restClient = rest(); const app = feathers(); const axios = require('axios'); //app.configure(restClient.axios(axios)) app.configure(hooks()) .configure(rest('http://localhost:4200').axios(axios)) .configure(auth({ storage: localStorage })); axios.post('http://localhost:4200/#', { strategy: 'local', username: 'k0', password:'kk' self.state.password }).then(response => { console.log('Authenticated!', response); return app.passport.verifyJWT(response.accessToken); }).then(payload => { console.log('JWT Payload', payload); return app.service('users').get(payload.userId); }).then(user => { app.set('user', user); console.log('User', app.get('user')); }).catch(function(error) { console.error('Error authenticating!', error); });

@bertho-zero
Copy link
Contributor

bertho-zero commented Oct 10, 2017

@kilaniba You can put your code between ``` to make it clearer, if you wish.

@ghost
Copy link
Author

ghost commented Oct 10, 2017

@bertho-zero
var Users=[ {_id:"1",email:"k0",password:"kk"}, {_id:"2",email:"k1",password:"kk"}, {_id:"3",email:"k2",password:"kk"}, {_id:"4",email:"k3",password:"kk"}, {_id:"5",email:"k4",password:"kk"}]; const feathers = require('feathers'); const bodyParser = require('body-parser'); const errors = require('feathers-errors'); const errorHandler = require('feathers-errors/handler'); const rest = require('feathers-rest'); const hooks = require('feathers-hooks'); const auth = require('feathers-authentication'); const local = require('feathers-authentication-local'); const memory = require('feathers-memory'); const jwt = require('feathers-authentication-jwt'); const app = feathers(); app.use(bodyParser.json()) app.use(bodyParser.urlencoded({ extended: true })); var cors = require('cors'); var port= process.env.PORT || 4200;app.use(cors());
`

.use('/users', {
find(params) {
return Promise.resolve(Users);
}
})
.use(errorHandler());
app.service('authentication').hooks({
before: {
create: [
auth.hooks.authenticate('local'),
customizeJWTPayload()
],
remove: [
auth.hooks.authenticate('jwt')
]
}
});
app.service('users').hooks({
before: {

create: [
  local.hooks.hashPassword({ passwordField: 'password' })
]
}
`});app.post('/#', auth.express.authenticate('local', { successRedirect: '/app', failureRedirect: '/#'     }));

app.get('/app', (req, res, next) => {
res.json({ success: true });
});

app.get('/#', (req, res, next) => {
res.json({ success: "faux" });
});
app.listen(port);`

@daffl
Copy link
Member

daffl commented Oct 15, 2017

Going to close this since I think we solved it in feathersjs-ecosystem/authentication-local#36

@daffl daffl closed this as completed Oct 15, 2017
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants