Search code examples
feathersjs

feathers.js -> Authentication token missing


I have a feathters.js application and now I want to secure the create and update hooks. I use a socket.io client and currently am going for JWT. I have added what I think I needed to add but am getting Error: Authentication token missing and Error Authenticating. The later I understand for that is from my code. I have a backend / frontend situation

So this is what I've implemented so far.

File: backend\backend.js (called in backend\index.js for the configuration of the app)

'use strict';

const path = require('path');
const serveStatic = require('feathers').static;
const favicon = require('serve-favicon');
const compress = require('compression');
const cors = require('cors');
const feathers = require('feathers');
const configuration = require('feathers-configuration');
const authentication = require('feathers-authentication');
const hooks = require('feathers-hooks');
const rest = require('feathers-rest');
const bodyParser = require('body-parser');
const socketio = require('feathers-socketio');
const middleware = require('./middleware/index');
const services = require('./services/index');

const appFeathers = feathers();

appFeathers.configure(configuration(path.join(__dirname, '..')));

appFeathers.use(compress())
    .options('*', cors())
    .use(cors())
    .use(favicon(path.join(appFeathers.get('public'), 'favicon.ico')))
    .use('/', serveStatic(appFeathers.get('public')))
    .use(bodyParser.json())
    .use(bodyParser.urlencoded({extended: true}))
    .configure(hooks())
    .configure(rest())
    .configure(socketio())
    .configure(services)
    .configure(middleware)
    .configure(authentication());

module.exports = appFeathers;

File: backend\config\default.json

{
    "host": "localhost",
    "port": 3001,
    "mysql_connection": "mysql://CONNECTION_STRING",
    "public": "../public/",
    "auth": {
        "idField": "id",
        "token": {
            "secret": "SECRET_KEY"
        },
        "local": {}
    }
}

In a working component of the frontend:

<template>
    <div class="vttIndex">
        idnex.vue
        todo: eagle.js slideshow
        todo: first info
        <ul>
            <li v-for="message in listMessages">
                {{ message }}
            </li>
        </ul>
    </div>
</template>

<script>
    import feathers from 'feathers/client';
    import socketio from 'feathers-socketio/client';
    import hooks from 'feathers-hooks';
    import io from 'socket.io-client';
    import authentication from 'feathers-authentication/client';
    import * as process from "../nuxt.config";

    const vttSocket = io(process.env.backendUrl);
    const vttFeathers = feathers()
        .configure(socketio(vttSocket))
        .configure(hooks())
        .configure(authentication());

    const serviceMessage = vttFeathers.service('messages');


vttFeathers.authenticate({
    type: 'token',
    'token ': 'SECRET_KEY'
}).then(function(result){
    console.log('Authenticated!', result);
}).catch(function(error){
    console.error('Error authenticating!', error);
});

    export default {
        layout: 'default',
        data: function() {
            return {
                listMessages: []
            }
        },
        mounted: function() {
            serviceMessage.find().then(page => {
                this.listMessages = page.data;
            });
            serviceMessage.on('created', (serviceMessage) => {
                this.listMessages.push(serviceMessage);
            });
        }
    }
</script>

As token, I have the secret key of the backend json file. As you see, now I only try to log console messages. It is doing something for my error message is coming from there.

Question

Where am I missing what to have this functioning?

Goal

Just in case it's needed. My goal is for all 'public' data to be select with a token in my client and then an admin section maybe with 0auth. So the general 'SELECT' stuff is secured through a token instead of no authentication at all.

Solution

Okay I solved it, sort of. I first needed to create a user. Then I needed to do a local login with the user. That returns a token. If I use that, then there is no problem at all.


Solution

  • To use a token, you must first make sure it is generated. I was using the secret key as token what isn't correct. When you first athenticate with the 'local' type (default email and password) it will create a token and that is what you could then use with the method 'token'