2014-08-31 23:22:56 +02:00
|
|
|
var App = App || {};
|
|
|
|
|
2014-09-04 18:06:25 +02:00
|
|
|
App.Auth = function(jQuery, util, api, appState, promise) {
|
2014-08-31 23:22:56 +02:00
|
|
|
|
|
|
|
function loginFromCredentials(userName, password, remember) {
|
2014-09-04 18:06:25 +02:00
|
|
|
return promise.make(function(resolve, reject) {
|
|
|
|
promise.wait(api.post('/login', {userName: userName, password: password}))
|
2014-08-31 23:22:56 +02:00
|
|
|
.then(function(response) {
|
2014-09-01 08:07:51 +02:00
|
|
|
updateAppState(response);
|
2014-08-31 23:22:56 +02:00
|
|
|
jQuery.cookie(
|
|
|
|
'auth',
|
|
|
|
response.json.token.name,
|
|
|
|
remember ? { expires: 365 } : {});
|
|
|
|
resolve(response);
|
2014-09-04 18:06:25 +02:00
|
|
|
}).fail(function(response) {
|
2014-08-31 23:22:56 +02:00
|
|
|
reject(response);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
function loginFromToken(token) {
|
2014-09-04 18:06:25 +02:00
|
|
|
return promise.make(function(resolve, reject) {
|
|
|
|
promise.wait(api.post('/login', {token: token}))
|
2014-08-31 23:22:56 +02:00
|
|
|
.then(function(response) {
|
2014-09-01 08:07:51 +02:00
|
|
|
updateAppState(response);
|
2014-08-31 23:22:56 +02:00
|
|
|
resolve(response);
|
2014-09-04 18:06:25 +02:00
|
|
|
}).fail(function(response) {
|
2014-08-31 23:22:56 +02:00
|
|
|
reject(response);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
function loginAnonymous() {
|
2014-09-04 18:06:25 +02:00
|
|
|
return promise.make(function(resolve, reject) {
|
|
|
|
promise.wait(api.post('/login'))
|
2014-08-31 23:22:56 +02:00
|
|
|
.then(function(response) {
|
2014-09-01 08:07:51 +02:00
|
|
|
updateAppState(response);
|
2014-08-31 23:22:56 +02:00
|
|
|
resolve(response);
|
2014-09-04 18:06:25 +02:00
|
|
|
}).fail(function(response) {
|
2014-08-31 23:22:56 +02:00
|
|
|
reject(response);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
function logout() {
|
2014-09-04 18:06:25 +02:00
|
|
|
return promise.make(function(resolve, reject) {
|
2014-08-31 23:22:56 +02:00
|
|
|
appState.set('loggedIn', false);
|
|
|
|
appState.set('loginToken', null);
|
|
|
|
jQuery.removeCookie('auth');
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
function tryLoginFromCookie() {
|
2014-09-04 18:06:25 +02:00
|
|
|
return promise.make(function(resolve, reject) {
|
2014-08-31 23:22:56 +02:00
|
|
|
if (appState.get('loggedIn')) {
|
|
|
|
resolve();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var authCookie = jQuery.cookie('auth');
|
|
|
|
if (!authCookie) {
|
|
|
|
reject();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-04 18:06:25 +02:00
|
|
|
promise.wait(loginFromToken(authCookie))
|
|
|
|
.then(function(response) {
|
|
|
|
resolve();
|
|
|
|
}).fail(function(response) {
|
|
|
|
jQuery.removeCookie('auth');
|
|
|
|
reject();
|
|
|
|
});
|
2014-08-31 23:22:56 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-09-01 08:07:51 +02:00
|
|
|
function updateAppState(response) {
|
|
|
|
appState.set('loginToken', response.json.token && response.json.token.name);
|
|
|
|
appState.set('loggedInUser', response.json.user);
|
2014-09-03 09:10:26 +02:00
|
|
|
appState.set('loggedIn', response.json.user && !!response.json.user.id);
|
2014-09-01 08:07:51 +02:00
|
|
|
}
|
|
|
|
|
2014-08-31 23:22:56 +02:00
|
|
|
return {
|
|
|
|
loginFromCredentials: loginFromCredentials,
|
|
|
|
loginFromToken: loginFromToken,
|
|
|
|
loginAnonymous: loginAnonymous,
|
|
|
|
tryLoginFromCookie: tryLoginFromCookie,
|
|
|
|
logout: logout,
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
App.DI.registerSingleton('auth', App.Auth);
|