cms: events and kyc #10

Merged
sugar merged 12 commits from noa.virellia/events-list into develop 2026-02-05 11:22:53 +00:00
15 changed files with 186 additions and 150 deletions
Showing only changes of commit 6ea414bc88 - Show all commits

View File

@@ -3,7 +3,7 @@ import pluginQuery from '@tanstack/eslint-plugin-query';
export default antfu({ export default antfu({
gitignore: true, gitignore: true,
ignores: ['**/node_modules/**', '**/dist/**', 'bun.lock', '**/routeTree.gen.ts', '**/ui/**', 'src/components/editor/**/*', 'src/client/**/*'], ignores: ['**/node_modules/**', '**/dist/**', 'bun.lock', '**/routeTree.gen.ts', '**/ui/**', 'src/components/editor/**/*', 'src/client/**/*', 'openapi-ts.config.ts'],
react: true, react: true,
stylistic: { stylistic: {
semi: true, semi: true,

View File

@@ -3,8 +3,8 @@
import { type InfiniteData, infiniteQueryOptions, queryOptions, type UseMutationOptions } from '@tanstack/react-query'; import { type InfiniteData, infiniteQueryOptions, queryOptions, type UseMutationOptions } from '@tanstack/react-query';
import { client } from '../client.gen'; import { client } from '../client.gen';
import { getAuthRedirect, getEventCheckin, getEventCheckinQuery, getEventInfo, getUserFull, getUserInfo, getUserList, type Options, patchUserUpdate, postAuthExchange, postAuthMagic, postAuthRefresh, postAuthToken, postEventCheckinSubmit } from '../sdk.gen'; import { getAuthRedirect, getEventCheckin, getEventCheckinQuery, getEventInfo, getEventList, getUserInfo, getUserList, type Options, patchUserUpdate, postAuthExchange, postAuthMagic, postAuthRefresh, postAuthToken, postEventCheckinSubmit } from '../sdk.gen';
import type { GetAuthRedirectData, GetAuthRedirectError, GetEventCheckinData, GetEventCheckinError, GetEventCheckinQueryData, GetEventCheckinQueryError, GetEventCheckinQueryResponse, GetEventCheckinResponse, GetEventInfoData, GetEventInfoError, GetEventInfoResponse, GetUserFullData, GetUserFullError, GetUserFullResponse, GetUserInfoData, GetUserInfoError, GetUserInfoResponse, GetUserListData, GetUserListError, GetUserListResponse, PatchUserUpdateData, PatchUserUpdateError, PatchUserUpdateResponse, PostAuthExchangeData, PostAuthExchangeError, PostAuthExchangeResponse, PostAuthMagicData, PostAuthMagicError, PostAuthMagicResponse, PostAuthRefreshData, PostAuthRefreshError, PostAuthRefreshResponse, PostAuthTokenData, PostAuthTokenError, PostAuthTokenResponse, PostEventCheckinSubmitData, PostEventCheckinSubmitError, PostEventCheckinSubmitResponse } from '../types.gen'; import type { GetAuthRedirectData, GetAuthRedirectError, GetEventCheckinData, GetEventCheckinError, GetEventCheckinQueryData, GetEventCheckinQueryError, GetEventCheckinQueryResponse, GetEventCheckinResponse, GetEventInfoData, GetEventInfoError, GetEventInfoResponse, GetEventListData, GetEventListError, GetEventListResponse, GetUserInfoData, GetUserInfoError, GetUserInfoResponse, GetUserListData, GetUserListError, GetUserListResponse, PatchUserUpdateData, PatchUserUpdateError, PatchUserUpdateResponse, PostAuthExchangeData, PostAuthExchangeError, PostAuthExchangeResponse, PostAuthMagicData, PostAuthMagicError, PostAuthMagicResponse, PostAuthRefreshData, PostAuthRefreshError, PostAuthRefreshResponse, PostAuthTokenData, PostAuthTokenError, PostAuthTokenResponse, PostEventCheckinSubmitData, PostEventCheckinSubmitError, PostEventCheckinSubmitResponse } from '../types.gen';
/** /**
* Exchange Auth Code * Exchange Auth Code
@@ -214,16 +214,16 @@ export const getEventInfoOptions = (options: Options<GetEventInfoData>) => query
queryKey: getEventInfoQueryKey(options) queryKey: getEventInfoQueryKey(options)
}); });
export const getUserFullQueryKey = (options?: Options<GetUserFullData>) => createQueryKey('getUserFull', options); export const getEventListQueryKey = (options: Options<GetEventListData>) => createQueryKey('getEventList', options);
/** /**
* Get Full User Table * List Events
* *
* Fetches all user records without pagination. This is typically used for administrative overview or data export. * Fetches a list of events with support for pagination via limit and offset. Data is retrieved directly from the database for consistency.
*/ */
export const getUserFullOptions = (options?: Options<GetUserFullData>) => queryOptions<GetUserFullResponse, GetUserFullError, GetUserFullResponse, ReturnType<typeof getUserFullQueryKey>>({ export const getEventListOptions = (options: Options<GetEventListData>) => queryOptions<GetEventListResponse, GetEventListError, GetEventListResponse, ReturnType<typeof getEventListQueryKey>>({
queryFn: async ({ queryKey, signal }) => { queryFn: async ({ queryKey, signal }) => {
const { data } = await getUserFull({ const { data } = await getEventList({
...options, ...options,
...queryKey[0], ...queryKey[0],
signal, signal,
@@ -231,7 +231,65 @@ export const getUserFullOptions = (options?: Options<GetUserFullData>) => queryO
}); });
return data; return data;
}, },
queryKey: getUserFullQueryKey(options) queryKey: getEventListQueryKey(options)
});
const createInfiniteParams = <K extends Pick<QueryKey<Options>[0], 'body' | 'headers' | 'path' | 'query'>>(queryKey: QueryKey<Options>, page: K) => {
const params = { ...queryKey[0] };
if (page.body) {
params.body = {
...queryKey[0].body as any,
...page.body as any
};
}
if (page.headers) {
params.headers = {
...queryKey[0].headers,
...page.headers
};
}
if (page.path) {
params.path = {
...queryKey[0].path as any,
...page.path as any
};
}
if (page.query) {
params.query = {
...queryKey[0].query as any,
...page.query as any
};
}
return params as unknown as typeof page;
};
export const getEventListInfiniteQueryKey = (options: Options<GetEventListData>): QueryKey<Options<GetEventListData>> => createQueryKey('getEventList', options, true);
/**
* List Events
*
* Fetches a list of events with support for pagination via limit and offset. Data is retrieved directly from the database for consistency.
*/
export const getEventListInfiniteOptions = (options: Options<GetEventListData>) => infiniteQueryOptions<GetEventListResponse, GetEventListError, InfiniteData<GetEventListResponse>, QueryKey<Options<GetEventListData>>, string | Pick<QueryKey<Options<GetEventListData>>[0], 'body' | 'headers' | 'path' | 'query'>>(
// @ts-ignore
{
queryFn: async ({ pageParam, queryKey, signal }) => {
// @ts-ignore
const page: Pick<QueryKey<Options<GetEventListData>>[0], 'body' | 'headers' | 'path' | 'query'> = typeof pageParam === 'object' ? pageParam : {
query: {
offset: pageParam
}
};
const params = createInfiniteParams(queryKey, page);
const { data } = await getEventList({
...options,
...params,
signal,
throwOnError: true
});
return data;
},
queryKey: getEventListInfiniteQueryKey(options)
}); });
export const getUserInfoQueryKey = (options?: Options<GetUserInfoData>) => createQueryKey('getUserInfo', options); export const getUserInfoQueryKey = (options?: Options<GetUserInfoData>) => createQueryKey('getUserInfo', options);
@@ -274,35 +332,6 @@ export const getUserListOptions = (options: Options<GetUserListData>) => queryOp
queryKey: getUserListQueryKey(options) queryKey: getUserListQueryKey(options)
}); });
const createInfiniteParams = <K extends Pick<QueryKey<Options>[0], 'body' | 'headers' | 'path' | 'query'>>(queryKey: QueryKey<Options>, page: K) => {
const params = { ...queryKey[0] };
if (page.body) {
params.body = {
...queryKey[0].body as any,
...page.body as any
};
}
if (page.headers) {
params.headers = {
...queryKey[0].headers,
...page.headers
};
}
if (page.path) {
params.path = {
...queryKey[0].path as any,
...page.path as any
};
}
if (page.query) {
params.query = {
...queryKey[0].query as any,
...page.query as any
};
}
return params as unknown as typeof page;
};
export const getUserListInfiniteQueryKey = (options: Options<GetUserListData>): QueryKey<Options<GetUserListData>> => createQueryKey('getUserList', options, true); export const getUserListInfiniteQueryKey = (options: Options<GetUserListData>): QueryKey<Options<GetUserListData>> => createQueryKey('getUserList', options, true);
/** /**

View File

@@ -1,4 +1,4 @@
// This file is auto-generated by @hey-api/openapi-ts // This file is auto-generated by @hey-api/openapi-ts
export { getAuthRedirect, getEventCheckin, getEventCheckinQuery, getEventInfo, getUserFull, getUserInfo, getUserList, type Options, patchUserUpdate, postAuthExchange, postAuthMagic, postAuthRefresh, postAuthToken, postEventCheckinSubmit } from './sdk.gen'; export { getAuthRedirect, getEventCheckin, getEventCheckinQuery, getEventInfo, getEventList, getUserInfo, getUserList, type Options, patchUserUpdate, postAuthExchange, postAuthMagic, postAuthRefresh, postAuthToken, postEventCheckinSubmit } from './sdk.gen';
export type { ClientOptions, DataUser, DataUserSearchDoc, GetAuthRedirectData, GetAuthRedirectError, GetAuthRedirectErrors, GetEventCheckinData, GetEventCheckinError, GetEventCheckinErrors, GetEventCheckinQueryData, GetEventCheckinQueryError, GetEventCheckinQueryErrors, GetEventCheckinQueryResponse, GetEventCheckinQueryResponses, GetEventCheckinResponse, GetEventCheckinResponses, GetEventInfoData, GetEventInfoError, GetEventInfoErrors, GetEventInfoResponse, GetEventInfoResponses, GetUserFullData, GetUserFullError, GetUserFullErrors, GetUserFullResponse, GetUserFullResponses, GetUserInfoData, GetUserInfoError, GetUserInfoErrors, GetUserInfoResponse, GetUserInfoResponses, GetUserListData, GetUserListError, GetUserListErrors, GetUserListResponse, GetUserListResponses, PatchUserUpdateData, PatchUserUpdateError, PatchUserUpdateErrors, PatchUserUpdateResponse, PatchUserUpdateResponses, PostAuthExchangeData, PostAuthExchangeError, PostAuthExchangeErrors, PostAuthExchangeResponse, PostAuthExchangeResponses, PostAuthMagicData, PostAuthMagicError, PostAuthMagicErrors, PostAuthMagicResponse, PostAuthMagicResponses, PostAuthRefreshData, PostAuthRefreshError, PostAuthRefreshErrors, PostAuthRefreshResponse, PostAuthRefreshResponses, PostAuthTokenData, PostAuthTokenError, PostAuthTokenErrors, PostAuthTokenResponse, PostAuthTokenResponses, PostEventCheckinSubmitData, PostEventCheckinSubmitError, PostEventCheckinSubmitErrors, PostEventCheckinSubmitResponse, PostEventCheckinSubmitResponses, ServiceAuthExchangeData, ServiceAuthExchangeResponse, ServiceAuthMagicData, ServiceAuthMagicResponse, ServiceAuthRefreshData, ServiceAuthTokenData, ServiceAuthTokenResponse, ServiceEventCheckinQueryResponse, ServiceEventCheckinResponse, ServiceEventCheckinSubmitData, ServiceEventInfoResponse, ServiceUserUserInfoData, ServiceUserUserTableResponse, UtilsRespStatus } from './types.gen'; export type { ClientOptions, DataEventIndexDoc, DataUserIndexDoc, GetAuthRedirectData, GetAuthRedirectError, GetAuthRedirectErrors, GetEventCheckinData, GetEventCheckinError, GetEventCheckinErrors, GetEventCheckinQueryData, GetEventCheckinQueryError, GetEventCheckinQueryErrors, GetEventCheckinQueryResponse, GetEventCheckinQueryResponses, GetEventCheckinResponse, GetEventCheckinResponses, GetEventInfoData, GetEventInfoError, GetEventInfoErrors, GetEventInfoResponse, GetEventInfoResponses, GetEventListData, GetEventListError, GetEventListErrors, GetEventListResponse, GetEventListResponses, GetUserInfoData, GetUserInfoError, GetUserInfoErrors, GetUserInfoResponse, GetUserInfoResponses, GetUserListData, GetUserListError, GetUserListErrors, GetUserListResponse, GetUserListResponses, PatchUserUpdateData, PatchUserUpdateError, PatchUserUpdateErrors, PatchUserUpdateResponse, PatchUserUpdateResponses, PostAuthExchangeData, PostAuthExchangeError, PostAuthExchangeErrors, PostAuthExchangeResponse, PostAuthExchangeResponses, PostAuthMagicData, PostAuthMagicError, PostAuthMagicErrors, PostAuthMagicResponse, PostAuthMagicResponses, PostAuthRefreshData, PostAuthRefreshError, PostAuthRefreshErrors, PostAuthRefreshResponse, PostAuthRefreshResponses, PostAuthTokenData, PostAuthTokenError, PostAuthTokenErrors, PostAuthTokenResponse, PostAuthTokenResponses, PostEventCheckinSubmitData, PostEventCheckinSubmitError, PostEventCheckinSubmitErrors, PostEventCheckinSubmitResponse, PostEventCheckinSubmitResponses, ServiceAuthExchangeData, ServiceAuthExchangeResponse, ServiceAuthMagicData, ServiceAuthMagicResponse, ServiceAuthRefreshData, ServiceAuthTokenData, ServiceAuthTokenResponse, ServiceEventCheckinQueryResponse, ServiceEventCheckinResponse, ServiceEventCheckinSubmitData, ServiceEventEventInfoResponse, ServiceUserUserInfoData, UtilsRespStatus } from './types.gen';

View File

@@ -2,7 +2,7 @@
import type { Client, Options as Options2, TDataShape } from './client'; import type { Client, Options as Options2, TDataShape } from './client';
import { client } from './client.gen'; import { client } from './client.gen';
import type { GetAuthRedirectData, GetAuthRedirectErrors, GetEventCheckinData, GetEventCheckinErrors, GetEventCheckinQueryData, GetEventCheckinQueryErrors, GetEventCheckinQueryResponses, GetEventCheckinResponses, GetEventInfoData, GetEventInfoErrors, GetEventInfoResponses, GetUserFullData, GetUserFullErrors, GetUserFullResponses, GetUserInfoData, GetUserInfoErrors, GetUserInfoResponses, GetUserListData, GetUserListErrors, GetUserListResponses, PatchUserUpdateData, PatchUserUpdateErrors, PatchUserUpdateResponses, PostAuthExchangeData, PostAuthExchangeErrors, PostAuthExchangeResponses, PostAuthMagicData, PostAuthMagicErrors, PostAuthMagicResponses, PostAuthRefreshData, PostAuthRefreshErrors, PostAuthRefreshResponses, PostAuthTokenData, PostAuthTokenErrors, PostAuthTokenResponses, PostEventCheckinSubmitData, PostEventCheckinSubmitErrors, PostEventCheckinSubmitResponses } from './types.gen'; import type { GetAuthRedirectData, GetAuthRedirectErrors, GetEventCheckinData, GetEventCheckinErrors, GetEventCheckinQueryData, GetEventCheckinQueryErrors, GetEventCheckinQueryResponses, GetEventCheckinResponses, GetEventInfoData, GetEventInfoErrors, GetEventInfoResponses, GetEventListData, GetEventListErrors, GetEventListResponses, GetUserInfoData, GetUserInfoErrors, GetUserInfoResponses, GetUserListData, GetUserListErrors, GetUserListResponses, PatchUserUpdateData, PatchUserUpdateErrors, PatchUserUpdateResponses, PostAuthExchangeData, PostAuthExchangeErrors, PostAuthExchangeResponses, PostAuthMagicData, PostAuthMagicErrors, PostAuthMagicResponses, PostAuthRefreshData, PostAuthRefreshErrors, PostAuthRefreshResponses, PostAuthTokenData, PostAuthTokenErrors, PostAuthTokenResponses, PostEventCheckinSubmitData, PostEventCheckinSubmitErrors, PostEventCheckinSubmitResponses } from './types.gen';
export type Options<TData extends TDataShape = TDataShape, ThrowOnError extends boolean = boolean> = Options2<TData, ThrowOnError> & { export type Options<TData extends TDataShape = TDataShape, ThrowOnError extends boolean = boolean> = Options2<TData, ThrowOnError> & {
/** /**
@@ -117,11 +117,11 @@ export const postEventCheckinSubmit = <ThrowOnError extends boolean = false>(opt
export const getEventInfo = <ThrowOnError extends boolean = false>(options: Options<GetEventInfoData, ThrowOnError>) => (options.client ?? client).get<GetEventInfoResponses, GetEventInfoErrors, ThrowOnError>({ url: '/event/info', ...options }); export const getEventInfo = <ThrowOnError extends boolean = false>(options: Options<GetEventInfoData, ThrowOnError>) => (options.client ?? client).get<GetEventInfoResponses, GetEventInfoErrors, ThrowOnError>({ url: '/event/info', ...options });
/** /**
* Get Full User Table * List Events
* *
* Fetches all user records without pagination. This is typically used for administrative overview or data export. * Fetches a list of events with support for pagination via limit and offset. Data is retrieved directly from the database for consistency.
*/ */
export const getUserFull = <ThrowOnError extends boolean = false>(options?: Options<GetUserFullData, ThrowOnError>) => (options?.client ?? client).get<GetUserFullResponses, GetUserFullErrors, ThrowOnError>({ url: '/user/full', ...options }); export const getEventList = <ThrowOnError extends boolean = false>(options: Options<GetEventListData, ThrowOnError>) => (options.client ?? client).get<GetEventListResponses, GetEventListErrors, ThrowOnError>({ url: '/event/list', ...options });
/** /**
* Get My User Information * Get My User Information

View File

@@ -4,21 +4,16 @@ export type ClientOptions = {
baseUrl: 'http://localhost:8000/api/v1' | 'https://localhost:8000/api/v1' | (string & {}); baseUrl: 'http://localhost:8000/api/v1' | 'https://localhost:8000/api/v1' | (string & {});
}; };
export type DataUser = { export type DataEventIndexDoc = {
allow_public?: boolean; description?: string;
avatar?: string; end_time?: string;
bio?: string; event_id?: string;
email?: string; name?: string;
id?: number; start_time?: string;
nickname?: string; type?: string;
permission_level?: number;
subtitle?: string;
user_id?: string;
username?: string;
uuid?: string;
}; };
export type DataUserSearchDoc = { export type DataUserIndexDoc = {
avatar?: string; avatar?: string;
email?: string; email?: string;
nickname?: string; nickname?: string;
@@ -76,7 +71,7 @@ export type ServiceEventCheckinSubmitData = {
checkin_code?: string; checkin_code?: string;
}; };
export type ServiceEventInfoResponse = { export type ServiceEventEventInfoResponse = {
end_time?: string; end_time?: string;
name?: string; name?: string;
start_time?: string; start_time?: string;
@@ -94,10 +89,6 @@ export type ServiceUserUserInfoData = {
username?: string; username?: string;
}; };
export type ServiceUserUserTableResponse = {
user_table?: Array<DataUser>;
};
export type UtilsRespStatus = { export type UtilsRespStatus = {
code?: number; code?: number;
data?: unknown; data?: unknown;
@@ -528,22 +519,39 @@ export type GetEventInfoResponses = {
* Successful retrieval * Successful retrieval
*/ */
200: UtilsRespStatus & { 200: UtilsRespStatus & {
data?: ServiceEventInfoResponse; data?: ServiceEventEventInfoResponse;
}; };
}; };
export type GetEventInfoResponse = GetEventInfoResponses[keyof GetEventInfoResponses]; export type GetEventInfoResponse = GetEventInfoResponses[keyof GetEventInfoResponses];
export type GetUserFullData = { export type GetEventListData = {
body?: never; body?: never;
path?: never; path?: never;
query?: never; query: {
url: '/user/full'; /**
* Maximum number of events to return (default 20)
*/
limit?: string;
/**
* Number of events to skip
*/
offset: string;
};
url: '/event/list';
}; };
export type GetUserFullErrors = { export type GetEventListErrors = {
/** /**
* Internal Server Error (Database Error) * Invalid Input (Missing offset or malformed parameters)
*/
400: UtilsRespStatus & {
data?: {
[key: string]: unknown;
};
};
/**
* Internal Server Error (Database query failed)
*/ */
500: UtilsRespStatus & { 500: UtilsRespStatus & {
data?: { data?: {
@@ -552,18 +560,18 @@ export type GetUserFullErrors = {
}; };
}; };
export type GetUserFullError = GetUserFullErrors[keyof GetUserFullErrors]; export type GetEventListError = GetEventListErrors[keyof GetEventListErrors];
export type GetUserFullResponses = { export type GetEventListResponses = {
/** /**
* Successful retrieval of full user table * Successful paginated list retrieval
*/ */
200: UtilsRespStatus & { 200: UtilsRespStatus & {
data?: ServiceUserUserTableResponse; data?: Array<DataEventIndexDoc>;
}; };
}; };
export type GetUserFullResponse = GetUserFullResponses[keyof GetUserFullResponses]; export type GetEventListResponse = GetEventListResponses[keyof GetEventListResponses];
export type GetUserInfoData = { export type GetUserInfoData = {
body?: never; body?: never;
@@ -654,7 +662,7 @@ export type GetUserListResponses = {
* Successful paginated list retrieval * Successful paginated list retrieval
*/ */
200: UtilsRespStatus & { 200: UtilsRespStatus & {
data?: Array<DataUserSearchDoc>; data?: Array<DataUserIndexDoc>;
}; };
}; };

View File

@@ -2,21 +2,16 @@
import { z } from 'zod'; import { z } from 'zod';
export const zDataUser = z.object({ export const zDataEventIndexDoc = z.object({
allow_public: z.optional(z.boolean()), description: z.optional(z.string()),
avatar: z.optional(z.string()), end_time: z.optional(z.string()),
bio: z.optional(z.string()), event_id: z.optional(z.string()),
email: z.optional(z.string()), name: z.optional(z.string()),
id: z.optional(z.int()), start_time: z.optional(z.string()),
nickname: z.optional(z.string()), type: z.optional(z.string())
permission_level: z.optional(z.int()),
subtitle: z.optional(z.string()),
user_id: z.optional(z.string()),
username: z.optional(z.string()),
uuid: z.optional(z.string())
}); });
export const zDataUserSearchDoc = z.object({ export const zDataUserIndexDoc = z.object({
avatar: z.optional(z.string()), avatar: z.optional(z.string()),
email: z.optional(z.string()), email: z.optional(z.string()),
nickname: z.optional(z.string()), nickname: z.optional(z.string()),
@@ -74,7 +69,7 @@ export const zServiceEventCheckinSubmitData = z.object({
checkin_code: z.optional(z.string()) checkin_code: z.optional(z.string())
}); });
export const zServiceEventInfoResponse = z.object({ export const zServiceEventEventInfoResponse = z.object({
end_time: z.optional(z.string()), end_time: z.optional(z.string()),
name: z.optional(z.string()), name: z.optional(z.string()),
start_time: z.optional(z.string()) start_time: z.optional(z.string())
@@ -92,10 +87,6 @@ export const zServiceUserUserInfoData = z.object({
username: z.optional(z.string()) username: z.optional(z.string())
}); });
export const zServiceUserUserTableResponse = z.object({
user_table: z.optional(z.array(zDataUser))
});
export const zUtilsRespStatus = z.object({ export const zUtilsRespStatus = z.object({
code: z.optional(z.int()), code: z.optional(z.int()),
data: z.optional(z.unknown()), data: z.optional(z.unknown()),
@@ -221,20 +212,23 @@ export const zGetEventInfoData = z.object({
* Successful retrieval * Successful retrieval
*/ */
export const zGetEventInfoResponse = zUtilsRespStatus.and(z.object({ export const zGetEventInfoResponse = zUtilsRespStatus.and(z.object({
data: z.optional(zServiceEventInfoResponse) data: z.optional(zServiceEventEventInfoResponse)
})); }));
export const zGetUserFullData = z.object({ export const zGetEventListData = z.object({
body: z.optional(z.never()), body: z.optional(z.never()),
path: z.optional(z.never()), path: z.optional(z.never()),
query: z.optional(z.never()) query: z.object({
limit: z.optional(z.string()),
offset: z.string()
})
}); });
/** /**
* Successful retrieval of full user table * Successful paginated list retrieval
*/ */
export const zGetUserFullResponse = zUtilsRespStatus.and(z.object({ export const zGetEventListResponse = zUtilsRespStatus.and(z.object({
data: z.optional(zServiceUserUserTableResponse) data: z.optional(z.array(zDataEventIndexDoc))
})); }));
export const zGetUserInfoData = z.object({ export const zGetUserInfoData = z.object({
@@ -263,7 +257,7 @@ export const zGetUserListData = z.object({
* Successful paginated list retrieval * Successful paginated list retrieval
*/ */
export const zGetUserListResponse = zUtilsRespStatus.and(z.object({ export const zGetUserListResponse = zUtilsRespStatus.and(z.object({
data: z.optional(z.array(zDataUserSearchDoc)) data: z.optional(z.array(zDataUserIndexDoc))
})); }));
export const zPatchUserUpdateData = z.object({ export const zPatchUserUpdateData = z.object({

View File

@@ -79,7 +79,7 @@ export function EditProfileDialog() {
onSubmit={(e) => { onSubmit={(e) => {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
form.handleSubmit().then(() => setOpen(false)); void form.handleSubmit().then(() => setOpen(false));
}} }}
className="grid gap-4" className="grid gap-4"
> >

View File

@@ -35,7 +35,7 @@ export function MainProfile() {
<div className="flex flex-col w-full gap-3"> <div className="flex flex-col w-full gap-3">
<div className="flex flex-row gap-3 w-full lg:flex-col"> <div className="flex flex-row gap-3 w-full lg:flex-col">
<Avatar className="size-16 rounded-full border-2 border-muted lg:size-64"> <Avatar className="size-16 rounded-full border-2 border-muted lg:size-64">
{user.avatar ? <AvatarImage src={user.avatar} alt={user.nickname} /> : IdentIcon} {user.avatar !== undefined ? <AvatarImage src={user.avatar} alt={user.nickname} /> : IdentIcon}
</Avatar> </Avatar>
<div className="flex flex-1 flex-col justify-center lg:mt-3"> <div className="flex flex-1 flex-col justify-center lg:mt-3">
<span className="font-semibold text-2xl" aria-hidden="true">{user.nickname}</span> <span className="font-semibold text-2xl" aria-hidden="true">{user.nickname}</span>

View File

@@ -53,7 +53,7 @@ function NavUser_() {
className="data-[state=open]:bg-sidebar-accent data-[state=open]:text-sidebar-accent-foreground" className="data-[state=open]:bg-sidebar-accent data-[state=open]:text-sidebar-accent-foreground"
> >
<Avatar className="h-8 w-8 rounded-lg"> <Avatar className="h-8 w-8 rounded-lg">
{user.avatar ? <AvatarImage src={user.avatar} alt={user.nickname} /> : IdentIcon} {user.avatar !== undefined ? <AvatarImage src={user.avatar} alt={user.nickname} /> : IdentIcon}
</Avatar> </Avatar>
<div className="grid flex-1 text-left text-sm leading-tight"> <div className="grid flex-1 text-left text-sm leading-tight">
<span className="truncate font-medium">{user.nickname}</span> <span className="truncate font-medium">{user.nickname}</span>
@@ -73,7 +73,7 @@ function NavUser_() {
<DropdownMenuLabel className="p-0 font-normal"> <DropdownMenuLabel className="p-0 font-normal">
<div className="flex items-center gap-2 px-1 py-1.5 text-left text-sm"> <div className="flex items-center gap-2 px-1 py-1.5 text-left text-sm">
<Avatar className="h-8 w-8 rounded-lg"> <Avatar className="h-8 w-8 rounded-lg">
{user.avatar ? <AvatarImage src={user.avatar} alt={user.nickname} /> : IdentIcon} {user.avatar !== undefined ? <AvatarImage src={user.avatar} alt={user.nickname} /> : IdentIcon}
</Avatar> </Avatar>
<div className="grid flex-1 text-left text-sm leading-tight"> <div className="grid flex-1 text-left text-sm leading-tight">
<span className="truncate font-medium">{user.nickname}</span> <span className="truncate font-medium">{user.nickname}</span>

View File

@@ -6,7 +6,7 @@ export function useExchangeToken() {
return useMutation({ return useMutation({
...postAuthExchangeMutation(), ...postAuthExchangeMutation(),
onSuccess: (data) => { onSuccess: (data) => {
window.location.href = data.data?.redirect_uri!; window.location.href = data.data!.redirect_uri!;
}, },
onError: (error) => { onError: (error) => {
console.error(error); console.error(error);

View File

@@ -1,13 +1,12 @@
import { isEmpty, isNil } from 'lodash-es'; import { isEmpty, isNil } from 'lodash-es';
import { client } from '@/client/client.gen'; import { client } from '@/client/client.gen';
import { router } from './router';
import { import {
clearTokens,
doRefreshToken, doRefreshToken,
getAccessToken,
getRefreshToken, getRefreshToken,
getToken, logout,
setAccessToken,
setRefreshToken, setRefreshToken,
setToken,
} from './token'; } from './token';
export function configInternalApiClient() { export function configInternalApiClient() {
@@ -19,8 +18,8 @@ export function configInternalApiClient() {
}); });
client.interceptors.request.use((request) => { client.interceptors.request.use((request) => {
const token = getToken(); const token = getAccessToken();
if (token) { if (!isNil(token) && !isEmpty(token)) {
request.headers.set('Authorization', `Bearer ${token}`); request.headers.set('Authorization', `Bearer ${token}`);
} }
return request; return request;
@@ -28,14 +27,21 @@ export function configInternalApiClient() {
client.interceptors.response.use(async (response, request, options) => { client.interceptors.response.use(async (response, request, options) => {
if (response.status === 401) { if (response.status === 401) {
const refreshToken = getRefreshToken();
// Avoid infinite loop if the refresh token request itself fails // Avoid infinite loop if the refresh token request itself fails
if (!request.url.includes('/auth/refresh') && !isNil(refreshToken)) { if (request.url.includes('/auth/refresh')) {
try { // Refresh token failed, clear tokens and redirect to login page
const refreshResponse = await doRefreshToken(); logout('Session expired');
}
else {
const refreshToken = getRefreshToken();
if (isNil(refreshToken) || isEmpty(refreshToken)) {
logout('You are not logged in');
}
else {
const refreshResponse = await doRefreshToken(refreshToken);
if (!isEmpty(refreshResponse)) { if (!isEmpty(refreshResponse)) {
const { access_token, refresh_token } = refreshResponse; const { access_token, refresh_token } = refreshResponse;
setToken(access_token!); setAccessToken(access_token!);
setRefreshToken(refresh_token!); setRefreshToken(refresh_token!);
const fetchFn = options.fetch ?? globalThis.fetch; const fetchFn = options.fetch ?? globalThis.fetch;
@@ -50,11 +56,6 @@ export function configInternalApiClient() {
}); });
} }
} }
catch (e) {
clearTokens();
await router.navigate({ to: '/authorize' });
return response;
}
} }
} }
return response; return response;

View File

@@ -1,40 +1,52 @@
import type { ServiceAuthTokenResponse } from '@/client'; import type { ServiceAuthTokenResponse } from '@/client';
import { toast } from 'sonner';
import { postAuthRefresh } from '@/client'; import { postAuthRefresh } from '@/client';
import { router } from './router';
export function setToken(token: string) { const ACCESS_TOKEN_LOCALSTORAGE_KEY = 'token';
localStorage.setItem('token', token); const REFRESH_TOKEN_LOCALSTORAGE_KEY = 'refreshToken';
export function setAccessToken(token: string) {
localStorage.setItem(ACCESS_TOKEN_LOCALSTORAGE_KEY, token);
} }
export function getToken() { export function getAccessToken() {
return localStorage.getItem('token'); return localStorage.getItem(ACCESS_TOKEN_LOCALSTORAGE_KEY);
} }
export function removeToken() { export function removeAccessToken() {
localStorage.removeItem('token'); localStorage.removeItem(ACCESS_TOKEN_LOCALSTORAGE_KEY);
}
export function hasToken() {
return getToken() !== null;
} }
export function setRefreshToken(refreshToken: string) { export function setRefreshToken(refreshToken: string) {
localStorage.setItem('refreshToken', refreshToken); localStorage.setItem(REFRESH_TOKEN_LOCALSTORAGE_KEY, refreshToken);
} }
export function getRefreshToken() { export function getRefreshToken() {
return localStorage.getItem('refreshToken'); return localStorage.getItem(REFRESH_TOKEN_LOCALSTORAGE_KEY);
}
export function removeRefreshToken() {
localStorage.removeItem(REFRESH_TOKEN_LOCALSTORAGE_KEY);
} }
export function clearTokens() { export function clearTokens() {
removeToken(); removeAccessToken();
setRefreshToken(''); removeRefreshToken();
} }
export async function doRefreshToken(): Promise<ServiceAuthTokenResponse | undefined> { export async function doRefreshToken(refreshToken: string): Promise<ServiceAuthTokenResponse | undefined> {
const { data } = await postAuthRefresh({ const { data } = await postAuthRefresh({
body: { body: {
refresh_token: getRefreshToken()!, refresh_token: refreshToken,
}, },
}); });
return data?.data; return data?.data;
} }
export function logout(message: string = 'Logged out') {
clearTokens();
void router.navigate({ to: '/authorize' }).then(() => {
toast.error(message);
});
}

View File

@@ -1,15 +1,7 @@
import { createFileRoute, redirect } from '@tanstack/react-router'; import { createFileRoute } from '@tanstack/react-router';
import { hasToken } from '@/lib/token';
export const Route = createFileRoute('/_sidebarLayout/')({ export const Route = createFileRoute('/_sidebarLayout/')({
component: Index, component: Index,
loader: async () => {
if (!hasToken()) {
throw redirect({
to: '/authorize',
});
}
},
}); });
function Index() { function Index() {

View File

@@ -6,7 +6,7 @@ import z from 'zod';
import { LoginForm } from '@/components/login-form'; import { LoginForm } from '@/components/login-form';
import { useExchangeToken } from '@/hooks/data/useExchangeToken'; import { useExchangeToken } from '@/hooks/data/useExchangeToken';
import { generateOAuthState } from '@/lib/random'; import { generateOAuthState } from '@/lib/random';
import { getToken } from '@/lib/token'; import { getAccessToken } from '@/lib/token';
const baseUrl = import.meta.env.VITE_APP_BASE_URL; const baseUrl = import.meta.env.VITE_APP_BASE_URL;
@@ -25,7 +25,7 @@ export const Route = createFileRoute('/authorize')({
}); });
function RouteComponent() { function RouteComponent() {
const token = getToken(); const token = getAccessToken();
const oauthParams = Route.useSearch(); const oauthParams = Route.useSearch();
const mutation = useExchangeToken(); const mutation = useExchangeToken();
/** /**

View File

@@ -6,7 +6,7 @@ import {
} from 'react'; } from 'react';
import z from 'zod'; import z from 'zod';
import { postAuthTokenMutation } from '@/client/@tanstack/react-query.gen'; import { postAuthTokenMutation } from '@/client/@tanstack/react-query.gen';
import { setRefreshToken, setToken } from '@/lib/token'; import { setAccessToken, setRefreshToken } from '@/lib/token';
const tokenCodeSchema = z.object({ const tokenCodeSchema = z.object({
code: z.string().nonempty(), code: z.string().nonempty(),
@@ -25,8 +25,8 @@ function RouteComponent() {
const mutation = useMutation({ const mutation = useMutation({
...postAuthTokenMutation(), ...postAuthTokenMutation(),
onSuccess: (data) => { onSuccess: (data) => {
setToken(data.data?.access_token!); setAccessToken(data.data!.access_token!);
setRefreshToken(data.data?.refresh_token!); setRefreshToken(data.data!.refresh_token!);
void navigate({ to: '/' }); void navigate({ to: '/' });
}, },
onError: () => { onError: () => {