React app
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

120 行
4.2 KiB

  1. import React, { useState } from "react";
  2. import styles from './Auth.module.scss';
  3. import { ReactComponent as LogoIcon } from '../../assets/icons/anamnesis.svg';
  4. import { ReactComponent as EyeOffIcon } from '../../assets/icons/eye-off-outline.svg';
  5. import { ReactComponent as EyeIcon } from '../../assets/icons/eye-outline.svg';
  6. import { NavLink } from "react-router-dom";
  7. import axios from 'axios';
  8. import { SERVER_URL } from "../../App";
  9. import { authenticateCredentials } from "./Login";
  10. const registerUser = async (name: string, username: string, password: string) => {
  11. return await axios.post(SERVER_URL + '/auth/register/', {
  12. name: name,
  13. email: username,
  14. password: password
  15. });
  16. }
  17. const requestVerification = async (email: string) => {
  18. return await axios.post(SERVER_URL + '/auth/request-verification/', {
  19. email: email,
  20. });
  21. }
  22. const verifyUser = async (email: string, otp: string) => {
  23. return await axios.post(SERVER_URL + '/auth/verify-user/', {
  24. email: email,
  25. otp: otp
  26. });
  27. }
  28. export const Signup: React.FC = () => {
  29. const [showPassowrd, setShowPassword] = useState<boolean>(true);
  30. const [fullName, setFullName] = useState<string>('');
  31. const [userName, setUsername] = useState<string>('');
  32. const [password, setPassword] = useState<string>('');
  33. const [otp, setOtp] = useState<string>('');
  34. const [viewState, setViewState] = useState<'REGISTER' | 'VERIFY'>('REGISTER');
  35. function register() {
  36. registerUser(fullName, userName, password).then(() => {
  37. window.alert("Registered your email, please wait while we redirect you to verify...");
  38. setTimeout(() => {
  39. requestVerification(userName).then(() => {
  40. setViewState('VERIFY');
  41. }, () => {
  42. window.alert("Couldn't send Verfication request");
  43. });
  44. }, 1000);
  45. }, (e) => {
  46. window.alert(e.toString());
  47. });
  48. }
  49. function verify() {
  50. verifyUser(userName, otp).then(() => {
  51. window.alert("Verified user!, Logging you in....");
  52. authenticateCredentials(userName, password).then((response: any) => {
  53. localStorage.anamnesisToken = response.data.token;
  54. window.location.assign('/home');
  55. }, () => {
  56. window.alert("Please check your credentials");
  57. });
  58. }, () => window.alert("Failed Verification, please try again"))
  59. }
  60. return <section className="page">
  61. <header className={styles.pageHeader}>
  62. <LogoIcon />
  63. </header>
  64. { viewState === 'REGISTER' && <section className={styles.form}>
  65. <div className={styles.inputHolder}>
  66. <input defaultValue={fullName} onInput={(e) => setFullName(e.currentTarget.value)}
  67. type="text" placeholder="Full Name" />
  68. </div>
  69. <div className={styles.inputHolder}>
  70. <input defaultValue={userName} onInput={(e) => setUsername(e.currentTarget.value)}
  71. type="email" placeholder="Email ID" />
  72. </div>
  73. <div className={styles.inputHolder}>
  74. <input defaultValue={password} onInput={(e) => setPassword(e.currentTarget.value)}
  75. type={ showPassowrd ? "text" : "password" } placeholder="Password" />
  76. <button className={styles.rightButton} onClick={() => setShowPassword(!showPassowrd)}>
  77. { showPassowrd ? <EyeOffIcon /> : <EyeIcon /> }
  78. </button>
  79. </div>
  80. <button className={styles.submitButton} onClick={() => register()}>
  81. Next Step (Verification)
  82. </button>
  83. <NavLink className={styles.otherLinks} to="/login"> Login? </NavLink>
  84. </section> }
  85. { viewState === 'VERIFY' && <section className={styles.form}>
  86. <div className={styles.inputHolder}>
  87. <input defaultValue={otp} onInput={(e) => setOtp(e.currentTarget.value)}
  88. type="tel" placeholder="Enter OTP sent to your Mail ID" />
  89. </div>
  90. <button className={styles.submitButton} onClick={() => verify()}>
  91. Verify
  92. </button>
  93. </section> }
  94. </section>
  95. };