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

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

Closed
Johnba1988 opened this Issue Oct 10, 2017 · 9 comments

Comments

Projects
None yet
3 participants
@Johnba1988
Copy link

Johnba1988 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/login', {
  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

This comment has been minimized.

Copy link
Member

daffl commented Oct 10, 2017

What is the /login 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.

@Johnba1988

This comment has been minimized.

Copy link
Author

Johnba1988 commented Oct 10, 2017

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

@daffl

This comment has been minimized.

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.

@Johnba1988

This comment has been minimized.

Copy link
Author

Johnba1988 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('/login', auth.express.authenticate('local', { successRedirect: '/app', failureRedirect: '/login' }));

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

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

This comment has been minimized.

Copy link
Author

Johnba1988 commented Oct 10, 2017

@daffl the error in the route /login ?

@Johnba1988

This comment has been minimized.

Copy link
Author

Johnba1988 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/login', { 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

This comment has been minimized.

Copy link
Contributor

bertho-zero commented Oct 10, 2017

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

@Johnba1988

This comment has been minimized.

Copy link
Author

Johnba1988 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('/login', auth.express.authenticate('local', { successRedirect: '/app', failureRedirect: '/login'     }));

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

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

This comment has been minimized.

Copy link
Member

daffl commented Oct 15, 2017

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

@daffl daffl closed this Oct 15, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.