refactor(auth): move user setter into function

pull/2749/head
Chaim Lev Ari 2019-01-02 20:01:23 +02:00
parent 81e3ace232
commit 9bebe9dee7
1 changed files with 17 additions and 20 deletions

View File

@ -18,32 +18,21 @@ function AuthenticationFactory(Auth, OAuth, jwtHelper, LocalStorage, StateManage
var jwt = LocalStorage.getJWT(); var jwt = LocalStorage.getJWT();
if (jwt) { if (jwt) {
var tokenPayload = jwtHelper.decodeToken(jwt); setUser(jwt);
user.username = tokenPayload.username;
user.ID = tokenPayload.id;
user.role = tokenPayload.role;
} }
} }
function oAuthLogin(code) { function oAuthLogin(code) {
return OAuth.login({code: code}).$promise return OAuth.login({ code: code }).$promise
.then(function success(data) { .then(function onLoginSuccess(response) {
LocalStorage.storeJWT(data.jwt); return setUser(response.jwt);
var tokenPayload = jwtHelper.decodeToken(data.jwt);
user.username = tokenPayload.username;
user.ID = tokenPayload.id;
user.role = tokenPayload.role;
}); });
} }
function login(username, password) { function login(username, password) {
return Auth.login({username: username, password: password}).$promise return Auth.login({ username: username, password: password }).$promise
.then(function success(data) { .then(function onLoginSuccess(response) {
LocalStorage.storeJWT(data.jwt); return setUser(response.jwt);
var tokenPayload = jwtHelper.decodeToken(data.jwt);
user.username = username;
user.ID = tokenPayload.id;
user.role = tokenPayload.role;
}); });
} }
@ -62,5 +51,13 @@ function AuthenticationFactory(Auth, OAuth, jwtHelper, LocalStorage, StateManage
return user; return user;
} }
function setUser(jwt) {
LocalStorage.storeJWT(jwt);
var tokenPayload = jwtHelper.decodeToken(jwt);
user.username = tokenPayload.username;
user.ID = tokenPayload.id;
user.role = tokenPayload.role;
}
return service; return service;
}]); }]);