Skip to content

Instantly share code, notes, and snippets.

@mentix02
Last active December 2, 2020 01:17
Show Gist options
  • Save mentix02/bec9bc0624daabc7580ce8693ce53a2d to your computer and use it in GitHub Desktop.
Save mentix02/bec9bc0624daabc7580ce8693ce53a2d to your computer and use it in GitHub Desktop.
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