-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add change email functionality
- Loading branch information
Showing
19 changed files
with
847 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { useFormState } from "react-use-form-state"; | ||
import React, { FC, useState } from "react"; | ||
import { Flex } from "reflexbox"; | ||
import axios from "axios"; | ||
|
||
import { getAxiosConfig } from "../../utils"; | ||
import { useMessage } from "../../hooks"; | ||
import { APIv2 } from "../../consts"; | ||
import { TextInput } from "../Input"; | ||
import Text, { H2 } from "../Text"; | ||
import { Button } from "../Button"; | ||
import { Col } from "../Layout"; | ||
import Icon from "../Icon"; | ||
|
||
const SettingsChangeEmail: FC = () => { | ||
const [loading, setLoading] = useState(false); | ||
const [message, setMessage] = useMessage(5000); | ||
const [formState, { password, email, label }] = useFormState<{ | ||
changeemailpass: string; | ||
changeemailaddress: string; | ||
}>(null, { | ||
withIds: true | ||
}); | ||
|
||
const onSubmit = async e => { | ||
e.preventDefault(); | ||
if (loading) return; | ||
setLoading(true); | ||
try { | ||
const res = await axios.post( | ||
APIv2.AuthChangeEmail, | ||
{ | ||
password: formState.values.changeemailpass, | ||
email: formState.values.changeemailaddress | ||
}, | ||
getAxiosConfig() | ||
); | ||
setMessage(res.data.message, "green"); | ||
} catch (error) { | ||
setMessage(error?.response?.data?.error || "Couldn't send email."); | ||
} | ||
setLoading(false); | ||
}; | ||
|
||
return ( | ||
<Col alignItems="flex-start" maxWidth="100%"> | ||
<H2 mb={4} bold> | ||
Change email address | ||
</H2> | ||
<Col alignItems="flex-start" onSubmit={onSubmit} width={1} as="form"> | ||
<Flex width={1} flexDirection={["column", "row"]}> | ||
<Col mr={[0, 2]} mb={[3, 0]} flex="0 0 auto"> | ||
<Text | ||
{...label("changeemailpass")} | ||
as="label" | ||
mb={[2, 3]} | ||
fontSize={[15, 16]} | ||
bold | ||
> | ||
Password: | ||
</Text> | ||
<TextInput | ||
{...password("changeemailpass")} | ||
placeholder="Password..." | ||
maxWidth="240px" | ||
required | ||
/> | ||
</Col> | ||
<Col ml={[0, 2]} flex="0 0 auto"> | ||
<Text | ||
{...label("changeemailaddress")} | ||
as="label" | ||
mb={[2, 3]} | ||
fontSize={[15, 16]} | ||
bold | ||
> | ||
New email address: | ||
</Text> | ||
<TextInput | ||
{...email("changeemailaddress")} | ||
placeholder="[email protected]" | ||
flex="1 1 auto" | ||
maxWidth="240px" | ||
/> | ||
</Col> | ||
</Flex> | ||
<Button type="submit" color="blue" mt={[24, 3]} disabled={loading}> | ||
<Icon name={loading ? "spinner" : "refresh"} mr={2} stroke="white" /> | ||
{loading ? "Sending..." : "Update"} | ||
</Button> | ||
</Col> | ||
<Text fontSize={15} color={message.color} mt={3}> | ||
{message.text} | ||
</Text> | ||
</Col> | ||
); | ||
}; | ||
|
||
export default SettingsChangeEmail; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import React, { useEffect } from "react"; | ||
import { Flex } from "reflexbox/styled-components"; | ||
import decode from "jwt-decode"; | ||
import { NextPage } from "next"; | ||
import cookie from "js-cookie"; | ||
|
||
import { useStoreActions } from "../store"; | ||
import AppWrapper from "../components/AppWrapper"; | ||
import { H2 } from "../components/Text"; | ||
import { TokenPayload } from "../types"; | ||
import Icon from "../components/Icon"; | ||
import { Colors } from "../consts"; | ||
import Footer from "../components/Footer"; | ||
|
||
interface Props { | ||
token?: string; | ||
} | ||
|
||
const VerifyEmail: NextPage<Props> = ({ token }) => { | ||
const addAuth = useStoreActions(s => s.auth.add); | ||
|
||
useEffect(() => { | ||
if (token) { | ||
cookie.set("token", token, { expires: 7 }); | ||
const decoded: TokenPayload = decode(token); | ||
addAuth(decoded); | ||
} | ||
}, []); | ||
|
||
return ( | ||
<AppWrapper> | ||
<Flex flex="1 1 100%" justifyContent="center" mt={4}> | ||
<Icon | ||
name={token ? "check" : "x"} | ||
size={26} | ||
stroke={token ? Colors.CheckIcon : Colors.TrashIcon} | ||
mr={3} | ||
mt={1} | ||
/> | ||
<H2 textAlign="center" normal> | ||
{token | ||
? "Email address verified successfully." | ||
: "Couldn't verify the email address."} | ||
</H2> | ||
</Flex> | ||
<Footer /> | ||
</AppWrapper> | ||
); | ||
}; | ||
|
||
VerifyEmail.getInitialProps = async ctx => { | ||
return { token: (ctx?.req as any)?.token }; | ||
}; | ||
|
||
export default VerifyEmail; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.