Last active
December 2, 2020 01:17
-
-
Save mentix02/bec9bc0624daabc7580ce8693ce53a2d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
interface TokenResponse { | |
token: string; | |
} | |
export interface Credentials { | |
username: string; | |
password: string; | |
} | |
const BASE_URL = "http://localhost:8000"; | |
export const getToken = async ({ username, password }: Credentials) => { | |
const body = new FormData(); | |
body.set("username", username); | |
body.set("password", password); | |
const res = await fetch(`${BASE_URL}/token/`, { body, method: "post" }); | |
if (res.status === 200) { | |
const data: TokenResponse = await res.json(); | |
return data.token; | |
} else throw new Error("Invalid credentials."); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment