86 lines
2.4 KiB
JavaScript
86 lines
2.4 KiB
JavaScript
const m = require('mithril')
|
|
const Authentication = require('./authentication')
|
|
const api = require('./api')
|
|
const Input = require('./input')
|
|
|
|
const Login = {
|
|
oninit: function(vnode) {
|
|
this.redirect = vnode.attrs.redirect || ''
|
|
Authentication.requiresNotLogin()
|
|
|
|
this.error = ''
|
|
this.form = {
|
|
email: '',
|
|
password: '',
|
|
}
|
|
},
|
|
|
|
loginuser: function(vnode, e) {
|
|
e.preventDefault()
|
|
this.error = ''
|
|
|
|
if (!this.form.password) this.error = 'Password is missing'
|
|
if (!this.form.email) this.error = 'Email is missing'
|
|
|
|
if (this.error) return false
|
|
|
|
api.sendRequest({
|
|
method: 'POST',
|
|
url: '/api/authentication/login',
|
|
body: this.form,
|
|
})
|
|
.then((result) => {
|
|
if (!result.token) return Promise.reject(new Error('Server authentication down.'))
|
|
Authentication.updateToken(result.token)
|
|
m.route.set(this.redirect || '/browse')
|
|
})
|
|
.catch((error) => {
|
|
this.error = 'Error while logging in: ' + error.message
|
|
this.form.password = ''
|
|
})
|
|
|
|
return false
|
|
},
|
|
|
|
view: function(vnode) {
|
|
return [
|
|
m('div.page.page-login', [
|
|
m('div.modal', [
|
|
m('form', {
|
|
onsubmit: this.loginuser.bind(this, vnode),
|
|
}, [
|
|
m('h3', 'Filadelfia archival center'),
|
|
this.error ? m('p.error', this.error) : null,
|
|
m(Input, {
|
|
label: 'Email or name',
|
|
form: this.form,
|
|
formKey: 'email',
|
|
}),
|
|
m(Input, {
|
|
label: 'Password',
|
|
type: 'password',
|
|
form: this.form,
|
|
formKey: 'password',
|
|
}),
|
|
m('input.spinner', {
|
|
hidden: api.loading,
|
|
type: 'submit',
|
|
value: 'Log in',
|
|
}),
|
|
api.loading ? m('div.loading-spinner') : null,
|
|
]),
|
|
m('div.login--asuna.spritesheet'),
|
|
]),
|
|
m('footer', [
|
|
'Photo by ',
|
|
m('a', { href: 'https://unsplash.com/@franhotchin?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash', target: '_blank' }, 'Francesca Hotchin'),
|
|
' on ',
|
|
m('a', { href: 'https://unsplash.com/photos/landscape-photo-of-mountain-covered-with-snow-FN-cedy6NHA?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash', target: '_blank' }, 'Unsplash'),
|
|
]),
|
|
]),
|
|
]
|
|
},
|
|
}
|
|
|
|
module.exports = Login
|