Skip to content

Commit

Permalink
DISPLAY-993: Refresh token when activated code to refresh tenants
Browse files Browse the repository at this point in the history
  • Loading branch information
tuj committed Sep 22, 2023
1 parent 8c6f286 commit ad8e7ea
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
35 changes: 33 additions & 2 deletions src/components/user/login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ function Login() {

// Set data in local storage, to persist login on refresh
localStorage.setItem(localStorageKeys.API_TOKEN, data.token);
localStorage.setItem(localStorageKeys.API_REFRESH_TOKEN, data.refresh_token);
localStorage.setItem(localStorageKeys.USER_NAME, user?.fullname);
localStorage.setItem(localStorageKeys.EMAIL, user?.email);
localStorage.setItem(localStorageKeys.TENANTS, JSON.stringify(tenants));
Expand Down Expand Up @@ -109,6 +110,36 @@ function Login() {
);
};

const refreshTokenAndLogin = () => {
ConfigLoader.loadConfig().then((config) => {
fetch(`${config.api}v1/authentication/token/refresh`, {
mode: "cors",
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
credentials: "include",
body: JSON.stringify({
refresh_token: localStorage.getItem(localStorageKeys.API_REFRESH_TOKEN),
})
})
.then((resp) => resp.json())
.then((data) => {
if (data.code !== 200) {
setErrorMessage(data.message);
}

if (data?.token) {
login(data);
}
})
.catch((err) => {
setError(true);
displayError(t("error-refreshing-code"), err);
});
});
}

const onActivationCodeSubmit = (e) => {
e.preventDefault();
e.stopPropagation();
Expand All @@ -120,8 +151,8 @@ function Login() {
}),
})
)
.then(() => {
window.href = "/admin";
.then((resp) => {
refreshTokenAndLogin();
})
.catch((err) => {
setError(true);
Expand Down
1 change: 1 addition & 0 deletions src/components/util/local-storage-keys.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const localStorageKeys = {
API_TOKEN: "api-token",
API_REFRESH_TOKEN: "api-refresh-token",
EMAIL: "email",
SELECTED_TENANT: "selected-tenant",
TENANTS: "tenants",
Expand Down
3 changes: 2 additions & 1 deletion src/translations/da/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,8 @@
"login-with-external": " login",
"logged-in-select-tenant": "Vælg lokation.",
"activation-code": "Indtast aktiveringskode for at blive koblet på en instans.",
"error-activating-code": "Aktivering fejlede."
"error-activating-code": "Aktivering fejlede.",
"error-refreshing-code": "Aktivering fejlede. Prøv igen."
},
"file-dropzone": {
"drag-and-drop-text": "Vælg filer ved at trykke på eller trække filer ind i firkanten",
Expand Down

0 comments on commit ad8e7ea

Please sign in to comment.