refactor: create separate shared store for auth token and refresh action
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
export * from "./login";
|
export * from "./login";
|
||||||
export * from "./register";
|
export * from "./register";
|
||||||
export * from "./logout";
|
export * from "./logout";
|
||||||
export * from "./refresh";
|
export * from "../../../../shared/api/calls/refresh";
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { http, HttpResponse } from "msw";
|
import { http, HttpResponse } from "msw";
|
||||||
import type { AuthData } from "../../../model/types/service";
|
import type { AuthData } from "../../../model/types/service";
|
||||||
import { BASE_URL } from "shared/config";
|
import { BASE_URL } from "shared/api";
|
||||||
import { LOGIN_API_ROUTE } from "./login";
|
import { LOGIN_API_ROUTE } from "./login";
|
||||||
import { MOCK_AUTH_RESPONSE, MOCK_EMAIL, MOCK_PASSWORD } from "../mocks";
|
import { MOCK_AUTH_RESPONSE, MOCK_EMAIL, MOCK_PASSWORD } from "../mocks";
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,8 @@
|
|||||||
import { setupServer } from "msw/node";
|
import { setupServer } from "msw/node";
|
||||||
import { login } from "./login";
|
import { login } from "./login";
|
||||||
import { loginMock } from "./login.mock";
|
import { loginMock } from "./login.mock";
|
||||||
import {
|
import { MOCK_EMAIL, MOCK_EXISTING_USER, MOCK_PASSWORD } from "../mocks";
|
||||||
MOCK_EMAIL,
|
import { MOCK_TOKEN } from "shared/api";
|
||||||
MOCK_EXISTING_USER,
|
|
||||||
MOCK_PASSWORD,
|
|
||||||
MOCK_TOKEN,
|
|
||||||
} from "../mocks";
|
|
||||||
|
|
||||||
const server = setupServer(loginMock);
|
const server = setupServer(loginMock);
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { http, HttpResponse } from "msw";
|
import { http, HttpResponse } from "msw";
|
||||||
import { BASE_URL } from "shared/config";
|
import { BASE_URL } from "shared/api";
|
||||||
import { LOGOUT_API_ROUTE } from "./logout";
|
import { LOGOUT_API_ROUTE } from "./logout";
|
||||||
|
|
||||||
const LOGOUT_URL = `${BASE_URL}/${LOGOUT_API_ROUTE}`;
|
const LOGOUT_URL = `${BASE_URL}/${LOGOUT_API_ROUTE}`;
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
import type { User } from "entities/User";
|
import type { User } from "entities/User";
|
||||||
import type { AuthResponse } from "../../model";
|
import type { AuthResponse } from "../../model";
|
||||||
|
import { MOCK_TOKEN } from "shared/api";
|
||||||
|
|
||||||
export const MOCK_TOKEN = "mock.access.token";
|
|
||||||
export const MOCK_FRESH_TOKEN = "mock.fresh.access.token";
|
|
||||||
export const MOCK_EMAIL = "test@test.com";
|
export const MOCK_EMAIL = "test@test.com";
|
||||||
export const MOCK_NEW_EMAIL = "new@test.com";
|
export const MOCK_NEW_EMAIL = "new@test.com";
|
||||||
export const MOCK_PASSWORD = "password";
|
export const MOCK_PASSWORD = "password";
|
||||||
|
|||||||
@@ -1,23 +0,0 @@
|
|||||||
import { http, HttpResponse } from "msw";
|
|
||||||
import { BASE_URL } from "shared/config";
|
|
||||||
import { REFRESH_API_ROUTE } from "./refresh";
|
|
||||||
import { MOCK_EXISTING_USER, MOCK_FRESH_TOKEN, MOCK_TOKEN } from "../mocks";
|
|
||||||
|
|
||||||
const REFRESH_URL = `${BASE_URL}/${REFRESH_API_ROUTE}`;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Msw interceptor. Mocks the refresh endpoint response.
|
|
||||||
* Validates the Authorization header — returns a fresh token on success, 401 on expired/missing session.
|
|
||||||
*/
|
|
||||||
export const refreshMock = http.post(REFRESH_URL, ({ request }) => {
|
|
||||||
const authHeader = request.headers.get("Authorization");
|
|
||||||
|
|
||||||
if (authHeader === `Bearer ${MOCK_TOKEN}`) {
|
|
||||||
return HttpResponse.json({
|
|
||||||
user: MOCK_EXISTING_USER,
|
|
||||||
accessToken: MOCK_FRESH_TOKEN,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return HttpResponse.json({ message: "Session expired" }, { status: 401 });
|
|
||||||
});
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
import type { AuthResponse } from "../../../model/types/service";
|
|
||||||
import { authHttpClient } from "../../config/authApi/authApi";
|
|
||||||
|
|
||||||
export const REFRESH_API_ROUTE = "auth/refresh";
|
|
||||||
|
|
||||||
export function refresh() {
|
|
||||||
return authHttpClient.post(REFRESH_API_ROUTE).json<AuthResponse>();
|
|
||||||
}
|
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
import { http, HttpResponse } from "msw";
|
import { http, HttpResponse } from "msw";
|
||||||
import type { AuthData } from "../../../model/types/service";
|
import type { AuthData } from "../../../model/types/service";
|
||||||
import { BASE_URL } from "shared/config";
|
import { BASE_URL, MOCK_TOKEN } from "shared/api";
|
||||||
import { REGISTER_API_ROUTE } from "./register";
|
import { REGISTER_API_ROUTE } from "./register";
|
||||||
import { MOCK_EMAIL, MOCK_TOKEN } from "../mocks";
|
import { MOCK_EMAIL } from "../mocks";
|
||||||
|
|
||||||
const REGISTER_URL = `${BASE_URL}/${REGISTER_API_ROUTE}`;
|
const REGISTER_URL = `${BASE_URL}/${REGISTER_API_ROUTE}`;
|
||||||
|
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ import {
|
|||||||
MOCK_NEW_EMAIL,
|
MOCK_NEW_EMAIL,
|
||||||
MOCK_NEW_USER,
|
MOCK_NEW_USER,
|
||||||
MOCK_PASSWORD,
|
MOCK_PASSWORD,
|
||||||
MOCK_TOKEN,
|
|
||||||
} from "../mocks";
|
} from "../mocks";
|
||||||
|
import { MOCK_TOKEN } from "shared/api";
|
||||||
|
|
||||||
const server = setupServer(registerMock);
|
const server = setupServer(registerMock);
|
||||||
|
|
||||||
|
|||||||
@@ -1,44 +1,34 @@
|
|||||||
import { api as baseApi } from "shared/config";
|
import { api as baseApi, useTokenStore } from "shared/api";
|
||||||
|
|
||||||
type TokenGetter = () => string | null | undefined;
|
export const authHttpClient = baseApi.extend({
|
||||||
|
hooks: {
|
||||||
|
beforeRequest: [
|
||||||
|
(request) => {
|
||||||
|
const token = useTokenStore.getState().accessToken;
|
||||||
|
|
||||||
class HttpClient {
|
if (token) {
|
||||||
private getToken: TokenGetter = () => null;
|
request.headers.set("Authorization", `Bearer ${token}`);
|
||||||
|
}
|
||||||
|
|
||||||
setTokenGetter(fn: TokenGetter) {
|
return request;
|
||||||
this.getToken = fn;
|
},
|
||||||
}
|
],
|
||||||
|
afterResponse: [
|
||||||
// Extend base API with authentication hooks
|
async (request, options, response) => {
|
||||||
private instance = baseApi.extend({
|
if (response.status !== 401) {
|
||||||
hooks: {
|
|
||||||
beforeRequest: [
|
|
||||||
(request) => {
|
|
||||||
const token = this.getToken();
|
|
||||||
|
|
||||||
if (token) {
|
|
||||||
request.headers.set("Authorization", `Bearer ${token}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return request;
|
|
||||||
},
|
|
||||||
],
|
|
||||||
afterResponse: [
|
|
||||||
async (request, options, response) => {
|
|
||||||
// Refresh token logic
|
|
||||||
return response;
|
return response;
|
||||||
},
|
}
|
||||||
],
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
get = (url: string, options?: Parameters<typeof this.instance.get>[1]) => {
|
const { accessToken, refreshToken } = useTokenStore.getState();
|
||||||
return this.instance.get(url, options);
|
if (!accessToken) return response;
|
||||||
};
|
|
||||||
|
|
||||||
post = (url: string, options?: Parameters<typeof this.instance.post>[1]) => {
|
try {
|
||||||
return this.instance.post(url, options);
|
await refreshToken?.();
|
||||||
};
|
return authHttpClient(request); // beforeRequest picks up new token automatically
|
||||||
}
|
} catch {
|
||||||
|
return response;
|
||||||
export const authHttpClient = new HttpClient();
|
}
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import {
|
|||||||
MOCK_EMAIL,
|
MOCK_EMAIL,
|
||||||
MOCK_NEW_EMAIL,
|
MOCK_NEW_EMAIL,
|
||||||
MOCK_PASSWORD,
|
MOCK_PASSWORD,
|
||||||
MOCK_TOKEN,
|
|
||||||
} from "../../../api/calls/mocks";
|
} from "../../../api/calls/mocks";
|
||||||
|
|
||||||
const server = setupServer(loginMock, registerMock, logoutMock, refreshMock);
|
const server = setupServer(loginMock, registerMock, logoutMock, refreshMock);
|
||||||
@@ -26,7 +25,7 @@ describe("authStore", () => {
|
|||||||
describe("reset", () => {
|
describe("reset", () => {
|
||||||
it("should reset the store to default state", () => {
|
it("should reset the store to default state", () => {
|
||||||
useAuthStore.getState().reset();
|
useAuthStore.getState().reset();
|
||||||
expect(useAuthStore.getState()).toMatchObject({ ...defaultStoreState });
|
expect(useAuthStore.getState()).toMatchObject(defaultStoreState);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -36,9 +35,8 @@ describe("authStore", () => {
|
|||||||
.getState()
|
.getState()
|
||||||
.login({ email: MOCK_EMAIL, password: MOCK_PASSWORD });
|
.login({ email: MOCK_EMAIL, password: MOCK_PASSWORD });
|
||||||
|
|
||||||
const { accessToken, user, status, error } = useAuthStore.getState();
|
const { user, status, error } = useAuthStore.getState();
|
||||||
|
|
||||||
expect(accessToken).toBeDefined();
|
|
||||||
expect(user).toBeDefined();
|
expect(user).toBeDefined();
|
||||||
expect(status).toBe("authenticated");
|
expect(status).toBe("authenticated");
|
||||||
expect(error).toBeNull();
|
expect(error).toBeNull();
|
||||||
@@ -62,9 +60,8 @@ describe("authStore", () => {
|
|||||||
.getState()
|
.getState()
|
||||||
.register({ email: MOCK_NEW_EMAIL, password: MOCK_PASSWORD });
|
.register({ email: MOCK_NEW_EMAIL, password: MOCK_PASSWORD });
|
||||||
|
|
||||||
const { accessToken, user, status, error } = useAuthStore.getState();
|
const { user, status, error } = useAuthStore.getState();
|
||||||
|
|
||||||
expect(accessToken).toBeDefined();
|
|
||||||
expect(user).toBeDefined();
|
expect(user).toBeDefined();
|
||||||
expect(status).toBe("authenticated");
|
expect(status).toBe("authenticated");
|
||||||
expect(error).toBeNull();
|
expect(error).toBeNull();
|
||||||
@@ -86,38 +83,11 @@ describe("authStore", () => {
|
|||||||
it("should clear access token, user data, and update status after logout", async () => {
|
it("should clear access token, user data, and update status after logout", async () => {
|
||||||
await useAuthStore.getState().logout();
|
await useAuthStore.getState().logout();
|
||||||
|
|
||||||
const { accessToken, user, status, error } = useAuthStore.getState();
|
const { user, status, error } = useAuthStore.getState();
|
||||||
|
|
||||||
expect(accessToken).toBeUndefined();
|
|
||||||
expect(user).toBeUndefined();
|
expect(user).toBeUndefined();
|
||||||
expect(status).toBe("unauthenticated");
|
expect(status).toBe("unauthenticated");
|
||||||
expect(error).toBeNull();
|
expect(error).toBeNull();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("refresh", () => {
|
|
||||||
it("should update access token and user data after successful refresh", async () => {
|
|
||||||
useAuthStore.setState({ accessToken: MOCK_TOKEN });
|
|
||||||
|
|
||||||
await useAuthStore.getState().refresh();
|
|
||||||
|
|
||||||
const { accessToken, user, status, error } = useAuthStore.getState();
|
|
||||||
|
|
||||||
expect(accessToken).toBeDefined();
|
|
||||||
expect(user).toBeDefined();
|
|
||||||
expect(status).toBe("authenticated");
|
|
||||||
expect(error).toBeNull();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should set error and update status if refresh fails", async () => {
|
|
||||||
useAuthStore.setState({ accessToken: "old_token" });
|
|
||||||
|
|
||||||
await useAuthStore.getState().refresh();
|
|
||||||
|
|
||||||
const { status, error } = useAuthStore.getState();
|
|
||||||
|
|
||||||
expect(status).toBe("unauthenticated");
|
|
||||||
expect(error).toBeDefined();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,20 +1,18 @@
|
|||||||
import { create } from "zustand";
|
import { create } from "zustand";
|
||||||
import type { AuthStore, AuthStoreState } from "../../types/store";
|
import type { AuthStore, AuthStoreState } from "../../types/store";
|
||||||
import { login, logout, refresh, 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/api";
|
||||||
import { authHttpClient } from "../../../api/config/authApi/authApi";
|
|
||||||
|
|
||||||
export const defaultStoreState: Readonly<AuthStoreState> = {
|
export const defaultStoreState: Readonly<AuthStoreState> = {
|
||||||
user: undefined,
|
user: undefined,
|
||||||
status: "idle",
|
status: "idle",
|
||||||
accessToken: undefined,
|
|
||||||
error: null,
|
error: null,
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useAuthStore = create<AuthStore>()((set) => ({
|
export const useAuthStore = create<AuthStore>()((set) => ({
|
||||||
...defaultStoreState,
|
...defaultStoreState,
|
||||||
reset: () => set({ ...defaultStoreState }),
|
reset: () => set(defaultStoreState),
|
||||||
login: async (loginData) => {
|
login: async (loginData) => {
|
||||||
set({ status: "loading" });
|
set({ status: "loading" });
|
||||||
try {
|
try {
|
||||||
@@ -28,9 +26,10 @@ 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,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// useTokenStore.setState({ accessToken: responseData?.accessToken });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
set({
|
set({
|
||||||
@@ -53,9 +52,10 @@ 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,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// useTokenStore.setState({ accessToken: responseData?.accessToken });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
set({
|
set({
|
||||||
@@ -77,38 +77,13 @@ export const useAuthStore = create<AuthStore>()((set) => ({
|
|||||||
set({
|
set({
|
||||||
status: "unauthenticated",
|
status: "unauthenticated",
|
||||||
user: undefined,
|
user: undefined,
|
||||||
accessToken: undefined,
|
|
||||||
error: null,
|
error: null,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// useTokenStore.setState({ accessToken: null });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
set({ error: new Error(UNEXPECTED_ERROR_MESSAGE) });
|
set({ error: new Error(UNEXPECTED_ERROR_MESSAGE) });
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
refresh: async () => {
|
|
||||||
set({ status: "loading" });
|
|
||||||
try {
|
|
||||||
const [responseData, refreshError] = await callApi(() => refresh());
|
|
||||||
|
|
||||||
if (refreshError) {
|
|
||||||
set({ status: "unauthenticated", error: refreshError });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
set({
|
|
||||||
status: "authenticated",
|
|
||||||
user: responseData?.user,
|
|
||||||
accessToken: responseData?.accessToken,
|
|
||||||
error: null,
|
|
||||||
});
|
|
||||||
} catch (err) {
|
|
||||||
console.error(err);
|
|
||||||
set({
|
|
||||||
status: "unauthenticated",
|
|
||||||
error: new Error(UNEXPECTED_ERROR_MESSAGE),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}));
|
}));
|
||||||
|
|
||||||
authHttpClient.setTokenGetter(() => useAuthStore.getState().accessToken);
|
|
||||||
|
|||||||
@@ -11,10 +11,6 @@ export interface AuthStoreState {
|
|||||||
* Authentication status
|
* Authentication status
|
||||||
*/
|
*/
|
||||||
status: AuthStatus;
|
status: AuthStatus;
|
||||||
/**
|
|
||||||
* Authentication token
|
|
||||||
*/
|
|
||||||
accessToken?: string;
|
|
||||||
/**
|
/**
|
||||||
* Error data
|
* Error data
|
||||||
*/
|
*/
|
||||||
@@ -25,14 +21,12 @@ export type ResetAction = () => void;
|
|||||||
export type LoginAction = (data: AuthData) => Promise<void>;
|
export type LoginAction = (data: AuthData) => Promise<void>;
|
||||||
export type RegisterAction = (data: AuthData) => Promise<void>;
|
export type RegisterAction = (data: AuthData) => Promise<void>;
|
||||||
export type LogoutAction = () => Promise<void>;
|
export type LogoutAction = () => Promise<void>;
|
||||||
export type RefreshAction = () => Promise<void>;
|
|
||||||
|
|
||||||
export interface AuthStoreActions {
|
export interface AuthStoreActions {
|
||||||
reset: ResetAction;
|
reset: ResetAction;
|
||||||
login: LoginAction;
|
login: LoginAction;
|
||||||
register: RegisterAction;
|
register: RegisterAction;
|
||||||
logout: LogoutAction;
|
logout: LogoutAction;
|
||||||
refresh: RefreshAction;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export type AuthStore = AuthStoreState & AuthStoreActions;
|
export type AuthStore = AuthStoreState & AuthStoreActions;
|
||||||
|
|||||||
2
src/shared/api/calls/index.ts
Normal file
2
src/shared/api/calls/index.ts
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
export * from "./refresh/refresh";
|
||||||
|
export * from "./refresh/refresh.mock";
|
||||||
26
src/shared/api/calls/refresh/refresh.mock.ts
Normal file
26
src/shared/api/calls/refresh/refresh.mock.ts
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
import { http, HttpResponse } from "msw";
|
||||||
|
import { REFRESH_API_ROUTE } from "./refresh";
|
||||||
|
import { BASE_URL, MOCK_FRESH_TOKEN, MOCK_TOKEN } from "../../model";
|
||||||
|
|
||||||
|
const REFRESH_URL = `${BASE_URL}/${REFRESH_API_ROUTE}`;
|
||||||
|
const MOCK_BEARER_TOKEN = `Bearer ${MOCK_TOKEN}`;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Msw interceptor. Mocks the refresh endpoint response.
|
||||||
|
* Cookie validation is a server concern — always returns a fresh token.
|
||||||
|
* Use server.use() to override with a 401 in tests that need a failure case.
|
||||||
|
*/
|
||||||
|
export const refreshMock = http.post(REFRESH_URL, async ({ request }) => {
|
||||||
|
if (request.headers.get("Authorization") === MOCK_BEARER_TOKEN) {
|
||||||
|
return HttpResponse.json({
|
||||||
|
accessToken: MOCK_FRESH_TOKEN,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return HttpResponse.json(
|
||||||
|
{
|
||||||
|
error: "Unauthorized",
|
||||||
|
},
|
||||||
|
{ status: 401 },
|
||||||
|
);
|
||||||
|
});
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
import { setupServer } from "msw/node";
|
import { setupServer } from "msw/node";
|
||||||
import { useAuthStore } from "../../../model";
|
import { useTokenStore } from "../../model/stores/tokenStore";
|
||||||
import { refresh } from "./refresh";
|
import { refresh } from "./refresh";
|
||||||
import { refreshMock } from "./refresh.mock";
|
import { refreshMock } from "./refresh.mock";
|
||||||
import { MOCK_EXISTING_USER, MOCK_FRESH_TOKEN, MOCK_TOKEN } from "../mocks";
|
import { MOCK_FRESH_TOKEN, MOCK_TOKEN } from "../../model/const";
|
||||||
|
|
||||||
const server = setupServer(refreshMock);
|
const server = setupServer(refreshMock);
|
||||||
|
|
||||||
@@ -10,24 +10,21 @@ describe("refresh", () => {
|
|||||||
beforeAll(() => server.listen({ onUnhandledRequest: "error" }));
|
beforeAll(() => server.listen({ onUnhandledRequest: "error" }));
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
server.resetHandlers();
|
server.resetHandlers();
|
||||||
useAuthStore.setState({ accessToken: undefined });
|
useTokenStore.setState({ accessToken: undefined });
|
||||||
});
|
});
|
||||||
afterAll(() => server.close());
|
afterAll(() => server.close());
|
||||||
|
|
||||||
describe("happy path", () => {
|
describe("happy path", () => {
|
||||||
it("returns a fresh access token and user when session is valid", async () => {
|
it("returns a fresh access token and user when session is valid", async () => {
|
||||||
useAuthStore.setState({ accessToken: MOCK_TOKEN });
|
const result = await refresh(MOCK_TOKEN);
|
||||||
|
|
||||||
const result = await refresh();
|
|
||||||
|
|
||||||
expect(result.accessToken).toBe(MOCK_FRESH_TOKEN);
|
expect(result.accessToken).toBe(MOCK_FRESH_TOKEN);
|
||||||
expect(result.user).toEqual(MOCK_EXISTING_USER);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("error cases", () => {
|
describe("error cases", () => {
|
||||||
it("throws when session is expired or missing", async () => {
|
it("throws when session is expired or missing", async () => {
|
||||||
await expect(refresh()).rejects.toThrow();
|
await expect(refresh("expired_token")).rejects.toThrow();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
10
src/shared/api/calls/refresh/refresh.ts
Normal file
10
src/shared/api/calls/refresh/refresh.ts
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
import { api } from "../../config/api";
|
||||||
|
import type { RefreshTokenResponse } from "../../model/types";
|
||||||
|
|
||||||
|
export const REFRESH_API_ROUTE = "auth/refresh";
|
||||||
|
|
||||||
|
export function refresh(token: string) {
|
||||||
|
return api
|
||||||
|
.post(REFRESH_API_ROUTE, { headers: { Authorization: `Bearer ${token}` } })
|
||||||
|
.json<RefreshTokenResponse>();
|
||||||
|
}
|
||||||
20
src/shared/api/config/api.ts
Normal file
20
src/shared/api/config/api.ts
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import ky from "ky";
|
||||||
|
import { BASE_URL } from "../model/const";
|
||||||
|
import { useTokenStore } from "../model";
|
||||||
|
|
||||||
|
export const api = ky.create({
|
||||||
|
prefixUrl: BASE_URL,
|
||||||
|
hooks: {
|
||||||
|
beforeRequest: [
|
||||||
|
(request) => {
|
||||||
|
const token = useTokenStore.getState().accessToken;
|
||||||
|
|
||||||
|
if (token) {
|
||||||
|
request.headers.set("Authorization", `Bearer ${token}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return request;
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
});
|
||||||
2
src/shared/api/index.ts
Normal file
2
src/shared/api/index.ts
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
export * from "./model";
|
||||||
|
export { api } from "./config/api";
|
||||||
@@ -2,3 +2,5 @@ export const BASE_URL =
|
|||||||
import.meta.env.VITE_API_BASE_URL || "https://localhost:3001";
|
import.meta.env.VITE_API_BASE_URL || "https://localhost:3001";
|
||||||
|
|
||||||
export const UNEXPECTED_ERROR_MESSAGE = "An unexpected error occured";
|
export const UNEXPECTED_ERROR_MESSAGE = "An unexpected error occured";
|
||||||
|
export const MOCK_TOKEN = "mock.access.token";
|
||||||
|
export const MOCK_FRESH_TOKEN = "mock.fresh.access.token";
|
||||||
3
src/shared/api/model/index.ts
Normal file
3
src/shared/api/model/index.ts
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
export * from "./types";
|
||||||
|
export * from "./stores/tokenStore";
|
||||||
|
export * from "./const";
|
||||||
45
src/shared/api/model/stores/tokenStore.spec.ts
Normal file
45
src/shared/api/model/stores/tokenStore.spec.ts
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
import { setupServer } from "msw/node";
|
||||||
|
import { MOCK_TOKEN } from "../const";
|
||||||
|
import { defaultStoreState, useTokenStore } from "./tokenStore";
|
||||||
|
import { refreshMock } from "shared/api/calls";
|
||||||
|
|
||||||
|
const server = setupServer(refreshMock);
|
||||||
|
|
||||||
|
describe("tokenStore", () => {
|
||||||
|
beforeAll(() => server.listen({ onUnhandledRequest: "error" }));
|
||||||
|
afterEach(() => {
|
||||||
|
useTokenStore.getState().reset();
|
||||||
|
server.resetHandlers();
|
||||||
|
});
|
||||||
|
afterAll(() => server.close());
|
||||||
|
|
||||||
|
describe("reset", () => {
|
||||||
|
it("should reset the store to default state", () => {
|
||||||
|
useTokenStore.getState().reset();
|
||||||
|
expect(useTokenStore.getState()).toMatchObject(defaultStoreState);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("refreshToken", () => {
|
||||||
|
it("should update access token after successful refresh", async () => {
|
||||||
|
useTokenStore.setState({ accessToken: MOCK_TOKEN });
|
||||||
|
|
||||||
|
await useTokenStore.getState().refreshToken();
|
||||||
|
|
||||||
|
const { accessToken, error } = useTokenStore.getState();
|
||||||
|
|
||||||
|
expect(accessToken).toBeDefined();
|
||||||
|
expect(error).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should set error if refresh fails", async () => {
|
||||||
|
useTokenStore.setState({ accessToken: "old_token" });
|
||||||
|
|
||||||
|
await useTokenStore.getState().refreshToken();
|
||||||
|
|
||||||
|
const { error } = useTokenStore.getState();
|
||||||
|
|
||||||
|
expect(error).toBeDefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
39
src/shared/api/model/stores/tokenStore.ts
Normal file
39
src/shared/api/model/stores/tokenStore.ts
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
import { create } from "zustand";
|
||||||
|
import type { TokenStore, TokenStoreState } from "../types";
|
||||||
|
import { callApi } from "shared/utils";
|
||||||
|
import { refresh } from "../../calls/refresh";
|
||||||
|
import { UNEXPECTED_ERROR_MESSAGE } from "../const";
|
||||||
|
|
||||||
|
export const defaultStoreState: TokenStoreState = {
|
||||||
|
accessToken: null,
|
||||||
|
error: null,
|
||||||
|
};
|
||||||
|
|
||||||
|
export const useTokenStore = create<TokenStore>()((set, get) => ({
|
||||||
|
...defaultStoreState,
|
||||||
|
reset: () => set(defaultStoreState),
|
||||||
|
refreshToken: async () => {
|
||||||
|
try {
|
||||||
|
const currentToken = get().accessToken;
|
||||||
|
if (!currentToken) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const [refreshResponse, refreshError] = await callApi(() =>
|
||||||
|
refresh(currentToken),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (refreshError) {
|
||||||
|
set({ error: refreshError });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
set({ accessToken: refreshResponse?.accessToken });
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
set({
|
||||||
|
error: new Error(UNEXPECTED_ERROR_MESSAGE),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}));
|
||||||
17
src/shared/api/model/types/index.ts
Normal file
17
src/shared/api/model/types/index.ts
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
import type { ApiError } from "shared/utils";
|
||||||
|
|
||||||
|
export interface TokenStoreState {
|
||||||
|
accessToken: string | null;
|
||||||
|
error: ApiError | Error | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface TokenStoreActions {
|
||||||
|
reset: () => void;
|
||||||
|
refreshToken: () => Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type TokenStore = TokenStoreState & TokenStoreActions;
|
||||||
|
|
||||||
|
export interface RefreshTokenResponse {
|
||||||
|
accessToken: string;
|
||||||
|
}
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
import ky from "ky";
|
|
||||||
import { BASE_URL } from "./constants";
|
|
||||||
|
|
||||||
export const api = ky.create({
|
|
||||||
prefixUrl: BASE_URL,
|
|
||||||
});
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
export * from "./api/constants";
|
|
||||||
export * from "./api/httpClient";
|
|
||||||
Reference in New Issue
Block a user