feat(auth): add accessToken field to authStore and setup beforeRequest hook to add this token to headers
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import { useAuthStore } from "../../../model";
|
||||||
import { api as baseApi } from "shared/config";
|
import { api as baseApi } from "shared/config";
|
||||||
|
|
||||||
// Extend base API with authentication hooks
|
// Extend base API with authentication hooks
|
||||||
@@ -5,7 +6,12 @@ export const api = baseApi.extend({
|
|||||||
hooks: {
|
hooks: {
|
||||||
beforeRequest: [
|
beforeRequest: [
|
||||||
(request) => {
|
(request) => {
|
||||||
// Add authentication token to request headers
|
const token = useAuthStore.getState().accessToken;
|
||||||
|
|
||||||
|
if (token) {
|
||||||
|
request.headers.set("Authorization", `Bearer ${token}`);
|
||||||
|
}
|
||||||
|
|
||||||
return request;
|
return request;
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -1,13 +1,18 @@
|
|||||||
import { create } from "zustand";
|
import { create } from "zustand";
|
||||||
import type { AuthStore } from "../../types/store";
|
import type { AuthStore, AuthStoreState } from "../../types/store";
|
||||||
import { login, logout, register } from "../../../api";
|
import { login, logout, register } from "../../../api";
|
||||||
import { callApi } from "shared/utils";
|
import { callApi } from "shared/utils";
|
||||||
import { UNEXPECTED_ERROR_MESSAGE } from "shared/config";
|
import { UNEXPECTED_ERROR_MESSAGE } from "shared/config";
|
||||||
|
|
||||||
export const useAuthStore = create<AuthStore>()((set) => ({
|
const defaultStoreState: Readonly<AuthStoreState> = {
|
||||||
user: undefined,
|
user: undefined,
|
||||||
status: "idle",
|
status: "idle",
|
||||||
|
accessToken: undefined,
|
||||||
error: null,
|
error: null,
|
||||||
|
};
|
||||||
|
|
||||||
|
export const useAuthStore = create<AuthStore>()((set) => ({
|
||||||
|
...defaultStoreState,
|
||||||
|
|
||||||
login: async (loginData) => {
|
login: async (loginData) => {
|
||||||
set({ status: "loading" });
|
set({ status: "loading" });
|
||||||
@@ -22,6 +27,7 @@ export const useAuthStore = create<AuthStore>()((set) => ({
|
|||||||
set({
|
set({
|
||||||
status: "authenticated",
|
status: "authenticated",
|
||||||
user: responseData?.user,
|
user: responseData?.user,
|
||||||
|
accessToken: responseData?.accessToken,
|
||||||
error: null,
|
error: null,
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -46,6 +52,7 @@ export const useAuthStore = create<AuthStore>()((set) => ({
|
|||||||
set({
|
set({
|
||||||
status: "authenticated",
|
status: "authenticated",
|
||||||
user: responseData?.user,
|
user: responseData?.user,
|
||||||
|
accessToken: responseData?.accessToken,
|
||||||
error: null,
|
error: null,
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -11,6 +11,10 @@ export interface AuthStoreState {
|
|||||||
* Authentication status
|
* Authentication status
|
||||||
*/
|
*/
|
||||||
status: AuthStatus;
|
status: AuthStatus;
|
||||||
|
/**
|
||||||
|
* Authentication token
|
||||||
|
*/
|
||||||
|
accessToken?: string;
|
||||||
/**
|
/**
|
||||||
* Error data
|
* Error data
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user