get auth state from cookie upon render

This commit is contained in:
Danny van Kooten 2016-11-23 16:33:52 +01:00
parent 86cbfa8dd4
commit fbd591547c
2 changed files with 22 additions and 6 deletions

View File

@ -33,20 +33,34 @@ const Login = {
view(c) { view(c) {
return m('div.block', [ return m('div.block', [
m('h2', 'Login'), m('h2', 'Login'),
m('p', 'Please enter your credentials to login to your Ana dashboard.'),
m('form', { m('form', {
method: "POST", method: "POST",
onsubmit: c.onSubmit onsubmit: c.onSubmit
}, [ }, [
m('div.form-group', [ m('div.form-group', [
m('label', 'Email address'), m('label', 'Email address'),
m('input', { type: "email", onchange: m.withAttr("value", c.data.email ) }), m('input', {
type: "email",
name: "email",
required: true,
onchange: m.withAttr("value", c.data.email )
}),
]), ]),
m('div.form-group', [ m('div.form-group', [
m('label', 'Password'), m('label', 'Password'),
m('input', { type: "password", onchange: m.withAttr("value", c.data.password ) }), m('input', {
type: "password",
name: "password",
required: true,
onchange: m.withAttr("value", c.data.password )
}),
]), ]),
m('div.form-group', [ m('div.form-group', [
m('input', { type: "submit", value: "Sign in" }), m('input', {
type: "submit",
value: "Sign in"
}),
]), ]),
]) ])
]) ])

View File

@ -10,7 +10,7 @@ import LogoutButton from './components/logoutButton.js';
const App = { const App = {
controller(args) { controller(args) {
this.state = { this.state = {
authenticated: false authenticated: document.cookie.indexOf('auth') > -1
}; };
this.setState = function(nextState) { this.setState = function(nextState) {
@ -23,11 +23,13 @@ const App = {
}, },
view(c) { view(c) {
if( ! c.state.authenticated ) { if( ! c.state.authenticated ) {
return m.component(Login, { return m('div.container', [
m.component(Login, {
onAuth: () => { onAuth: () => {
c.setState({ authenticated: true }) c.setState({ authenticated: true })
} }
}); })
]);
} }
return [ return [