/
Login.tsx
185 lines (168 loc) · 5.85 KB
/
Login.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import React, { FC, useState } from "react";
import { useNavigate, Link, useLocation } from "react-router-dom";
import { Authentication, instance } from "../api/api";
import { Card, Input, Button, Form } from "antd";
import "./Login.css";
import { useQueryClient, useMutation } from "@tanstack/react-query";
import { toast } from "react-toastify";
import Cookies from "universal-cookie";
import posthog from "posthog-js";
import LoadingSpinner from "../components/LoadingSpinner";
import { QueryErrors } from "../types/error-response-types";
import useGlobalStore from "../stores/useGlobalstore";
import Tooltip from "../components/base/Tooltip/Tooltip";
import Avatar from "../components/base/Avatar/Avatar";
import Dropdown from "../components/base/Dropdown/Dropdown";
const cookies = new Cookies();
interface LoginForm extends HTMLFormControlsCollection {
username: string;
password: string;
}
interface FormElements extends HTMLFormElement {
readonly elements: LoginForm;
}
interface LocationState {
redirectTo?: string;
}
interface LoginProps {
username?: string;
password?: string;
}
const Login: FC<LoginProps> = (props) => {
const [username, setUsername] = useState(props.username || "");
const [password, setPassword] = useState(props.password || "");
const [error, setError] = useState("");
const setUsernameToStore = useGlobalStore((state) => state.setUsername);
const queryClient = useQueryClient();
const location = useLocation();
const [isAuthenticated, setIsAuthenticated] = useState(false);
const navigate = useNavigate();
const handlePasswordChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setPassword(event.target.value);
};
const handleUserNameChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setUsername(event.target.value);
};
const redirectAfterLogin = () => {
navigate((location.state as LocationState).redirectTo!);
};
const isDemo = (import.meta as any).env.VITE_IS_DEMO === "true";
const mutation = useMutation(
(data: { username: string; password: string }) =>
isDemo
? Authentication.demo_login(username, password)
: Authentication.login(username, password),
{
onSuccess: (response) => {
setIsAuthenticated(true);
const { token, detail, user } = response;
setUsernameToStore(user.username);
if (import.meta.env.VITE_API_URL === "https://api.uselotus.io/") {
posthog.group("company", user.organization_id, {
organization_name: user.organization_name,
});
posthog.identify(
user.email, // distinct_id, required
{ organization_id: user.organization_id }, // $set, optional
{ username: user.username } // $set_once, optional
);
}
cookies.set("Token", token);
instance.defaults.headers.common.Authorization = `Token ${token}`;
queryClient.refetchQueries(["session"]);
redirectAfterLogin();
},
onError: (error: QueryErrors) => {
// setError(error.message);
if (error.response.status === 403) {
toast.error("Please login again.");
window.location.reload();
} else {
toast.error(error.response.data.detail);
}
},
}
);
const handleLogin = (event: React.FormEvent<FormElements>) => {
// const pwBitArray = sjcl.hash.sha256.hash(password);
// const hashedPassword = sjcl.codec.hex.fromBits(pwBitArray);
mutation.mutate({ username, password });
};
if (!isAuthenticated) {
return (
<div className="grid h-screen place-items-center">
<div className="space-y-4">
<Card
title="Login"
className="flex flex-col"
style={{
borderRadius: "0.5rem",
borderWidth: "2px",
borderColor: "#EAEAEB",
borderStyle: "solid",
}}
>
{/* <img src="../assets/images/logo_large.jpg" alt="logo" /> */}
<Form onFinish={handleLogin} name="normal_login">
<Form.Item>
<label htmlFor="username">Username or Email</label>
<Input
type="text"
name="username"
id="username"
value={username}
defaultValue="username123"
onChange={handleUserNameChange}
/>
</Form.Item>
<label htmlFor="password">Password</label>
<Form.Item>
<Input
type="password"
id="password"
name="password"
value={password}
defaultValue="password123"
onChange={handlePasswordChange}
/>
<div>
{error && <small className="text-danger">{error}</small>}
</div>
</Form.Item>
<Form.Item>
<Button htmlType="submit">Login</Button>
</Form.Item>
<Link
to="/reset-password"
className=" text-darkgold hover:text-black"
>
Forgot Password?
</Link>
</Form>
</Card>
{(import.meta.env.VITE_API_URL !== "https://api.uselotus.io/" ||
import.meta.env.IS_DEMO == "true") && (
<div>
<Button
type="primary"
className="w-full"
onClick={() => navigate("/register")}
>
Sign Up
</Button>
</div>
)}
</div>
{mutation.isLoading && <LoadingSpinner />}
</div>
);
}
return (
<div className="container mt-3">
<div className="grid h-screen place-items-center">
<LoadingSpinner />
</div>
</div>
);
};
export default Login;