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:
@@ -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")}`
|
||||||
|
: "",
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user