Created
January 11, 2013 13:59
-
-
Save tlaitinen/4510854 to your computer and use it in GitHub Desktop.
A simple jQuery module for authentication against Yesod.Auth.HashDB.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
define({ | |
session: function() { | |
return { | |
status: '', | |
user: '', | |
baseUrl: '', | |
login: function(baseUrl, username, password, doneCallback, failCallback) { | |
var ses = this; | |
ses.baseUrl = baseUrl; | |
$.ajax(baseUrl + '/auth/page/hashdb/login', { | |
type:'POST', | |
data: { | |
"username" : username, | |
"password" : password | |
} | |
}) | |
.done(function(data, textStatus, jqXHR) { | |
if (data.indexOf('Invalid username/password') != -1) { | |
ses.status = 'invalidlogin' | |
} else { | |
ses.status = 'authenticated'; | |
ses.user = username; | |
} | |
if (doneCallback) | |
doneCallback(ses.status); | |
}) | |
.fail(function(data) { | |
ses.status = 'failed'; | |
if (failCallback) | |
failCallback(); | |
}); | |
}, | |
logout: function(doneCallback, failCallback) { | |
var ses = this; | |
$.ajax(ses.baseUrl + '/auth/logout', { | |
type:'POST' | |
}) | |
.done(function(data, textStatus, jqXHR) { | |
ses.status = ''; | |
if (doneCallback) | |
doneCallback(); | |
}) | |
.fail(function(data) { | |
if (failCallback) | |
failCallback(); | |
}); | |
} | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment