Skip to content

It's not possible to have a custom payload on failed authentication #96

@sazzer

Description

@sazzer

In some situations it would be very useful to have a custom payload returned when authentication fails. However, it seems that this isn't possible to achieve.

The obvious way of doing this based on the current documentation would be something like:

    percolator.route('/api/authentication/internal', {
        basicAuthenticate: function (username, password, req, res, cb) {
            if (username === 'username' && password === 'password') {
                cb(null, {username: username, password: password});
            } else {
                res.object({'hello': 'world'}).send();
                cb(true);
            }
        }, 
        POST : function (req, res) {
            res.object({auth: req.authenticated}).send();
        }
    });

However, if the basicAuthenticate function does a res.object().send() then you get an "Error: Can't set headers after they are sent" logged, and whilst the custom object is sent, the HTTP Status code is still set to 200. If the basicAuthenticate does a res.object() but doesn't do a send() then the custom object is never sent.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions