|
@@ -10,86 +10,82 @@ import { get, post } from "../../../request";
|
|
|
import store from "../../../store";
|
|
import store from "../../../store";
|
|
|
|
|
|
|
|
interface IFormData {
|
|
interface IFormData {
|
|
|
- email: string;
|
|
|
|
|
- password: string;
|
|
|
|
|
|
|
+ email: string;
|
|
|
|
|
+ password: string;
|
|
|
}
|
|
}
|
|
|
interface ISignInResponse {
|
|
interface ISignInResponse {
|
|
|
- ok: boolean;
|
|
|
|
|
- message: string;
|
|
|
|
|
- data: string;
|
|
|
|
|
|
|
+ ok: boolean;
|
|
|
|
|
+ message: string;
|
|
|
|
|
+ data: string;
|
|
|
}
|
|
}
|
|
|
interface IUserResponse {
|
|
interface IUserResponse {
|
|
|
- ok: boolean;
|
|
|
|
|
- message: string;
|
|
|
|
|
- data: IUser;
|
|
|
|
|
|
|
+ ok: boolean;
|
|
|
|
|
+ message: string;
|
|
|
|
|
+ data: IUser;
|
|
|
}
|
|
}
|
|
|
interface ISignInRequest {
|
|
interface ISignInRequest {
|
|
|
- username: string;
|
|
|
|
|
- password: string;
|
|
|
|
|
|
|
+ username: string;
|
|
|
|
|
+ password: string;
|
|
|
}
|
|
}
|
|
|
const Widget = () => {
|
|
const Widget = () => {
|
|
|
- const intl = useIntl();
|
|
|
|
|
- const dispatch = useAppDispatch();
|
|
|
|
|
- const navigate = useNavigate();
|
|
|
|
|
|
|
+ const intl = useIntl();
|
|
|
|
|
+ const dispatch = useAppDispatch();
|
|
|
|
|
+ const navigate = useNavigate();
|
|
|
|
|
|
|
|
- return (
|
|
|
|
|
- <ProForm<IFormData>
|
|
|
|
|
- onFinish={async (values: IFormData) => {
|
|
|
|
|
- // TODO
|
|
|
|
|
- console.log(values);
|
|
|
|
|
- const user = {
|
|
|
|
|
- username: values.email,
|
|
|
|
|
- password: values.password,
|
|
|
|
|
- };
|
|
|
|
|
- const signin = await post<ISignInRequest, ISignInResponse>(
|
|
|
|
|
- "/v2/auth/signin",
|
|
|
|
|
- user
|
|
|
|
|
- );
|
|
|
|
|
- if (signin.ok) {
|
|
|
|
|
- console.log("token", signin.data);
|
|
|
|
|
- localStorage.setItem("token", signin.data);
|
|
|
|
|
- get<IUserResponse>("/v2/auth/current").then((json) => {
|
|
|
|
|
- if (json.ok) {
|
|
|
|
|
- dispatch(signIn([json.data, signin.data]));
|
|
|
|
|
- navigate(TO_HOME);
|
|
|
|
|
- } else {
|
|
|
|
|
- console.error(json.message);
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
- message.success(
|
|
|
|
|
- intl.formatMessage({ id: "flashes.success" })
|
|
|
|
|
- );
|
|
|
|
|
- } else {
|
|
|
|
|
- message.error(signin.message);
|
|
|
|
|
- }
|
|
|
|
|
- }}
|
|
|
|
|
- >
|
|
|
|
|
- <ProForm.Group>
|
|
|
|
|
- <ProFormText
|
|
|
|
|
- width="md"
|
|
|
|
|
- name="email"
|
|
|
|
|
- required
|
|
|
|
|
- label={intl.formatMessage({
|
|
|
|
|
- id: "forms.fields.email.label",
|
|
|
|
|
- })}
|
|
|
|
|
- rules={[
|
|
|
|
|
- { required: true, type: "email", max: 255, min: 6 },
|
|
|
|
|
- ]}
|
|
|
|
|
- />
|
|
|
|
|
- </ProForm.Group>
|
|
|
|
|
- <ProForm.Group>
|
|
|
|
|
- <ProFormText
|
|
|
|
|
- width="md"
|
|
|
|
|
- name="password"
|
|
|
|
|
- required
|
|
|
|
|
- label={intl.formatMessage({
|
|
|
|
|
- id: "forms.fields.password.label",
|
|
|
|
|
- })}
|
|
|
|
|
- rules={[{ required: true, max: 32, min: 4 }]}
|
|
|
|
|
- />
|
|
|
|
|
- </ProForm.Group>
|
|
|
|
|
- </ProForm>
|
|
|
|
|
- );
|
|
|
|
|
|
|
+ return (
|
|
|
|
|
+ <ProForm<IFormData>
|
|
|
|
|
+ onFinish={async (values: IFormData) => {
|
|
|
|
|
+ // TODO
|
|
|
|
|
+ console.log(values);
|
|
|
|
|
+ const user = {
|
|
|
|
|
+ username: values.email,
|
|
|
|
|
+ password: values.password,
|
|
|
|
|
+ };
|
|
|
|
|
+ const signin = await post<ISignInRequest, ISignInResponse>(
|
|
|
|
|
+ "/v2/auth/signin",
|
|
|
|
|
+ user
|
|
|
|
|
+ );
|
|
|
|
|
+ if (signin.ok) {
|
|
|
|
|
+ console.log("token", signin.data);
|
|
|
|
|
+ localStorage.setItem("token", signin.data);
|
|
|
|
|
+ get<IUserResponse>("/v2/auth/current").then((json) => {
|
|
|
|
|
+ if (json.ok) {
|
|
|
|
|
+ dispatch(signIn([json.data, signin.data]));
|
|
|
|
|
+ navigate(TO_HOME);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ console.error(json.message);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ message.success(intl.formatMessage({ id: "flashes.success" }));
|
|
|
|
|
+ } else {
|
|
|
|
|
+ message.error(signin.message);
|
|
|
|
|
+ }
|
|
|
|
|
+ }}
|
|
|
|
|
+ >
|
|
|
|
|
+ <ProForm.Group>
|
|
|
|
|
+ <ProFormText
|
|
|
|
|
+ width="md"
|
|
|
|
|
+ name="email"
|
|
|
|
|
+ required
|
|
|
|
|
+ label={intl.formatMessage({
|
|
|
|
|
+ id: "forms.fields.email.label",
|
|
|
|
|
+ })}
|
|
|
|
|
+ rules={[{ required: true, max: 255, min: 6 }]}
|
|
|
|
|
+ />
|
|
|
|
|
+ </ProForm.Group>
|
|
|
|
|
+ <ProForm.Group>
|
|
|
|
|
+ <ProFormText
|
|
|
|
|
+ width="md"
|
|
|
|
|
+ name="password"
|
|
|
|
|
+ required
|
|
|
|
|
+ label={intl.formatMessage({
|
|
|
|
|
+ id: "forms.fields.password.label",
|
|
|
|
|
+ })}
|
|
|
|
|
+ rules={[{ required: true, max: 32, min: 4 }]}
|
|
|
|
|
+ />
|
|
|
|
|
+ </ProForm.Group>
|
|
|
|
|
+ </ProForm>
|
|
|
|
|
+ );
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
export default Widget;
|
|
export default Widget;
|