Skip to content

Commit

Permalink
change web auth
Browse files Browse the repository at this point in the history
  • Loading branch information
0fatal committed Mar 21, 2024
1 parent 07b6a20 commit e40b54b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
38 changes: 22 additions & 16 deletions web/src/pages/auth/store.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { SessionV1 } from "@zjy365/sealos-desktop-sdk";
import * as yaml from "js-yaml";
import { create } from "zustand";
import { devtools } from "zustand/middleware";
import { devtools, persist } from "zustand/middleware";
import { immer } from "zustand/middleware/immer";

type SessionState = {
Expand All @@ -14,22 +14,28 @@ type SessionState = {

const useSessionStore = create<SessionState>()(
devtools(
immer((set, get) => ({
session: {} as SessionV1,
locale: "zh",
setSession: (ss: SessionV1) => set({ session: ss }),
getKubeconfig: () => {
return get().session?.kubeconfig || "";
persist(
immer((set, get) => ({
session: {} as SessionV1,
locale: "zh",
setSession: (ss: SessionV1) => set({ session: ss }),
getKubeconfig: () => {
return get().session?.kubeconfig || "";
},
getNamespace: () => {
if (!get().session?.kubeconfig) {
return "";
}
const doc = yaml.load(get().session.kubeconfig);
// @ts-ignore
return doc?.contexts[0]?.context?.namespace;
},
})),
{
name: "sealaf-session",
version: 1,
},
getNamespace: () => {
if (!get().session?.kubeconfig) {
return "";
}
const doc = yaml.load(get().session.kubeconfig);
// @ts-ignore
return doc?.contexts[0]?.context?.namespace;
},
})),
),
),
);

Expand Down
3 changes: 3 additions & 0 deletions web/src/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import axios, { AxiosRequestHeaders, AxiosResponse } from "axios";

import { CHAKRA_UI_COLOR_MODE_KEY, VITE_SERVER_BASE_URL } from "../constants";

import useSessionStore from "@/pages/auth/store";

const { toast } = createStandaloneToast();

const request = axios.create({
Expand All @@ -22,6 +24,7 @@ request.interceptors.request.use(
let _headers: AxiosRequestHeaders | any = {
Authorization: "Bearer " + localStorage.getItem("token"),
"Content-Type": "application/json",
Credential: btoa(useSessionStore().getKubeconfig()),
};

config.headers = {
Expand Down
3 changes: 3 additions & 0 deletions web/src/utils/streamFetch.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import useSessionStore from "@/pages/auth/store";

interface StreamFetchProps {
url: string;
onMessage: (text: string) => void;
Expand All @@ -13,6 +15,7 @@ export const streamFetch = ({ url, onMessage, firstResponse, abortSignal }: Stre
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + localStorage.getItem("token"),
Credential: btoa(useSessionStore().getKubeconfig()),
},
signal: abortSignal.signal,
});
Expand Down

0 comments on commit e40b54b

Please sign in to comment.