mirror of
https://litchi.icu/ngc2207/judge4c.git
synced 2025-05-18 09:56:32 +00:00
feat: add user management functionality with Gitea API integration and Zustand store
This commit is contained in:
parent
ef6343fd9c
commit
b075614a42
@ -15,6 +15,7 @@
|
|||||||
"@radix-ui/react-select": "^2.1.2",
|
"@radix-ui/react-select": "^2.1.2",
|
||||||
"@radix-ui/react-slot": "^1.1.0",
|
"@radix-ui/react-slot": "^1.1.0",
|
||||||
"@radix-ui/react-toast": "^1.2.2",
|
"@radix-ui/react-toast": "^1.2.2",
|
||||||
|
"base-64": "^1.0.0",
|
||||||
"class-variance-authority": "^0.7.1",
|
"class-variance-authority": "^0.7.1",
|
||||||
"clsx": "^2.1.1",
|
"clsx": "^2.1.1",
|
||||||
"cross-fetch": "^4.0.0",
|
"cross-fetch": "^4.0.0",
|
||||||
@ -32,13 +33,14 @@
|
|||||||
"zustand": "^5.0.1"
|
"zustand": "^5.0.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"typescript": "^5",
|
"@types/base-64": "^1.0.2",
|
||||||
"@types/node": "^20",
|
"@types/node": "^20",
|
||||||
"@types/react": "^18",
|
"@types/react": "^18",
|
||||||
"@types/react-dom": "^18",
|
"@types/react-dom": "^18",
|
||||||
|
"eslint": "^8",
|
||||||
|
"eslint-config-next": "15.0.3",
|
||||||
"postcss": "^8",
|
"postcss": "^8",
|
||||||
"tailwindcss": "^3.4.1",
|
"tailwindcss": "^3.4.1",
|
||||||
"eslint": "^8",
|
"typescript": "^5"
|
||||||
"eslint-config-next": "15.0.3"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
23
src/app/actions/(gitea)/user/index.ts
Normal file
23
src/app/actions/(gitea)/user/index.ts
Normal 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;
|
||||||
|
}
|
35
src/app/actions/(gitea)/user/store.ts
Normal file
35
src/app/actions/(gitea)/user/store.ts
Normal 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,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}));
|
Loading…
Reference in New Issue
Block a user