nfp_sites/app/login/login.js

149 lines
3.8 KiB
JavaScript
Raw Normal View History

2019-02-20 16:10:37 +00:00
const m = require('mithril')
const Authentication = require('../authentication')
2019-10-01 11:35:00 +00:00
const Api = require('../api/common')
2019-02-20 16:10:37 +00:00
const Login = {
loadedGoogle: false,
loading: false,
2019-02-22 14:53:43 +00:00
redirect: '',
2019-02-20 16:10:37 +00:00
error: '',
initGoogleButton: function() {
gapi.signin2.render('googlesignin', {
'scope': 'email',
'width': 240,
'height': 50,
'longtitle': true,
'theme': 'dark',
'onsuccess': Login.onGoogleSuccess,
'onfailure': Login.onGoogleFailure,
2019-09-13 13:33:10 +00:00
})
2019-02-20 16:10:37 +00:00
},
onGoogleSuccess: function(googleUser) {
Login.loading = true
m.redraw()
m.request({
method: 'POST',
url: '/api/login',
2019-09-13 13:33:10 +00:00
body: { token: googleUser.Zi.access_token },
2019-02-20 16:10:37 +00:00
})
.then(function(result) {
Authentication.updateToken(result.token)
2019-02-22 14:53:43 +00:00
m.route.set(Login.redirect || '/')
2019-02-20 16:10:37 +00:00
})
.catch(function(error) {
2019-09-13 13:33:10 +00:00
Login.error = 'Error while logging into NFP! ' + error.status + ': ' + error.message
let auth2 = gapi.auth2.getAuthInstance()
2019-02-20 16:10:37 +00:00
return auth2.signOut()
})
.then(function () {
Login.loading = false
m.redraw()
})
},
onGoogleFailure: function(error) {
2019-09-13 13:33:10 +00:00
if (error.error !== 'popup_closed_by_user' && error.error !== 'popup_blocked_by_browser') {
Login.error = 'Error while logging into Google: ' + error.error
m.redraw()
}
2019-02-20 16:10:37 +00:00
},
2019-02-22 14:53:43 +00:00
oninit: function(vnode) {
Login.redirect = vnode.attrs.redirect || ''
2019-02-20 16:10:37 +00:00
if (Authentication.currentUser) return m.route.set('/')
Login.error = ''
this.username = ''
this.password = ''
2019-02-20 16:10:37 +00:00
},
oncreate: function() {
if (Authentication.currentUser) return
Authentication.createGoogleScript()
.then(function() {
Login.initGoogleButton()
})
},
loginuser: function(vnode, e) {
e.preventDefault()
if (!this.username) {
Login.error = 'Email is missing'
} else if (!this.password) {
Login.error = 'Password is missing'
} else {
Login.error = ''
}
if (Login.error) return
Login.loading = true
2019-10-01 11:35:00 +00:00
Api.sendRequest({
method: 'POST',
url: '/api/login/user',
body: {
username: this.username,
password: this.password,
},
})
.then(function(result) {
Authentication.updateToken(result.token)
m.route.set(Login.redirect || '/')
})
.catch(function(error) {
Login.error = 'Error while logging into NFP! ' + error.message
vnode.state.password = ''
})
.then(function () {
Login.loading = false
m.redraw()
})
},
view: function(vnode) {
2019-02-20 16:10:37 +00:00
return [
m('div.login-wrapper', [
m('div.login-icon'),
2019-02-20 16:10:37 +00:00
m('article.login', [
m('header', [
m('h1', 'NFP.moe login'),
]),
m('div.content', [
m('h5', 'Please login to access restricted area'),
2019-02-20 16:10:37 +00:00
Login.error ? m('div.error', Login.error) : null,
Login.loading ? m('div.loading-spinner') : null,
m('form', {
hidden: Login.loading,
onsubmit: this.loginuser.bind(this, vnode),
}, [
m('label', 'Email'),
m('input', {
type: 'text',
value: this.username,
oninput: function(e) { vnode.state.username = e.currentTarget.value },
}),
m('label', 'Password'),
m('input', {
type: 'password',
value: this.password,
oninput: function(e) { vnode.state.password = e.currentTarget.value },
}),
m('input', {
type: 'submit',
value: 'Login',
}),
]),
m('h5', { hidden: Login.loading }, 'Alternative login'),
2019-02-20 16:10:37 +00:00
m('div#googlesignin', { hidden: Login.loading }, m('div.loading-spinner')),
2019-09-13 13:33:10 +00:00
]),
2019-02-20 16:10:37 +00:00
]),
]),
]
2019-09-13 13:33:10 +00:00
},
2019-02-20 16:10:37 +00:00
}
module.exports = Login