Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions apps/web/src/features/auth/login-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import { validateRedirectUrl } from '@/utils/utils';
import { z } from '@/utils/zod';

const formSchema = z.object({
email: z.string().email(),
password: z.string().min(6),
identifier: z.string().min(5),
password: z.string().min(8).max(256),
rememberMe: z.boolean().default(false),
});

Expand All @@ -43,7 +43,7 @@ const LoginForm = () => {
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
email: '',
identifier: '',
password: '',
rememberMe: false,
},
Expand All @@ -64,11 +64,15 @@ const LoginForm = () => {
});

const handleFormSubmit = (data: z.infer<typeof formSchema>) => {
// Determine if identifier is email or username
const isEmail = data.identifier.includes('@');

const args = isEmail
? { email: data.identifier, password: data.password }
: { username: data.identifier, password: data.password };

mutationLogin.mutate({
args: {
email: data.email,
password: data.password,
},
args,
captcha: {
captcha: String(captchaRef.current?.getResponse()),
},
Expand All @@ -83,14 +87,14 @@ const LoginForm = () => {
>
<FormField
control={form.control}
name="email"
name="identifier"
render={({ field }) => (
<FormItem>
<FormLabel>Email</FormLabel>
<FormLabel>Ваш юзернейм або пошта</FormLabel>
<FormControl>
<Input
type="email"
placeholder="Введіть ваш email"
type="text"
placeholder="Введіть ваш юзернейм або пошту"
{...field}
/>
</FormControl>
Expand Down Expand Up @@ -173,4 +177,4 @@ const LoginForm = () => {
);
};

export default LoginForm;
export default LoginForm;
5 changes: 3 additions & 2 deletions packages/client/modules/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
CodeArgs,
ComfirmResetArgs,
EmailArgs,
LoginArgs,
EmailLoginArgs,
PaginationArgs,
ProviderUrlResponse,
SignupArgs,
Expand All @@ -16,6 +16,7 @@ import {
TokenRequestArgs,
TokenRequestResponse,
TokenResponse,
UsernameLoginArgs,
UserResponse,
} from '../types';
import { BaseModule } from './base';
Expand All @@ -25,7 +26,7 @@ export class AuthModule extends BaseModule {
* Create a user session with credentials
*/
public async createUserSession(
args: LoginArgs,
args: EmailLoginArgs | UsernameLoginArgs,
{ captcha }: CaptchaArgs,
options?: BaseRequestOptionsArgs,
): Promise<TokenResponse> {
Expand Down
12 changes: 10 additions & 2 deletions packages/client/types/auth.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import { ClientResponse } from './client';

/**
* Login request parameters
* Login request parameters for email-based
*/
export interface LoginArgs {
export interface EmailLoginArgs {
email: string;
password: string;
}

/**
* Login request parameters for username-based
*/
export interface UsernameLoginArgs {
username: string;
password: string;
}

/**
* Signup request parameters
*/
Expand Down
5 changes: 3 additions & 2 deletions packages/react/client/hooks/api/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import {
CodeArgs,
ComfirmResetArgs,
EmailArgs,
LoginArgs,
EmailLoginArgs,
ProviderUrlResponse,
SignupArgs,
TokenArgs,
TokenProceedArgs,
TokenRequestArgs,
UsernameLoginArgs,
} from '@hikka/client';

import {
Expand Down Expand Up @@ -103,7 +104,7 @@ export const useCreatePasswordResetRequest = createMutation({
export const useCreateUserSession = createMutation({
mutationFn: (
client,
{ args, captcha }: { args: LoginArgs; captcha: CaptchaArgs },
{ args, captcha }: { args: EmailLoginArgs | UsernameLoginArgs; captcha: CaptchaArgs },
) => client.auth.createUserSession(args, captcha),
invalidateQueries: () => [queryKeys.auth.tokenInfo(), queryKeys.user.me()],
});
Expand Down