Skip to content

Commit

Permalink
Add token refresh function to auth utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Campbell committed Nov 16, 2020
1 parent 170a87d commit 4a79cb4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/auth/index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { login } from "./utilities";
export { login, refresh } from "./utilities";
26 changes: 26 additions & 0 deletions src/auth/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,29 @@ export async function login(username, password){
return data.access_token || false;
}

/**
* Refresh current access token.
* @example
* refresh("csrf_refresh_token")
* @param {String} csrf_refresh_token The current CSRF validation string.
* @returns {Boolean | String} An access token on success, `false` otherwise.
*/
export async function refresh(csrf_refresh_token){
const refreshInit = {
method: "POST",
headers: {'X-CSRF-TOKEN': csrf_refresh_token},
};

let refreshResponse = await fetch("/tokens/refresh", refreshInit);
let data = await refreshResponse.json();

if (data === null){
return false;
}
if (!refreshResponse.ok){
console.error(`Refresh failed. Got code ${refreshResponse.status} (${refreshResponse.statusText})`);
return false;
}

return data.access_token || false;
}

0 comments on commit 4a79cb4

Please sign in to comment.