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

**URL:** https://discuss.elastic.co/t/not-able-to-redirect-to-login-in-the-below-code-always-coming-statuscode-404-error-not-found-message-not-found/121712
**Category:** Kibana
**Created:** [February 27, 2018, 5:54pm UTC](https://discuss.elastic.co/t/not-able-to-redirect-to-login-in-the-below-code-always-coming-statuscode-404-error-not-found-message-not-found/121712 "2018-02-27T17:54:27Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Sunny\_Sachan](https://avatars.discourse-cdn.com/v4/letter/s/f4b2a3/32.png) [@Sunny\_Sachan](https://discuss.elastic.co/u/Sunny_Sachan)
#### Post date: [February 27, 2018, 5:54pm UTC](https://discuss.elastic.co/t/not-able-to-redirect-to-login-in-the-below-code-always-coming-statuscode-404-error-not-found-message-not-found/121712/1 "2018-02-27T17:54:27Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![tylersmalley](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tylersmalley/32/8833_2.png) [@tylersmalley](https://discuss.elastic.co/u/tylersmalley)
#### Post date: [February 27, 2018, 11:16pm UTC](https://discuss.elastic.co/t/not-able-to-redirect-to-login-in-the-below-code-always-coming-statuscode-404-error-not-found-message-not-found/121712/2 "2018-02-27T23:16:45Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Sunny\_Sachan](https://avatars.discourse-cdn.com/v4/letter/s/f4b2a3/32.png) [@Sunny\_Sachan](https://discuss.elastic.co/u/Sunny_Sachan)
#### Post date: [February 28, 2018, 7:37am UTC](https://discuss.elastic.co/t/not-able-to-redirect-to-login-in-the-below-code-always-coming-statuscode-404-error-not-found-message-not-found/121712/3 "2018-02-28T07:37:27Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/5/9/5972987c748733da52dac1ec847a54c5f354d245.png)

---

<div class="post-metadata">

### Author: ![Sunny\_Sachan](https://avatars.discourse-cdn.com/v4/letter/s/f4b2a3/32.png) [@Sunny\_Sachan](https://discuss.elastic.co/u/Sunny_Sachan)
#### Post date: [March 1, 2018, 5:46pm UTC](https://discuss.elastic.co/t/not-able-to-redirect-to-login-in-the-below-code-always-coming-statuscode-404-error-not-found-message-not-found/121712/4 "2018-03-01T17:46:14Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![tylersmalley](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tylersmalley/32/8833_2.png) [@tylersmalley](https://discuss.elastic.co/u/tylersmalley)
#### Post date: [March 2, 2018, 7:10am UTC](https://discuss.elastic.co/t/not-able-to-redirect-to-login-in-the-below-code-always-coming-statuscode-404-error-not-found-message-not-found/121712/5 "2018-03-02T07:10:45Z")

</div>

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')`

---

<div class="post-metadata">

### Author: ![Sunny\_Sachan](https://avatars.discourse-cdn.com/v4/letter/s/f4b2a3/32.png) [@Sunny\_Sachan](https://discuss.elastic.co/u/Sunny_Sachan)
#### Post date: [March 2, 2018, 4:20pm UTC](https://discuss.elastic.co/t/not-able-to-redirect-to-login-in-the-below-code-always-coming-statuscode-404-error-not-found-message-not-found/121712/6 "2018-03-02T16:20:56Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [March 30, 2018, 4:21pm UTC](https://discuss.elastic.co/t/not-able-to-redirect-to-login-in-the-below-code-always-coming-statuscode-404-error-not-found-message-not-found/121712/7 "2018-03-30T16:21:13Z")

</div>

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