feat: Select article list in sight
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { API_URL, decodeJWT } from "@shared";
|
||||
import { makeAutoObservable } from "mobx";
|
||||
import { makeAutoObservable, runInAction } from "mobx";
|
||||
import axios, { AxiosError } from "axios";
|
||||
|
||||
type LoginResponse = {
|
||||
@@ -14,6 +14,7 @@ type LoginResponse = {
|
||||
|
||||
class AuthStore {
|
||||
payload: LoginResponse | null = null;
|
||||
token: string | null = null;
|
||||
isLoading = false;
|
||||
error: string | null = null;
|
||||
|
||||
@@ -28,21 +29,13 @@ class AuthStore {
|
||||
|
||||
if (decoded) {
|
||||
this.payload = decoded;
|
||||
// Set the token in axios defaults for future requests
|
||||
if (storedToken) {
|
||||
axios.defaults.headers.common[
|
||||
"Authorization"
|
||||
] = `Bearer ${storedToken}`;
|
||||
}
|
||||
} else {
|
||||
// If token is invalid or missing, clear it
|
||||
this.logout();
|
||||
}
|
||||
}
|
||||
|
||||
private setAuthToken(token: string) {
|
||||
localStorage.setItem("token", token);
|
||||
axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
login = async (email: string, password: string) => {
|
||||
@@ -58,12 +51,13 @@ class AuthStore {
|
||||
}
|
||||
);
|
||||
|
||||
const { token } = response.data;
|
||||
const data = response.data;
|
||||
|
||||
// Update auth token and store state
|
||||
this.setAuthToken(token);
|
||||
this.payload = response.data;
|
||||
this.error = null;
|
||||
runInAction(() => {
|
||||
this.setAuthToken(data.token);
|
||||
this.payload = response.data;
|
||||
this.error = null;
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof AxiosError) {
|
||||
this.error =
|
||||
@@ -85,7 +79,7 @@ class AuthStore {
|
||||
};
|
||||
|
||||
get isAuthenticated() {
|
||||
return !!this.payload?.token;
|
||||
return this.payload?.token !== null;
|
||||
}
|
||||
|
||||
get user() {
|
||||
|
||||
Reference in New Issue
Block a user