2022-04-20 01:14:08 -04:00
|
|
|
import { Input, Button, useToasts } from "@geist-ui/core"
|
|
|
|
import Cookies from "js-cookie"
|
|
|
|
import { useState } from "react"
|
|
|
|
|
|
|
|
const Password = () => {
|
2022-04-20 04:49:34 -04:00
|
|
|
const [password, setPassword] = useState<string>("")
|
|
|
|
const [newPassword, setNewPassword] = useState<string>("")
|
|
|
|
const [confirmPassword, setConfirmPassword] = useState<string>("")
|
2022-04-20 01:14:08 -04:00
|
|
|
|
2022-04-20 04:49:34 -04:00
|
|
|
const { setToast } = useToasts()
|
2022-04-20 01:14:08 -04:00
|
|
|
|
2022-04-20 04:49:34 -04:00
|
|
|
const handlePasswordChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
setPassword(e.target.value)
|
|
|
|
}
|
2022-04-20 01:14:08 -04:00
|
|
|
|
2022-04-20 04:49:34 -04:00
|
|
|
const handleNewPasswordChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
setNewPassword(e.target.value)
|
|
|
|
}
|
2022-04-20 01:14:08 -04:00
|
|
|
|
2022-04-20 04:49:34 -04:00
|
|
|
const handleConfirmPasswordChange = (
|
|
|
|
e: React.ChangeEvent<HTMLInputElement>
|
|
|
|
) => {
|
|
|
|
setConfirmPassword(e.target.value)
|
|
|
|
}
|
2022-04-20 01:14:08 -04:00
|
|
|
|
2022-04-20 04:49:34 -04:00
|
|
|
const onSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
|
|
|
|
e.preventDefault()
|
|
|
|
if (!password || !newPassword || !confirmPassword) {
|
|
|
|
setToast({
|
|
|
|
text: "Please fill out all fields",
|
|
|
|
type: "error"
|
|
|
|
})
|
|
|
|
}
|
2022-04-20 01:14:08 -04:00
|
|
|
|
2022-04-20 04:49:34 -04:00
|
|
|
if (newPassword !== confirmPassword) {
|
|
|
|
setToast({
|
|
|
|
text: "New password and confirm password do not match",
|
|
|
|
type: "error"
|
|
|
|
})
|
|
|
|
}
|
2022-04-20 01:14:08 -04:00
|
|
|
|
2022-04-20 04:49:34 -04:00
|
|
|
const res = await fetch("/server-api/auth/change-password", {
|
|
|
|
method: "PUT",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Authorization: `Bearer ${Cookies.get("drift-token")}`
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
oldPassword: password,
|
|
|
|
newPassword
|
|
|
|
})
|
|
|
|
})
|
2022-04-20 01:14:08 -04:00
|
|
|
|
2022-04-20 04:49:34 -04:00
|
|
|
if (res.status === 200) {
|
|
|
|
setToast({
|
|
|
|
text: "Password updated successfully",
|
|
|
|
type: "success"
|
|
|
|
})
|
|
|
|
setPassword("")
|
|
|
|
setNewPassword("")
|
|
|
|
setConfirmPassword("")
|
|
|
|
} else {
|
|
|
|
const data = await res.json()
|
2022-04-20 01:14:08 -04:00
|
|
|
|
2022-04-20 04:49:34 -04:00
|
|
|
setToast({
|
|
|
|
text: data.error ?? "Failed to update password",
|
|
|
|
type: "error"
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-04-20 01:14:08 -04:00
|
|
|
|
2022-04-20 04:49:34 -04:00
|
|
|
return (
|
|
|
|
<form
|
|
|
|
style={{
|
|
|
|
display: "flex",
|
|
|
|
flexDirection: "column",
|
|
|
|
gap: "var(--gap)",
|
|
|
|
maxWidth: "300px"
|
|
|
|
}}
|
|
|
|
onSubmit={onSubmit}
|
|
|
|
>
|
|
|
|
<div>
|
|
|
|
<label htmlFor="current-password">Current password</label>
|
|
|
|
<Input
|
|
|
|
onChange={handlePasswordChange}
|
|
|
|
minLength={6}
|
|
|
|
maxLength={128}
|
|
|
|
value={password}
|
|
|
|
id="current-password"
|
|
|
|
htmlType="password"
|
|
|
|
required
|
|
|
|
autoComplete="current-password"
|
|
|
|
placeholder="Current Password"
|
|
|
|
width={"100%"}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<label htmlFor="new-password">New password</label>
|
|
|
|
<Input
|
|
|
|
onChange={handleNewPasswordChange}
|
|
|
|
minLength={6}
|
|
|
|
maxLength={128}
|
|
|
|
value={newPassword}
|
|
|
|
id="new-password"
|
|
|
|
htmlType="password"
|
|
|
|
required
|
|
|
|
autoComplete="new-password"
|
|
|
|
placeholder="New Password"
|
|
|
|
width={"100%"}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<label htmlFor="confirm-password">Confirm password</label>
|
|
|
|
<Input
|
|
|
|
onChange={handleConfirmPasswordChange}
|
|
|
|
minLength={6}
|
|
|
|
maxLength={128}
|
|
|
|
value={confirmPassword}
|
|
|
|
id="confirm-password"
|
|
|
|
htmlType="password"
|
|
|
|
required
|
|
|
|
autoComplete="confirm-password"
|
|
|
|
placeholder="Confirm Password"
|
|
|
|
width={"100%"}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<Button htmlType="submit" auto>
|
|
|
|
Change Password
|
|
|
|
</Button>
|
|
|
|
</form>
|
|
|
|
)
|
2022-04-20 01:14:08 -04:00
|
|
|
}
|
|
|
|
|
2022-04-20 04:49:34 -04:00
|
|
|
export default Password
|