feat: add user management functionality with Gitea API integration and Zustand store

This commit is contained in:
ngc2207 2024-12-03 15:57:31 +08:00
parent ef6343fd9c
commit b075614a42
3 changed files with 63 additions and 3 deletions

View File

@ -15,6 +15,7 @@
"@radix-ui/react-select": "^2.1.2",
"@radix-ui/react-slot": "^1.1.0",
"@radix-ui/react-toast": "^1.2.2",
"base-64": "^1.0.0",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
"cross-fetch": "^4.0.0",
@ -32,13 +33,14 @@
"zustand": "^5.0.1"
},
"devDependencies": {
"typescript": "^5",
"@types/base-64": "^1.0.2",
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"eslint": "^8",
"eslint-config-next": "15.0.3",
"postcss": "^8",
"tailwindcss": "^3.4.1",
"eslint": "^8",
"eslint-config-next": "15.0.3"
"typescript": "^5"
}
}

View File

@ -0,0 +1,23 @@
import base64 from "base-64";
import { APIError, RequestParams, User } from "gitea-js";
import { callGiteaApi, gitea, GiteaApiResponse } from "@/lib/gitea";
export async function userGetCurrent(
username: string,
password: string
): Promise<GiteaApiResponse<User, APIError>> {
const basicAuth = `Basic ${base64.encode(`${username}:${password}`)}`;
const params: RequestParams = {
headers: {
Authorization: basicAuth,
},
};
const context = { username, password };
const result = await callGiteaApi(
() => gitea.user.userGetCurrent(params),
"Successfully retrieved current user",
"Failed to retrieve current user",
context
);
return result;
}

View File

@ -0,0 +1,35 @@
import { create } from "zustand";
import logger from "@/lib/logger";
import { userGetCurrent } from ".";
import { APIError, User } from "gitea-js";
interface UserGetCurrentState {
user: User | null;
error: APIError | null;
loading: boolean;
userGetCurrent: (username: string, password: string) => Promise<void>;
}
const userGetCurrentStore = create<UserGetCurrentState>((set) => ({
user: null,
error: null,
loading: false,
userGetCurrent: async (username: string, password: string) => {
set({ loading: true, user: null, error: null });
try {
const result = await userGetCurrent(username, password);
if (result.error) {
set({ loading: false, user: null, error: result.error });
} else {
set({ loading: false, user: result.data, error: null });
}
} catch (error) {
logger.error({ error }, "Error in userGetCurrentStore");
set({
loading: false,
user: null,
error: { message: "Error in Server" } as APIError,
});
}
},
}));