Skip to content

Commit 2d3adb6

Browse files
authored
[HUMAN App] refactor: service sign up (#3184)
1 parent dd88e4e commit 2d3adb6

17 files changed

Lines changed: 68 additions & 72 deletions

File tree

packages/apps/human-app/frontend/src/api/fetch-refresh-token.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { apiPaths } from '@/api/api-paths';
22
import { browserAuthProvider } from '@/shared/contexts/browser-auth-provider';
3-
import { signInSuccessResponseSchema } from '@/modules/worker/services/sign-in/schema';
3+
import { authTokensSuccessResponseSchema } from '@/shared/schemas';
44

55
export const fetchTokenRefresh = async (baseUrl: string) => {
66
const response = await fetch(
@@ -23,7 +23,7 @@ export const fetchTokenRefresh = async (baseUrl: string) => {
2323

2424
const data: unknown = await response.json();
2525

26-
const refetchAccessTokenSuccess = signInSuccessResponseSchema.parse(data);
26+
const refetchAccessTokenSuccess = authTokensSuccessResponseSchema.parse(data);
2727

2828
return refetchAccessTokenSuccess;
2929
};

packages/apps/human-app/frontend/src/api/fetcher.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { ZodError, type ZodType, type ZodTypeDef } from 'zod';
33
import type { ResponseError } from '@/shared/types/global.type';
44
import { browserAuthProvider } from '@/shared/contexts/browser-auth-provider';
55
import { env } from '@/shared/env';
6-
import { type SignInSuccessResponse } from '@/modules/worker/services/sign-in/types';
76
import { normalizeBaseUrl } from '@/shared/helpers/url';
7+
import { type AuthTokensSuccessResponse } from '@/shared/schemas';
88
import { fetchTokenRefresh } from './fetch-refresh-token';
99

1010
const appendHeader = (
@@ -66,7 +66,7 @@ export type FetcherOptions<SuccessInput, SuccessOutput> =
6666

6767
export type FetcherUrl = string | URL;
6868

69-
let refreshPromise: Promise<SignInSuccessResponse | null> | null = null;
69+
let refreshPromise: Promise<AuthTokensSuccessResponse | null> | null = null;
7070

7171
export async function refreshToken(): Promise<{
7272
access_token: string;

packages/apps/human-app/frontend/src/modules/auth-web3/context/web3-auth-context.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { useState, createContext, useEffect } from 'react';
33
import { jwtDecode } from 'jwt-decode';
44
import { z } from 'zod';
55
import { useQueryClient } from '@tanstack/react-query';
6-
import type { SignInSuccessResponse } from '@/modules/worker/services/sign-in/types';
76
import { browserAuthProvider } from '@/shared/contexts/browser-auth-provider';
87
import {
98
ModalType,
109
useModalStore,
1110
} from '@/shared/components/ui/modal/modal.store';
11+
import { type AuthTokensSuccessResponse } from '@/shared/schemas';
1212

1313
const web3userDataSchema = z.object({
1414
userId: z.number(),
@@ -25,15 +25,15 @@ export interface Web3AuthenticatedUserContextType {
2525
user: Web3UserData;
2626
status: AuthStatus;
2727
signOut: (throwExpirationModal?: boolean) => void;
28-
signIn: (singIsSuccess: SignInSuccessResponse) => void;
28+
signIn: (singIsSuccess: AuthTokensSuccessResponse) => void;
2929
updateUserData: (updateUserDataPayload: Partial<Web3UserData>) => void;
3030
}
3131

3232
interface Web3UnauthenticatedUserContextType {
3333
user: null;
3434
status: AuthStatus;
3535
signOut: (throwExpirationModal?: boolean) => void;
36-
signIn: (singIsSuccess: SignInSuccessResponse) => void;
36+
signIn: (singIsSuccess: AuthTokensSuccessResponse) => void;
3737
}
3838

3939
export const Web3AuthContext = createContext<
@@ -97,7 +97,7 @@ export function Web3AuthProvider({ children }: { children: React.ReactNode }) {
9797
}
9898
};
9999

100-
const signIn = (singIsSuccess: SignInSuccessResponse) => {
100+
const signIn = (singIsSuccess: AuthTokensSuccessResponse) => {
101101
browserAuthProvider.signIn(singIsSuccess, 'web3');
102102
handleSignIn();
103103
};

packages/apps/human-app/frontend/src/modules/auth/context/auth-context.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { useState, createContext, useEffect } from 'react';
33
import { jwtDecode } from 'jwt-decode';
44
import { z } from 'zod';
55
import { useQueryClient } from '@tanstack/react-query';
6-
import type { SignInSuccessResponse } from '@/modules/worker/services/sign-in/types';
76
import { browserAuthProvider } from '@/shared/contexts/browser-auth-provider';
87
import {
98
ModalType,
109
useModalStore,
1110
} from '@/shared/components/ui/modal/modal.store';
11+
import { type AuthTokensSuccessResponse } from '@/shared/schemas';
1212

1313
const extendableUserDataSchema = z.object({
1414
site_key: z.string().optional().nullable(),
@@ -35,15 +35,15 @@ export interface AuthenticatedUserContextType {
3535
user: UserData;
3636
status: AuthStatus;
3737
signOut: (throwExpirationModal?: boolean) => void;
38-
signIn: (singIsSuccess: SignInSuccessResponse) => void;
38+
signIn: (singIsSuccess: AuthTokensSuccessResponse) => void;
3939
updateUserData: (updateUserDataPayload: UpdateUserDataPayload) => void;
4040
}
4141

4242
interface UnauthenticatedUserContextType {
4343
user: null;
4444
status: AuthStatus;
4545
signOut: (throwExpirationModal?: boolean) => void;
46-
signIn: (singIsSuccess: SignInSuccessResponse) => void;
46+
signIn: (singIsSuccess: AuthTokensSuccessResponse) => void;
4747
}
4848

4949
export const AuthContext = createContext<
@@ -112,7 +112,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
112112
}
113113
};
114114

115-
const signIn = (singIsSuccess: SignInSuccessResponse) => {
115+
const signIn = (singIsSuccess: AuthTokensSuccessResponse) => {
116116
browserAuthProvider.signIn(singIsSuccess, 'web2');
117117
handleSignIn();
118118
};

packages/apps/human-app/frontend/src/modules/worker/services/sign-in/schema.ts renamed to packages/apps/human-app/frontend/src/modules/signin/worker/schemas.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,4 @@ export const signInDtoSchema = z.object({
1111
h_captcha_token: z.string().min(1, t('validation.captcha')).default('token'),
1212
});
1313

14-
export const signInSuccessResponseSchema = z.object({
15-
// eslint-disable-next-line camelcase -- data from api
16-
access_token: z.string(),
17-
// eslint-disable-next-line camelcase -- data from api
18-
refresh_token: z.string(),
19-
});
14+
export type SignInDto = z.infer<typeof signInDtoSchema>;

packages/apps/human-app/frontend/src/modules/signin/worker/sign-in-form.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ import { Input } from '@/shared/components/data-entry/input';
88
import { Button } from '@/shared/components/ui/button';
99
import { Password } from '@/shared/components/data-entry/password/password';
1010
import { routerPaths } from '@/router/router-paths';
11-
import { type SignInDto } from '@/modules/worker/services/sign-in/types';
12-
import { signInDtoSchema } from '@/modules/worker/services/sign-in/schema';
1311
import { useResetMutationErrors } from '@/shared/hooks/use-reset-mutation-errors';
1412
import { HCaptchaForm } from '@/shared/components/hcaptcha';
13+
import { type SignInDto, signInDtoSchema } from './schemas';
1514

1615
interface SignInFormProps {
1716
onSubmit: (data: SignInDto) => void;

packages/apps/human-app/frontend/src/modules/worker/services/sign-in/sign-in.ts renamed to packages/apps/human-app/frontend/src/modules/signin/worker/use-sign-in-mutation.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import { apiClient } from '@/api/api-client';
44
import { apiPaths } from '@/api/api-paths';
55
import { routerPaths } from '@/router/router-paths';
66
import { useAuth } from '@/modules/auth/hooks/use-auth';
7-
import { type SignInDto } from './types';
8-
import { signInSuccessResponseSchema } from './schema';
7+
import { authTokensSuccessResponseSchema } from '@/shared/schemas';
8+
import { type SignInDto } from './schemas';
99

1010
function signInMutationFn(data: SignInDto) {
1111
return apiClient(apiPaths.worker.signIn.path, {
12-
successSchema: signInSuccessResponseSchema,
12+
successSchema: authTokensSuccessResponseSchema,
1313
options: {
1414
method: 'POST',
1515
body: JSON.stringify(data),

packages/apps/human-app/frontend/src/modules/signin/worker/use-sign-in.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useEffect } from 'react';
2-
import { useSignInMutation } from '@/modules/worker/services/sign-in/sign-in';
32
import { browserAuthProvider } from '@/shared/contexts/browser-auth-provider';
4-
import { type SignInDto } from '@/modules/worker/services/sign-in/types';
3+
import { useSignInMutation } from './use-sign-in-mutation';
4+
import { type SignInDto } from './schemas';
55

66
export function useSignIn() {
77
const {

packages/apps/human-app/frontend/src/modules/worker/services/sign-up.ts renamed to packages/apps/human-app/frontend/src/modules/signup/worker/hooks/use-sign-up-mutation.ts

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,10 @@
11
import { z } from 'zod';
22
import { useMutation, useQueryClient } from '@tanstack/react-query';
33
import { useNavigate } from 'react-router-dom';
4-
import { t } from 'i18next';
54
import { apiClient } from '@/api/api-client';
65
import { apiPaths } from '@/api/api-paths';
76
import { routerPaths } from '@/router/router-paths';
8-
9-
export const signUpDtoSchema = z
10-
.object({
11-
email: z.string().email(t('validation.invalidEmail')),
12-
// eslint-disable-next-line camelcase -- export vite config
13-
h_captcha_token: z
14-
.string()
15-
.min(1, t('validation.captcha'))
16-
.default('token'),
17-
})
18-
.and(
19-
z
20-
.object({
21-
password: z
22-
.string()
23-
.min(8, t('validation.min'))
24-
.max(50, t('validation.max', { count: 50 })),
25-
confirmPassword: z
26-
.string()
27-
.min(1, t('validation.required'))
28-
.max(50, t('validation.max', { count: 50 })),
29-
})
30-
.refine(({ password, confirmPassword }) => confirmPassword === password, {
31-
message: t('validation.passwordMismatch'),
32-
path: ['confirmPassword'],
33-
})
34-
);
35-
36-
export type SignUpDto = z.infer<typeof signUpDtoSchema>;
7+
import { type SignUpDto } from '../schema';
378

389
const signUpSuccessResponseSchema = z.unknown();
3910

packages/apps/human-app/frontend/src/modules/signup/worker/hooks/use-sign-up-worker.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { useEffect } from 'react';
22
import omit from 'lodash/omit';
33
import { browserAuthProvider } from '@/shared/contexts/browser-auth-provider';
4-
import type { SignUpDto } from '@/modules/worker/services/sign-up';
5-
import { useSignUpMutation } from '@/modules/worker/services/sign-up';
4+
import { type SignUpDto } from '../schema';
5+
import { useSignUpMutation } from './use-sign-up-mutation';
66

77
export function useSignUpWorker() {
88
const {

0 commit comments

Comments
 (0)