mirror of https://github.com/portainer/portainer
refactor(auth): move user setter into function
parent
81e3ace232
commit
9bebe9dee7
|
@ -18,33 +18,22 @@ 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;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function logout() {
|
function logout() {
|
||||||
|
@ -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;
|
||||||
}]);
|
}]);
|
||||||
|
|
Loading…
Reference in New Issue