Skip to content
This repository has been archived by the owner on Jul 31, 2019. It is now read-only.

Authentication tutorial error: value must be an object, undefined handler #102

Closed
hickeyadamc opened this issue Dec 12, 2014 · 8 comments · May be fixed by tejzpr/hapijs.com#9
Closed

Authentication tutorial error: value must be an object, undefined handler #102

hickeyadamc opened this issue Dec 12, 2014 · 8 comments · May be fixed by tejzpr/hapijs.com#9
Assignees
Labels

Comments

@hickeyadamc
Copy link

Hey!

The Authentication tutorial gives the error: Invalid server options 3000, value must be an object.

Copy pasted from tutorial:

var Bcrypt = require('bcrypt');
var Hapi = require('hapi');
var Basic = require('hapi-auth-basic');

var server = new Hapi.Server( 3000 );

var users = {
    john: {
        username: 'john',
        password: '$2a$10$iqJSHD.BGr0E2IxQwYgJmeP3NvhPrXAeLSaGCj6IR/XU5QtjVu5Tm',   // 'secret'
        name: 'John Doe',
        id: '2133d32a'
    }
};

var validate = function (username, password, callback) {
    var user = users[username];
    if (!user) {
        return callback(null, false);
    }

    Bcrypt.compare(password, user.password, function (err, isValid) {
        callback(err, isValid, { id: user.id, name: user.name });
    });
};

server.register(Basic, function (err) {
    server.auth.strategy('simple', 'basic', { validateFunc: validate });
    server.route({ method: 'GET', path: '/', config: { auth: 'simple' } });
});

That error can be fixed by changing:

var server = new Hapi.Server( );
server.connection({ port: 3000 });

Which results in a new error:

/Users/adamhickey/Prototype/node_modules/hapi/node_modules/hoek/lib/index.js:663
    throw new Error(msgs.join(' ') || 'Unknown error');
          ^
Error: Missing or undefined handler: GET /
    at Object.exports.assert (/Users/adamhickey/Prototype/node_modules/hapi/node_modules/hoek/lib/index.js:663:11)
    at new module.exports.internals.Route (/Users/adamhickey/Prototype/node_modules/hapi/lib/route.js:35:10)
    at internals.Connection._addRoute (/Users/adamhickey/Prototype/node_modules/hapi/lib/connection.js:342:17)
    at internals.Connection._route (/Users/adamhickey/Prototype/node_modules/hapi/lib/connection.js:334:18)
    at internals.Plugin._apply (/Users/adamhickey/Prototype/node_modules/hapi/lib/plugin.js:432:14)
    at internals.Plugin.route (/Users/adamhickey/Prototype/node_modules/hapi/lib/plugin.js:407:10)
    at /Users/adamhickey/Prototype/server.js:30:12
    at done (/Users/adamhickey/Prototype/node_modules/hapi/node_modules/items/lib/index.js:30:25)
    at Object.exports.register (/Users/adamhickey/Prototype/node_modules/hapi-auth-basic/lib/index.js:15:5)
    at /Users/adamhickey/Prototype/node_modules/hapi/lib/plugin.js:235:14

@nlf nlf added the bug label Dec 12, 2014
@nlf nlf self-assigned this Dec 12, 2014
@nlf nlf closed this as completed in d88afc8 Dec 12, 2014
@nlf
Copy link
Contributor

nlf commented Dec 12, 2014

Fixed and deployed, sorry about that!

@hickeyadamc
Copy link
Author

I am still getting this error:

/Users/adamhickey/Prototype/node_modules/hapi/node_modules/hoek/lib/index.js:663
    throw new Error(msgs.join(' ') || 'Unknown error');
          ^
Error: Missing or undefined handler: GET /
    at Object.exports.assert (/Users/adamhickey/Prototype/node_modules/hapi/node_modules/hoek/lib/index.js:663:11)
    at new module.exports.internals.Route (/Users/adamhickey/Prototype/node_modules/hapi/lib/route.js:35:10)
    at internals.Connection._addRoute (/Users/adamhickey/Prototype/node_modules/hapi/lib/connection.js:342:17)
    at internals.Connection._route (/Users/adamhickey/Prototype/node_modules/hapi/lib/connection.js:334:18)
    at internals.Plugin._apply (/Users/adamhickey/Prototype/node_modules/hapi/lib/plugin.js:432:14)
    at internals.Plugin.route (/Users/adamhickey/Prototype/node_modules/hapi/lib/plugin.js:407:10)
    at /Users/adamhickey/Prototype/server.js:30:12
    at done (/Users/adamhickey/Prototype/node_modules/hapi/node_modules/items/lib/index.js:30:25)
    at Object.exports.register (/Users/adamhickey/Prototype/node_modules/hapi-auth-basic/lib/index.js:15:5)
    at /Users/adamhickey/Prototype/node_modules/hapi/lib/plugin.js:235:14

@nlf nlf reopened this Dec 12, 2014
@nlf
Copy link
Contributor

nlf commented Dec 12, 2014

I'll check it out and see what's going on, sorry for the premature close.

@hickeyadamc
Copy link
Author

No problem! Thanks for the fast response!

Turns out the error was caused by lack of a handler. Also, a call to start might be helpful too. Here's what worked for me:

var Bcrypt = require('bcrypt');
var Hapi = require('hapi');
var Basic = require('hapi-auth-basic');

var server = new Hapi.Server();
server.connection({ port: 3000 });

var users = {
    john: {
        username: 'john',
        password: '$2a$10$iqJSHD.BGr0E2IxQwYgJmeP3NvhPrXAeLSaGCj6IR/XU5QtjVu5Tm',   // password is: 'secret'
        name: 'John Doe',
        id: '2133d32a'
    }
};

var validate = function (username, password, callback) {
    var user = users[username];
    if (!user) {
        return callback(null, false);
    }

    Bcrypt.compare(password, user.password, function (err, isValid) {
        callback(err, isValid, { id: user.id, name: user.name });
    });
};

server.register(Basic, function (err) {
    server.auth.strategy('simple', 'basic', { validateFunc: validate });
    server.route({
        method: 'GET',
        path: '/',
        handler: function (request, reply) {
              reply('Success!');
        },
        config: { auth: 'simple' } });
});
server.start(function () {
    console.log('Server running at:', server.info.uri);
});

@nlf nlf closed this as completed in 911f0e0 Dec 12, 2014
@nlf
Copy link
Contributor

nlf commented Dec 12, 2014

Updated the tutorial again, this time fixed for real :)

@hickeyadamc
Copy link
Author

Thanks! :)

@ghost
Copy link

ghost commented Jan 16, 2018

server.register(require('inert'), (err) => {

if (err) {         throw err;     } 

server.route({         
	method: 'GET',         
	path: '/mydemo',         
	handler: function (request, reply) 
	{             
		reply.file('./public/mydemo.html');         
	}     
}); 

server.start((err) => { 

    if (err) {             
    	throw err;         
    }         
    server.log('info', 'Server running at: ' + server.info.uri);     
}); 

});

I am getting an error while using the inert@4 plugin in hapi@17 in the above code. Can some one please help me fix this? Following is the error I am able to see on cmd.
UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): AssertionError [ERR_ASSERTION]: Invalid register options "value" must be an object
(node:14584) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

@biswarupchakravarty
Copy link

@Npednekar9

Try switching to an async/await structure. This is what works for me:

async function startServer() {
  await server.register(require('inert'));
  server.route({
    method: 'GET',
    path: '/mydemo',
    handler: function(request, reply) {
      reply.file('./public/mydemo.html');
    }
  });

  await server.start();
});
startServer();

Check this issue for further details.
TLDR; v17 moved to an async/await approach, and away from callbacks, and the documentation is in process of being updated. Alternatively, you could downgrade to v16.

@outmoded outmoded locked as resolved and limited conversation to collaborators Feb 1, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants