Skip to content

Commit

Permalink
Create base auth utilities -- only login
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Campbell committed Nov 13, 2020
1 parent 4e40b02 commit ca130f8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/auth/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { login } from "./utilities";
33 changes: 33 additions & 0 deletions src/auth/utilities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/** Utility Functions for webqueue2 API */



/**
* Returns an access token to be used for authorization.
* @example
* login("janeDoe", "superSecretPassword")
* @param {String} username
* @param {String} password
* @returns {Boolean | String} An access token on success, `false` otherwise.
*/
export async function login(username, password){
const loginInit = {
method: "POST",
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({ "username": username, "password": password})
};

let loginResponse = await fetch("/login", loginInit);
let data = await loginResponse.json();

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

return data.access_token || false;
}

0 comments on commit ca130f8

Please sign in to comment.