1+ import React , { useState } from 'react' ;
2+ import Avatar from '@material-ui/core/Avatar' ;
3+ import Button from '@material-ui/core/Button' ;
4+ import CssBaseline from '@material-ui/core/CssBaseline' ;
5+ import TextField from '@material-ui/core/TextField' ;
6+ import FormControlLabel from '@material-ui/core/FormControlLabel' ;
7+ import Checkbox from '@material-ui/core/Checkbox' ;
8+ import Link from '@material-ui/core/Link' ;
9+ import Grid from '@material-ui/core/Grid' ;
10+ import Box from '@material-ui/core/Box' ;
11+ import LockOutlinedIcon from '@material-ui/icons/LockOutlined' ;
12+ import Typography from '@material-ui/core/Typography' ;
13+ import { makeStyles } from '@material-ui/core/styles' ;
14+ import Container from '@material-ui/core/Container' ;
15+ import { useHistory } from "react-router-dom"
16+
17+ // context
18+ import { useAuth } from '../context/AuthContext'
19+
20+ function Copyright ( ) {
21+ return (
22+ < Typography variant = "body2" color = "textSecondary" align = "center" >
23+ { 'Copyright © ' }
24+ < Link color = "inherit" href = "https://material-ui.com/" >
25+ Your Website
26+ </ Link > { ' ' }
27+ { new Date ( ) . getFullYear ( ) }
28+ { '.' }
29+ </ Typography >
30+ ) ;
31+ }
32+
33+ const useStyles = makeStyles ( ( theme ) => ( {
34+ paper : {
35+ marginTop : theme . spacing ( 8 ) ,
36+ display : 'flex' ,
37+ flexDirection : 'column' ,
38+ alignItems : 'center' ,
39+ } ,
40+ avatar : {
41+ margin : theme . spacing ( 1 ) ,
42+ backgroundColor : theme . palette . secondary . main ,
43+ } ,
44+ form : {
45+ width : '100%' , // Fix IE 11 issue.
46+ marginTop : theme . spacing ( 1 ) ,
47+ } ,
48+ submit : {
49+ margin : theme . spacing ( 3 , 0 , 2 ) ,
50+ } ,
51+ } ) ) ;
52+
53+ export default function SignIn ( ) {
54+ const classes = useStyles ( ) ;
55+ const { login } = useAuth ( )
56+ const history = useHistory ( )
57+
58+ async function handleSubmit ( e ) {
59+ e . preventDefault ( )
60+
61+ const signinFields = {
62+ email : e . target . email . value ,
63+ password : e . target . password . value
64+ }
65+
66+ try {
67+ await login ( signinFields . email , signinFields . password )
68+ history . push ( "/" )
69+ } catch {
70+ console . log ( "Failed to log in" )
71+ }
72+ }
73+
74+ return (
75+ < Container component = "main" maxWidth = "xs" >
76+ < CssBaseline />
77+ < div className = { classes . paper } >
78+ < Avatar className = { classes . avatar } >
79+ < LockOutlinedIcon />
80+ </ Avatar >
81+ < Typography component = "h1" variant = "h5" >
82+ Sign in
83+ </ Typography >
84+ < form className = { classes . form } onSubmit = { handleSubmit } noValidate >
85+ < TextField
86+ variant = "outlined"
87+ margin = "normal"
88+ required
89+ fullWidth
90+ id = "email"
91+ label = "Email Address"
92+ name = "email"
93+ autoComplete = "email"
94+ autoFocus
95+ />
96+ < TextField
97+ variant = "outlined"
98+ margin = "normal"
99+ required
100+ fullWidth
101+ name = "password"
102+ label = "Password"
103+ type = "password"
104+ id = "password"
105+ autoComplete = "current-password"
106+ />
107+ < FormControlLabel
108+ control = { < Checkbox value = "remember" color = "primary" /> }
109+ label = "Remember me"
110+ />
111+ < Button
112+ type = "submit"
113+ fullWidth
114+ variant = "contained"
115+ color = "primary"
116+ className = { classes . submit }
117+ >
118+ Sign In
119+ </ Button >
120+ < Grid container >
121+ < Grid item xs >
122+ < Link href = "#" variant = "body2" >
123+ Forgot password?
124+ </ Link >
125+ </ Grid >
126+ < Grid item >
127+ < Link href = "/signup" variant = "body2" >
128+ { "Don't have an account? Sign Up" }
129+ </ Link >
130+ </ Grid >
131+ </ Grid >
132+ </ form >
133+ </ div >
134+ < Box mt = { 8 } >
135+ < Copyright />
136+ </ Box >
137+ </ Container >
138+ ) ;
139+ }
0 commit comments