diff options
Diffstat (limited to 'www/js/auth.js')
-rw-r--r-- | www/js/auth.js | 160 |
1 files changed, 75 insertions, 85 deletions
diff --git a/www/js/auth.js b/www/js/auth.js index d01b70d..691a19e 100644 --- a/www/js/auth.js +++ b/www/js/auth.js @@ -24,94 +24,84 @@ var Rdxport = Rdxport || {}; -var auth_username = null; -var auth_fullname = null; -var auth_token = null; - -function auth_loginSuccess(data) { - if (data.status == 'OK') { - auth_username = data.username; - auth_fullname = data.fullname; - auth_token = data.token; - - sessionStorage.setItem("auth_username", auth_username); - sessionStorage.setItem("auth_fullname", auth_fullname); - sessionStorage.setItem("auth_token", auth_token); - - // todo: do this at a central place - rdxport = new Rdxport.Rdxport(auth_username, auth_token, '/rd-bin/rdxport.cgi'); - rdxport.setListDropboxesEndpoint('/rh-bin/listdropboxes.cgi'); - rdxport.setMusicgridEndpoint('/rh-bin/musicgrid.cgi'); - - apps_select(apps_current); - - $('#username-field').html(auth_fullname + ' (' + auth_username + ')'); - $('#loginbox').slideUp(); - $('#mainwindow').fadeIn(); - } else { - alertbox.error('loginbox', "Fehler beim Login", data.errorstring); - auth_cleanup(); - } -} - -function auth_loginError(req, status, error) { - var message = req.status + ': ' + error; - if(req.status == 401) { - message = "Benutzer und/oder Passwort sind falsch!"; - } - alertbox.error('loginbox', "Fehler beim Login", message); - $("#password").val(''); -} - -function auth_logout() { - if (importer && importer.isUploading()) { - alert('Achtung: Es laufen noch imports.'); - return; - } - - auth_cleanup(); - apps_cleanup(); +Rdxport.Auth = function() { + this.username = sessionStorage.getItem('auth_username'); + this.fullname = sessionStorage.getItem('auth_fullname'); + this.token = sessionStorage.getItem('auth_token'); +}; + +Rdxport.Auth.prototype.isLoggedIn = function() { + return this.username && this.fullname && this.token; +}; + +Rdxport.Auth.prototype.set = function(username, fullname, token) { + this.username = username; + this.fullname = fullname; + this.token = token; + + sessionStorage.setItem('auth_username', this.username); + sessionStorage.setItem('auth_fullname', this.fullname); + sessionStorage.setItem('auth_token', this.token); +}; + +Rdxport.Auth.prototype.cleanup = function() { + sessionStorage.removeItem('auth_username'); + sessionStorage.removeItem('auth_fullname'); + sessionStorage.removeItem('auth_token'); + + this.username = null; + this.fullname = null; + this.token = null; +}; + +Rdxport.AuthView = function(model) { + this.model = model; +}; + +Rdxport.AuthView.prototype.renderLoggedIn = function() { + $('#loginbox').slideUp(); + $('#mainwindow').fadeIn(); + $('#username-field').text(this.model.fullname + ' (' + this.model.username + ')'); + + $('button.logout').off().on('click', function() { + router.route('logout'); + }); +}; - $(".alert").alert('close'); - $("#username").val(''); - $("#password").val(''); - $("#mainwindow").fadeOut(); - $('#username-field').html(''); +Rdxport.AuthView.prototype.renderLoginForm = function() { + $('.alert').alert('close'); $('#loginbox').slideDown(); -} + $('#mainwindow').fadeOut(); + $('#username-field').empty(); -function auth_init() { - auth_username = sessionStorage.getItem("auth_username"); - auth_fullname = sessionStorage.getItem("auth_fullname"); - auth_token = sessionStorage.getItem("auth_token"); - - if(auth_token && auth_username && auth_fullname) { - $("#loginbox").hide(); - $('#username-field').html(auth_fullname + ' (' + auth_username + ')'); - } else { - $("#mainwindow").hide(); - } - $("#loginform").submit(function(event) { + var self = this; + $('#loginform').on('submit', function(event) { event.preventDefault(); - Rdxport.Rdxport.authLogin( - '/rh-bin/authtoken.json', - $("#username").val(), - $("#password").val(), - auth_loginSuccess - ).fail(auth_loginError); + '/rh-bin/authtoken.json', + $("#username").val(), + $("#password").val(), + function(data) { + if (data.status == 'OK') { + self.model.set( + data.username, + data.fullname, + data.token + ); + + router.route(); + } else { + alertbox.error('loginbox', "Fehler beim Login", data.errorstring); + self.model.cleanup(); + } + } + ).fail(function(req, status, error) { + var message = req.status + ': ' + error; + if(req.status == 401) { + message = "Benutzer und/oder Passwort sind falsch!"; + } + alertbox.error('loginbox', "Fehler beim Login", message); + $("#password").val(''); + }); }); -} - -function auth_cleanup() { - sessionStorage.removeItem("auth_username"); - sessionStorage.removeItem("auth_fullname"); - sessionStorage.removeItem("auth_token"); - - auth_username = null; - auth_fullname = null; - auth_token = null; - - $("#username").val('').focus(); - $("#password").val(''); -} +}; |