You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
1.7 KiB
71 lines
1.7 KiB
import axios from "./api-client"; |
|
|
|
const basepath = process.env.API_BASE_PATH; |
|
|
|
export interface User { |
|
id: String; |
|
username: String; |
|
email: String; |
|
} |
|
|
|
export default { |
|
async getUser(): Promise<User> { |
|
console.debug("Getting user"); |
|
const response = await axios.get(basepath + "/auth/profile", { |
|
headers: { Authorization: "Bearer " + document.cookie }, |
|
}); |
|
const data = response.data; |
|
|
|
return { |
|
id: data._id, |
|
username: data.username, |
|
email: data.email, |
|
}; |
|
}, |
|
|
|
async getUserByName(username: String): Promise<User> { |
|
const response = await axios.get(basepath + "/users/" + username, { |
|
headers: { Authorization: "Bearer " + document.cookie }, |
|
}); |
|
const data = response.data; |
|
|
|
return { |
|
id: data.id, |
|
username: data.username, |
|
email: data.email, |
|
}; |
|
|
|
}, |
|
|
|
async login(email: String, password: String): Promise<String> { |
|
const response = await axios.post(basepath + "/auth/login", { |
|
email, |
|
password, |
|
}); |
|
const token = response.data.token; |
|
document.cookie = "token=" + token; |
|
return token; |
|
}, |
|
|
|
async register( |
|
email: string, |
|
password: string, |
|
username: string |
|
): Promise<User> { |
|
const response = await axios.post(basepath + "/auth/register", { |
|
email, |
|
password, |
|
username: username, |
|
}); |
|
|
|
const user = response.data; |
|
return { id: user.id, username: user.username, email: user.email }; |
|
}, |
|
getToken(): string | undefined { |
|
const cookie = document.cookie |
|
.split(";") |
|
.filter((cookie) => cookie.startsWith("token="))[0]; |
|
const token = cookie ? cookie.replace("token=", "") : undefined; |
|
return token; |
|
}, |
|
};
|
|
|