Source

ui/pages/SigninPage.tsx

  1. import { Meteor } from 'meteor/meteor';
  2. import { Roles } from 'meteor/alanning:roles';
  3. import React, { useState } from 'react';
  4. import { Redirect } from 'react-router-dom';
  5. import { Container, Form, Grid, Header, Message, Segment } from 'semantic-ui-react';
  6. import { ROLE } from '../../api/role/Role';
  7. import LandingNavBar from '../components/landing/LandingNavBar';
  8. import { COMPONENTIDS } from '../utilities/ComponentIDs';
  9. /**
  10. * SigninPage page overrides the form’s submit event and call Meteor’s loginWithPassword().
  11. * Authentication errors modify the component’s state to be displayed
  12. */
  13. const SigninPage: React.FC = () => {
  14. const [emailState, setEmail] = useState('');
  15. const [passwordState, setPassword] = useState('');
  16. const [errorState, setError] = useState('');
  17. const [redirectToRefererState, setRedirectToReferer] = useState(false);
  18. const instanceName = Meteor.settings.public.instanceName;
  19. /** Update the form controls each time the user interacts with them. */
  20. const handleChange = (e, { name, value }) => {
  21. // console.log('handleChange', name, value);
  22. switch (name) {
  23. case 'email':
  24. setEmail(value);
  25. break;
  26. case 'password':
  27. setPassword(value);
  28. break;
  29. default:
  30. // do nothing
  31. }
  32. };
  33. /** Handle SigninPage submission using Meteor's account mechanism. */
  34. const handleSubmit = () => {
  35. Meteor.loginWithPassword(emailState, passwordState, (err) => {
  36. if (err) {
  37. setError(err.message);
  38. } else {
  39. setError('');
  40. setRedirectToReferer(true);
  41. localStorage.setItem('logoutEvent', 'false');
  42. }
  43. });
  44. };
  45. /** Render the signin form. */
  46. const username = emailState;
  47. const userId = Meteor.userId();
  48. let pathname = '';
  49. if (Roles.userIsInRole(userId, [ROLE.ADMIN])) {
  50. pathname = `/${ROLE.ADMIN.toLowerCase()}/${username}/home`;
  51. } else if (Roles.userIsInRole(userId, [ROLE.ADVISOR])) {
  52. pathname = `/${ROLE.ADVISOR.toLowerCase()}/${username}/home`;
  53. } else if (Roles.userIsInRole(userId, [ROLE.FACULTY])) {
  54. pathname = `/${ROLE.FACULTY.toLowerCase()}/${username}/home`;
  55. } else if (Roles.userIsInRole(userId, [ROLE.STUDENT])) {
  56. pathname = `/${ROLE.STUDENT.toLowerCase()}/${username}/home`;
  57. } else if (Roles.userIsInRole(userId, [ROLE.ALUMNI])) {
  58. pathname = `/${ROLE.ALUMNI.toLowerCase()}/${username}/home`;
  59. }
  60. const { from } = { from: { pathname } };
  61. // if correct authentication, redirect to page instead of login screen
  62. if (redirectToRefererState) {
  63. return <Redirect to={from} />;
  64. }
  65. const containerStyle = { marginTop: 15 };
  66. // Otherwise return the Login form.
  67. return (
  68. <div>
  69. <LandingNavBar currentUser="" instanceName={instanceName} />
  70. <Container id="signin-page" style={containerStyle}>
  71. <Grid textAlign="center" verticalAlign="middle" centered columns={2}>
  72. <Grid.Column>
  73. <Header as="h2" textAlign="center">
  74. Login to your account
  75. </Header>
  76. <Form onSubmit={handleSubmit}>
  77. <Segment stacked>
  78. <Form.Input label="Email" id={COMPONENTIDS.SIGNIN_FORM_EMAIL} icon="user" iconPosition="left" name="email" type="email" placeholder="E-mail address" onChange={handleChange} />
  79. <Form.Input label="Password" id={COMPONENTIDS.SIGNIN_FORM_PASSWORD} icon="lock" iconPosition="left" name="password" placeholder="Password" type="password" onChange={handleChange} />
  80. <Form.Button id={COMPONENTIDS.SIGNIN_FORM_SUBMIT} content="Submit" />
  81. </Segment>
  82. </Form>
  83. {errorState === '' ? '' : <Message error header="Login was not successful" content={errorState} />}
  84. </Grid.Column>
  85. </Grid>
  86. </Container>
  87. </div>
  88. );
  89. };
  90. export default SigninPage;