Add user information retrieval endpoint and update token management

This commit is contained in:
2025-05-18 10:29:08 +08:00
parent 1396f6939b
commit 1b5eb23a65
3 changed files with 48 additions and 1 deletions

View File

@@ -63,12 +63,16 @@ class Network {
if (!app.token) {
return;
}
const res = await this.getUserInfo(app.user!.username)
const res = await this.getMe()
if (!res.success && res.message.includes("Invalid token")) {
app.token = null;
app.user = null;
app.saveData();
window.location.reload();
} else {
app.user = res.data!;
app.token = res.data!.token;
app.saveData();
}
}
@@ -105,6 +109,19 @@ class Network {
}
}
async getMe(): Promise<Response<UserWithToken>> {
try {
const response = await axios.get(`${this.apiBaseUrl}/user/me`)
return response.data
} catch (e: any) {
console.error(e)
return {
success: false,
message: e.toString(),
}
}
}
async getUserInfo(username: string): Promise<Response<User>> {
try {
const response = await axios.get(`${this.apiBaseUrl}/user/info`, {