-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create base auth utilities -- only login
- Loading branch information
Justin Campbell
committed
Nov 13, 2020
1 parent
4e40b02
commit ca130f8
Showing
2 changed files
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { login } from "./utilities"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|