feat(auth): add bearer token authentication to API clients

Migrate authentication from cookie-based to Bearer token-based approach.
Update axios HTTP client to include Authorization header with session token.
Modify auth client to use Bearer token in fetch options and handle token storage in localStorage.
This commit is contained in:
2025-11-02 09:52:06 +00:00
parent 1148af17e5
commit a8179909d9
2 changed files with 17 additions and 2 deletions

View File

@@ -2,4 +2,9 @@ import axios from "axios";
export const http = axios.create({ export const http = axios.create({
baseURL: import.meta.env.VITE_SERVER_URL as string, baseURL: import.meta.env.VITE_SERVER_URL as string,
headers: {
Authorization: localStorage.getItem("session_token")
? `Bearer ${localStorage.getItem("session_token")}`
: "",
},
}); });

View File

@@ -7,7 +7,6 @@ import {
import { adminClient, usernameClient } from "better-auth/client/plugins"; import { adminClient, usernameClient } from "better-auth/client/plugins";
import { createAccessControl } from "better-auth/plugins/access"; import { createAccessControl } from "better-auth/plugins/access";
import { createAuthClient } from "better-auth/react"; import { createAuthClient } from "better-auth/react";
import { reactStartCookies } from "better-auth/react-start";
const ac = createAccessControl(statement); const ac = createAccessControl(statement);
@@ -23,6 +22,17 @@ export const authClient = createAuthClient({
user: userRole, user: userRole,
}, },
}), }),
reactStartCookies(),
], ],
fetchOptions: {
auth: {
type: "Bearer",
token: () => localStorage.getItem("session_token") || "",
},
onSuccess: (ctx) => {
const authToken = ctx.response.headers.get("set-auth-token");
if (authToken) {
localStorage.setItem("session_token", authToken);
}
},
},
}); });