forked from mirrors/pronouns.cc
fix: don't call /users/@me without token
This commit is contained in:
parent
8b31519952
commit
afb241232b
1 changed files with 5 additions and 8 deletions
|
@ -1,5 +1,6 @@
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import { atom, useRecoilState, useRecoilValue } from "recoil";
|
import { atom, useRecoilState, useRecoilValue } from "recoil";
|
||||||
|
import fetchAPI from "./fetch";
|
||||||
import { APIError, ErrorCode, MeUser } from "./types";
|
import { APIError, ErrorCode, MeUser } from "./types";
|
||||||
|
|
||||||
export const userState = atom<MeUser>({
|
export const userState = atom<MeUser>({
|
||||||
|
@ -12,16 +13,12 @@ async function getCurrentUser() {
|
||||||
if (!token) return null;
|
if (!token) return null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const resp = await axios.get<MeUser | APIError>("/api/v1/users/@me");
|
return await fetchAPI<MeUser>("/users/@me");
|
||||||
if (resp.status === 200) {
|
} catch (e) {
|
||||||
return resp.data as MeUser;
|
if ((e as APIError).code === ErrorCode.Forbidden) {
|
||||||
}
|
|
||||||
|
|
||||||
// if we got a forbidden error, the token is invalid
|
|
||||||
if ((resp.data as APIError).code === ErrorCode.Forbidden) {
|
|
||||||
localStorage.removeItem("pronouns-token");
|
localStorage.removeItem("pronouns-token");
|
||||||
}
|
}
|
||||||
} catch (e) {
|
|
||||||
console.log("Error fetching /users/@me:", e);
|
console.log("Error fetching /users/@me:", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue