login-hooks.ts


Overview

login-hooks.ts provides a collection of React hooks designed to manage user authentication workflows within a React application. These hooks encapsulate the login, logout, registration processes, and retrieval of login channels, while integrating with backend services and handling authorization tokens. The file leverages @tanstack/react-query for asynchronous data fetching and mutation management, antd for form handling, and i18next for internationalized messages.

The primary purpose of this file is to abstract the complexities of authentication logic into reusable hooks that can be easily integrated into React components, promoting modularity and maintainability.


Detailed Explanation of Exports

Interfaces

ILoginRequestBody

export interface ILoginRequestBody {
  email: string;
  password: string;
}

IRegisterRequestBody

export interface IRegisterRequestBody extends ILoginRequestBody {
  nickname: string;
}

ILoginChannel

export interface ILoginChannel {
  channel: string;
  display_name: string;
  icon: string;
}

Hooks


useLoginChannels

export const useLoginChannels = () => { ... }
const { channels, loading } = useLoginChannels();
if (loading) return <Spinner />;
return (
  <ul>
    {channels.map(ch => (
      <li key={ch.channel}>{ch.display_name}</li>
    ))}
  </ul>
);

useLoginWithChannel

export const useLoginWithChannel = () => { ... }
const { loading, login } = useLoginWithChannel();
<button disabled={loading} onClick={() => login('google')}>
  Login with Google
</button>;

useLogin

export const useLogin = () => { ... }
const { data, loading, login } = useLogin();

const handleSubmit = async (values: ILoginRequestBody) => {
  const code = await login(values);
  if (code === 0) {
    // Successfully logged in
  } else {
    // Handle error
  }
};

useRegister

export const useRegister = () => { ... }
const { data, loading, register } = useRegister();

const onRegister = async (values: IRegisterRequestBody) => {
  const code = await register(values);
  if (code === 0) {
    // Registration successful
  }
};

useLogout

export const useLogout = () => { ... }
const { data, loading, logout } = useLogout();

const handleLogout = async () => {
  const code = await logout();
  if (code === 0) {
    // User logged out successfully
  }
};

useHandleSubmittable

export const useHandleSubmittable = (form: FormInstance) => { ... }
const [form] = Form.useForm();
const { submittable } = useHandleSubmittable(form);

return (
  <Form form={form}>
    {/* form items */}
    <Button type="primary" disabled={!submittable}>Submit</Button>
  </Form>
);

Important Implementation Details and Algorithms


Interaction with Other Parts of the System

This file acts as the business logic layer between UI components (login forms, registration forms, logout buttons) and backend services, providing clean hooks to be consumed by UI components.


Mermaid Diagram: Flowchart of Main Functions and Their Relationships

flowchart TD
  A[useLoginChannels] -->|fetches| B[getLoginChannels API]
  C[useLoginWithChannel] -->|initiates| D[loginWithChannel Service]
  E[useLogin] -->|calls| F[userService.login API]
  E -->|stores tokens| G[authorizationUtil.setItems]
  H[useRegister] -->|calls| I[userService.register API]
  H -->|shows messages| J[message UI]
  K[useLogout] -->|calls| L[userService.logout API]
  K -->|clears auth| M[authorizationUtil.removeAll]
  K -->|redirects| N[redirectToLogin]
  O[useHandleSubmittable] -->|watches| P[AntD Form]
  P -->|validateFields| Q[form validation]

  style A fill:#bbdefb,stroke:#1e88e5
  style C fill:#bbdefb,stroke:#1e88e5
  style E fill:#bbdefb,stroke:#1e88e5
  style H fill:#bbdefb,stroke:#1e88e5
  style K fill:#bbdefb,stroke:#1e88e5
  style O fill:#c8e6c9,stroke:#388e3c

Summary

login-hooks.ts is a utility file that exports React hooks to handle authentication-related actions in a modular and maintainable way. It encapsulates API communication, token management, UI feedback, and form validation into easy-to-use hooks that can be consumed by React components in the application. Its integration with React Query, Ant Design, and i18next makes it a robust and internationalized solution for login workflows.