bodley bodley - 4 months ago 18
Jade Question

Koa.js validating password (jade) render and redirect

I am trying to customize one on the mod_auth_openidc libraries.

I have a problem with rendering in koa. When I write the same thing in express, this Was easy, because jade file looks the sam I attached below and I can use res.redner.

Now when I run my code i only get JSON output:

{"username":"test@gmail.com","password":"asdasd"}


My question is when I perform validating the password and than, if user enter wrong password I want to stay on current page, do not change the url and only add
p
tag with information as it is in jade file below.

Any ideas? I know
yield render('/views/login' ...
is wrong but whatever I tried to pass here (i.e only
/
) I got 500 or other error codes.

I have got jade file:

doctype html
html(lang='en')
head
meta(charset='UTF-8')
link(href='/stylesheets/style.css', rel='stylesheet')
title Login Page
body
.header
div Title
.body-container
div
form.ng-pristine.ng-valid(method='POST', action='/')
.input-label
.input-container
input#username(type='text', name='username')
.input-container
input#password(type='password', name='password')
.buttons
input(type='hidden', name='ok', value='')
input(type='hidden', name='cancel', value='')
button#ping
span.words Sign In
span.loading_spinner
i.fa.fa-circle-o-notch.fa-spin
if errors
if errors.password
p.
Please enter a valid password


and js (koa) file (shortcut):

const koa = require('koa');
const body = require('koa-body');
const serve = require('koa-static');
let send = require('koa-send');
const Router = require('koa-router');
const app = koa();
const router = new Router();

var render = require('koa-render-jade')(__dirname);

router.get('/', function *(req, res) {
this.body = yield render('/views/login.jade');
});

function validatePassword(password) {
if (password == "secret") {
return true;
} else return false;
}

app.use(serve(path.join(__dirname, 'public')));
app.use(serve('/views/login.jade'));

var bodyParser = require('koa-body-parser');
app.use(bodyParser());

router.post('/', function *(res, req) {
this.body = this.request.body;
if (validatePassword(this.body.password)) {
console.log("working");
this.response.redirect("www.google.com");
} else {
console.log("not working");
yield render('/views/login', {
title: 'Login',
queryParams: this.body.query,
agency: this.body.agency,
email: this.body.email,
errors: {
"password": true
}
});
}
});


@UPDATE - I managed with redirect, only "render part" still is a problem.

Answer

Jade file stay as it is.

koa js file:

var bodyParser = require('koa-body-parser');
app.use(bodyParser());

router.post('/', function *() {
    if (validatePassword(this.request.body.password)) {
        this.response.redirect("www.google.com");
    } else {
        this.body = yield render('/views/login', {
            title: 'Login',
            queryParams: this.request.query,
            agency: this.request.agency,
            email: this.request.email,
            errors: {
                "password": true
            }
        });
    }
});

works fine for me. Hope it will help someone.