Not able to redirect to login in the below code always coming {"statusCode":404,"error":"Not Found","message":"Not Found"}

Hi all below mentioned is my code in the example.js I am using template-kibana-plugin version 7.0.1 and kibana version 6.2.1 also in below code i have installed dependency hapi-auth-cookie version 3.1.0, now each time I am hitting localhost:5601 in browser it is redirecting me to localhost:5601/login which is fine but then my login html is not displayed instead it always says
{"statusCode":404,"error":"Not Found","message":"Not Found"}

not sure what I am missing here I have tried all I can but no luck, If somebody can please help me in below code.

export default function (server) {

const users = {};
(process.env.LOCAL_AUTH_LOGINS || 'admin:admin').split(',').forEach(function (kv) {
var toks = kv.split(':');
users[toks[0]] = toks[1];
});

const logout = function (request, reply) {
request.auth.session.clear();
return reply.redirect('/');
};

server.register(require('hapi-auth-cookie'), (err) => {

if (err) {
  throw err;
}

const cache = server.cache({ segment: 'sessions', expiresIn: 3 * 24 * 60 * 60 * 1000 });
server.app.cache = cache;

server.auth.strategy('session', 'cookie', true, {
  password: 'secret' + "RJMIgyv5P8gxiylnd7z5vrHj3a91ILBe",
  cookie: 'sid',
  redirectTo: '/login',
  isSecure: false,
  validateFunc: function (request, session, callback) {

    cache.get(session.sid, (err, cached) => {

      if (err) {
        return callback(err, false);
      }

      if (!cached) {
        return callback(null, false);
      }

      return callback(null, true, cached.username);
    });
  }
});

server.route([
  {
    method: ['GET', 'POST'],
    path: '/login',
    config: {
      handler: function(request, reply){
        if (request.auth.isAuthenticated) {
          return reply.redirect('/');
        }
    
        var message;
        var username;
        var password;
    
        if (request.method === 'post') {
          username = request.payload.username;
          password = request.payload.password;
        } else if (request.method === 'get') {
          username = request.query.username;
          password = request.query.password;
        }
        var checked = username && users[username] === password;
        if (username || password) {
          if (!checked) {
            message = 'Invalid username or password';
          }
        } else if (request.method === 'post') {
          message = 'Missing username or password';
        }
        if (!checked) {
          return reply('<!DOCTYPE html><html><head><title>Login Required</title>'
              + '<link rel="stylesheet" href="/bundles/commons.style.css">'
              + '<link rel="stylesheet" href="/bundles/kibana.style.css">'
              + '</head><body>'
              + '<center><div class="container" style="width: 20%;margin-left: auto;margin-right:auto;margin-top: 10%;">'
              + '<h1><img width="60%" ng-src="/plugins/kibana/settings/sections/about/barcode.svg" src="/plugins/kibana/settings/sections/about/barcode.svg"></h1>'
              + (message ? '<h3>' + message + '</h3><br/>' : '')
              + '<form id="login-form" class="ng-valid ng-dirty ng-valid-parse" method="get" action="/login">'
              + '<div class="form-group inner-addon left-addon">'
              + '  <input type="text" style="margin-bottom:8px;font-size: 1.25em;height: auto;" name="username" placeholder="Username" class="form-control ng-valid ng-touched ng-dirty">'
              + '  <input type="password" style="font-size: 1.25em;height: auto;" name="password" placeholder="Password" class="form-control ng-valid ng-touched ng-dirty">'
              + '</div><div style="width:200px;margin-left:auto;margin-right:auto;">'
              + '<input type="submit" value="Login" class="btn btn-default login" style="width: 80%;font-size: 1.5em;">' 
              + '</div></form></div></center></body></html>');
        }
    
        var uuid = 1;
        const sid = String(++uuid);
        request.server.app.cache.set(sid, { username: username }, 0, (err) => {
    
          if (err) {
            reply(err);
          }
    
          request.auth.session.set({ sid: sid });
          return reply.redirect('/');
        });
      },
      auth: { mode: 'try' },
      plugins: { 'hapi-auth-cookie': { redirectTo: false } }
    }
  },
  { method: 'GET', path: '/logout', config: { handler: logout } }
]);

});

}

Many thanks in advance.

What is the URL which is giving you the 404? Can you preserve the network log to trace this?

Hi Tyler,

the URL which gives me the error is "localhost:5601/login" in which I am redirecting, the redirection is fine I can see this URL in my browser after redirection and also in the above code as you can see I have given the handler function for '/login' then not sure why it is not working.

I also tried wrapping the login function to a const login variable and then calling the same but that too failed

below is the second version code.

module.exports = function (server) {
const users = {};
(process.env.LOCAL_AUTH_LOGINS || 'admin:admin').split(',').forEach(function (kv) {
var toks = kv.split(':');
users[toks[0]] = toks[1];
});

const login = function (request, reply) {

if (request.auth.isAuthenticated) {
  return reply.redirect('/');
}

var message;
var username;
var password;

if (request.method === 'post') {
  username = request.payload.username;
  password = request.payload.password;
} else if (request.method === 'get') {
  username = request.query.username;
  password = request.query.password;
}
var checked = username && users[username] === password;
if (username || password) {
  if (!checked) {
    message = 'Invalid username or password';
  }
} else if (request.method === 'post') {
  message = 'Missing username or password';
}
if (!checked) {
  return reply('<!DOCTYPE html><html><head><title>Login Required</title>'
      + '<link rel="stylesheet" href="/bundles/commons.style.css">'
      + '<link rel="stylesheet" href="/bundles/kibana.style.css">'
      + '</head><body>'
      + '<center><div class="container" style="width: 20%;margin-left: auto;margin-right:auto;margin-top: 10%;">'
      + '<h1><img width="60%" ng-src="/plugins/kibana/settings/sections/about/barcode.svg" src="/plugins/kibana/settings/sections/about/barcode.svg"></h1>'
      + (message ? '<h3>' + message + '</h3><br/>' : '')
      + '<form id="login-form" class="ng-valid ng-dirty ng-valid-parse" method="get" action="/login">'
      + '<div class="form-group inner-addon left-addon">'
      + '  <input type="text" style="margin-bottom:8px;font-size: 1.25em;height: auto;" name="username" placeholder="Username" class="form-control ng-valid ng-touched ng-dirty">'
      + '  <input type="password" style="font-size: 1.25em;height: auto;" name="password" placeholder="Password" class="form-control ng-valid ng-touched ng-dirty">'
      + '</div><div style="width:200px;margin-left:auto;margin-right:auto;">'
      + '<input type="submit" value="Login" class="btn btn-default login" style="width: 80%;font-size: 1.5em;">' 
      + '</div></form></div></center></body></html>');
}

var uuid = 1;
const sid = String(++uuid);
request.server.app.cache.set(sid, { username: username }, 0, (err) => {

  if (err) {
    reply(err);
  }

  request.auth.session.set({ sid: sid });
  return reply.redirect('/');
});

};

const logout = function (request, reply) {
request.auth.session.clear();
return reply.redirect('/');
};

server.register(require('hapi-auth-cookie'), (err) => {

if (err) {
  throw err;
}

const cache = server.cache({ segment: 'sessions', expiresIn: 3 * 24 * 60 * 60 * 1000 });
server.app.cache = cache;

server.auth.strategy('session', 'cookie', true, {
  password: 'secret' + "RJMIgyv5P8gxiylnd7z5vrHj3a91ILBe",
  cookie: 'sid',
  redirectTo: '/login',
  isSecure: false,
  validateFunc: function (request, session, callback) {

    cache.get(session.sid, (err, cached) => {

      if (err) {
        return callback(err, false);
      }

      if (!cached) {
        return callback(null, false);
      }

      return callback(null, true, cached.username);
    });
  }
});

server.route([
  {
    method: ['GET', 'POST'],
    path: '/login',
    config: {
      handler: login,
      auth: { mode: 'try' },
      plugins: { 'hapi-auth-cookie': { redirectTo: false } }
    }
  },
  { method: 'GET', path: '/logout', config: { handler: logout } }
]);

});
};

not even simple reply() is working always giving this error {"statusCode":404,"error":"Not Found","message":"Not Found"} not sure if this is a version issue or anything else.

below are the versions i am using.
node version: 6.12.2
kibana version: 6.2
elastic version: 6.2.1
hapi-auth-cookie version: 3.1.0
template-kibana-plugin version 7.0.1
hapi version: 14.2.0

also network log trace screenshot is below mentioned, Please help, and please do let me know if anything else required from my side.

Hi Tyler,

After a lot of digging here, I finally found that the problem was of base path proxy.

Thanks a lot anyways for giving me you precious time.

Yeah, you can disable the base path in development with --no-base-path. Also, you can access the base path with server.config().get('server.basePath')

Many thanks Tyler for your support here.

can you please also tell me if there is a way to replace the 'kibana' word coming in URL with something else or to mask all the URL's

Thanks a ton in advance.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.