mirror of
https://github.com/coder/coder.git
synced 2026-10-24 01:26:25 +08:00
#37 implemented the Sign-_in_ flow, but there wasn't a Sign-_out_ flow as part of that PR (aside from letting the cookie expire... or manually deleting the cookie...), which is obviously not ideal. This PR implements a basic sign-out flow, along with a very simple user dropdown:  Bringing in a few pruned down components for the `<UserDropdown />` to integrate into the `<NavBar />`. In addition, this also implements a simple back-end API for `/logout` which just clears the session token.
37 lines
701 B
TypeScript
37 lines
701 B
TypeScript
interface LoginResponse {
|
|
session_token: string
|
|
}
|
|
|
|
export const login = async (email: string, password: string): Promise<LoginResponse> => {
|
|
const response = await fetch("/api/v2/login", {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({
|
|
email,
|
|
password,
|
|
}),
|
|
})
|
|
|
|
const body = await response.json()
|
|
if (!response.ok) {
|
|
throw new Error(body.message)
|
|
}
|
|
|
|
return body
|
|
}
|
|
|
|
export const logout = async (): Promise<void> => {
|
|
const response = await fetch("/api/v2/logout", {
|
|
method: "POST",
|
|
})
|
|
|
|
if (!response.ok) {
|
|
const body = await response.json()
|
|
throw new Error(body.message)
|
|
}
|
|
|
|
return
|
|
}
|