2018-11-29 13:47:49 +00:00
|
|
|
# libs
|
|
|
|
window.$ = require('jquery')
|
|
|
|
|
2018-12-16 21:51:08 +00:00
|
|
|
# import bootstrap
|
|
|
|
require './style/custom.scss'
|
|
|
|
require 'bootstrap/js/dist/alert'
|
|
|
|
|
|
|
|
window.showAlert = (level, content) ->
|
|
|
|
($ '.alert').alert 'close'
|
|
|
|
|
|
|
|
($ '#alert-area').append ($ '<div>',
|
|
|
|
class: "alert alert-#{level} alert-dismissible fade show"
|
|
|
|
role: 'alert'
|
|
|
|
.append content, ($ '<button>',
|
|
|
|
type: 'button'
|
|
|
|
class: 'close'
|
|
|
|
'data-dismiss': 'alert',
|
|
|
|
'aria-label': 'Close'
|
|
|
|
.append ($ '<span>',
|
|
|
|
'aria-hidden': 'true'
|
|
|
|
.append '×'
|
|
|
|
)))
|
|
|
|
|
2018-11-29 13:47:49 +00:00
|
|
|
# on load
|
|
|
|
$ ->
|
2018-12-16 01:38:43 +00:00
|
|
|
# reset forms
|
|
|
|
$('form').each ->
|
|
|
|
@fullReset = ->
|
2018-12-16 21:51:08 +00:00
|
|
|
$('input', @).each ->
|
|
|
|
$(@).removeClass 'is-invalid'
|
|
|
|
$(@).removeClass 'is-valid'
|
|
|
|
@reset()
|
2018-12-13 16:07:58 +00:00
|
|
|
|
|
|
|
# login form
|
2018-12-16 02:19:27 +00:00
|
|
|
$('form[name="login"]').submit (event) ->
|
2018-12-16 21:51:08 +00:00
|
|
|
that = @
|
2018-12-16 01:38:43 +00:00
|
|
|
# inhibit normal form submission
|
|
|
|
event.preventDefault()
|
|
|
|
|
2018-12-13 16:07:58 +00:00
|
|
|
# gather form data
|
2018-12-16 01:38:43 +00:00
|
|
|
login = $('input[name="login"]', @)
|
2018-12-13 16:07:58 +00:00
|
|
|
password = $('input[name="password"]', @)
|
|
|
|
|
|
|
|
# transmit form data
|
2018-12-16 01:40:01 +00:00
|
|
|
$.post '/login',
|
|
|
|
login: login.val()
|
|
|
|
password: password.val()
|
|
|
|
.done (data) ->
|
2018-12-16 21:51:08 +00:00
|
|
|
if data.status == 'ok'
|
|
|
|
that.fullReset()
|
|
|
|
showAlert 'success', "successfully logged in '#{data.login}'"
|
2018-12-13 16:07:58 +00:00
|
|
|
|
2018-12-16 21:51:08 +00:00
|
|
|
else
|
|
|
|
switch data.text
|
|
|
|
when 'login'
|
|
|
|
showAlert 'warning', 'Invalid username and/or password.'
|
|
|
|
login.addClass 'is-invalid'
|
|
|
|
password.addClass 'is-invalid'
|
|
|
|
when 'db' or 'hash'
|
|
|
|
showAlert 'danger', 'Internal failure, try again later.'
|
|
|
|
else
|
|
|
|
showAlert 'danger', 'Unknown failure. Can you reproduce this?'
|
2018-12-13 16:07:58 +00:00
|
|
|
|
2018-12-16 02:19:27 +00:00
|
|
|
# registration form
|
2018-12-16 01:38:43 +00:00
|
|
|
$('form[name="register"]').submit (event) ->
|
2018-12-16 21:51:08 +00:00
|
|
|
that = @
|
2018-12-16 01:38:43 +00:00
|
|
|
# inhibit normal form submission
|
|
|
|
event.preventDefault()
|
|
|
|
|
2018-12-13 16:07:58 +00:00
|
|
|
# gather form data
|
2018-12-16 01:38:43 +00:00
|
|
|
login = $('input[name="login"]', @)
|
2018-12-13 16:07:58 +00:00
|
|
|
password = $('input[name="password"]', @)
|
|
|
|
confirm = $('input[name="confirm"]', @)
|
|
|
|
|
|
|
|
# check form data
|
2018-12-16 21:51:08 +00:00
|
|
|
if password.val() != confirm.val()
|
|
|
|
confirm.addClass 'is-invalid'
|
|
|
|
confirm.focus()
|
2018-12-13 16:07:58 +00:00
|
|
|
|
2018-12-16 21:51:08 +00:00
|
|
|
else
|
2018-12-13 16:07:58 +00:00
|
|
|
# transmit form data
|
2018-12-16 01:40:01 +00:00
|
|
|
$.post '/register',
|
|
|
|
login: login.val()
|
|
|
|
password: password.val()
|
|
|
|
.done (data) ->
|
2018-12-16 21:51:08 +00:00
|
|
|
if data.status == 'ok'
|
|
|
|
that.fullReset()
|
|
|
|
showAlert 'success', "successfully registered '#{data.login}'"
|
2018-12-13 16:07:58 +00:00
|
|
|
|
2018-12-16 21:51:08 +00:00
|
|
|
else
|
|
|
|
switch data.text
|
|
|
|
when 'invalid'
|
|
|
|
showAlert 'warning', 'Invalid user input. Please provide username AND password.'
|
|
|
|
login.addClass 'is-invalid'
|
|
|
|
password.addClass 'is-invalid'
|
|
|
|
login.focus()
|
|
|
|
when 'hash'
|
|
|
|
showAlert 'danger', 'Internal failure, try again later.'
|
|
|
|
when 'db'
|
|
|
|
showAlert 'danger', 'Internal failure or user name already taken.'
|
|
|
|
login.addClass 'is-invalid'
|
|
|
|
login.focus()
|
|
|
|
else
|
|
|
|
showAlert 'danger', 'Unknown failure. Can you reproduce this?'
|