Fixed and implemented login
This commit is contained in:
parent
f1f6352b22
commit
e88972a02c
4 changed files with 117 additions and 12 deletions
|
|
@ -4,4 +4,5 @@ NODE_PATH='src/'
|
||||||
# App URLs
|
# App URLs
|
||||||
# Don't add a trailing slash to these URLs.
|
# Don't add a trailing slash to these URLs.
|
||||||
SIERO_API_URL=''
|
SIERO_API_URL=''
|
||||||
|
SIERO_OAUTH_URL=''
|
||||||
SIERO_IMG_URL=''
|
SIERO_IMG_URL=''
|
||||||
|
|
@ -1,6 +1,10 @@
|
||||||
import React from 'react'
|
import React, { useState } from 'react'
|
||||||
import Portal from '~utils/Portal'
|
import { withCookies, Cookies } from 'react-cookie'
|
||||||
|
import { createPortal } from 'react-dom'
|
||||||
|
import api from '~utils/api'
|
||||||
|
|
||||||
|
import Button from '~components/Button/Button'
|
||||||
|
import Fieldset from '~components/Fieldset/Fieldset'
|
||||||
import Modal from '~components/Modal/Modal'
|
import Modal from '~components/Modal/Modal'
|
||||||
import Overlay from '~components/Overlay/Overlay'
|
import Overlay from '~components/Overlay/Overlay'
|
||||||
|
|
||||||
|
|
@ -9,33 +13,129 @@ import './LoginForm.css'
|
||||||
import New from '../../../assets/new'
|
import New from '../../../assets/new'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
|
cookies: Cookies
|
||||||
close: () => void
|
close: () => void
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface ErrorMap {
|
||||||
|
[index: string]: string
|
||||||
|
email: string
|
||||||
|
password: string
|
||||||
|
}
|
||||||
|
|
||||||
|
const emailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
|
||||||
|
|
||||||
const LoginModal = (props: Props) => {
|
const LoginModal = (props: Props) => {
|
||||||
|
const emailInput: React.RefObject<HTMLInputElement> = React.createRef()
|
||||||
|
const passwordInput: React.RefObject<HTMLInputElement> = React.createRef()
|
||||||
|
const form: React.RefObject<HTMLInputElement>[] = [emailInput, passwordInput]
|
||||||
|
|
||||||
|
const [formValid, setFormValid] = useState(false)
|
||||||
|
const [errors, setErrors] = useState<ErrorMap>({
|
||||||
|
email: '',
|
||||||
|
password: ''
|
||||||
|
})
|
||||||
|
|
||||||
|
function handleChange(event: React.ChangeEvent<HTMLInputElement>) {
|
||||||
|
event.preventDefault()
|
||||||
|
|
||||||
|
const { name, value } = event.target
|
||||||
|
let newErrors = errors
|
||||||
|
|
||||||
|
switch(name) {
|
||||||
|
case 'email':
|
||||||
|
errors.email = emailRegex.test(value)
|
||||||
|
? ''
|
||||||
|
: 'That email address is not valid'
|
||||||
|
break
|
||||||
|
|
||||||
|
case 'password':
|
||||||
|
errors.password = value.length == 0
|
||||||
|
? 'Please enter your password'
|
||||||
|
: ''
|
||||||
|
break
|
||||||
|
|
||||||
|
default:
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
setErrors(newErrors)
|
||||||
|
setFormValid(validateForm())
|
||||||
|
}
|
||||||
|
|
||||||
|
function validateForm() {
|
||||||
|
let valid = true
|
||||||
|
|
||||||
|
Object.values(form).forEach(
|
||||||
|
(input) => input.current?.value.length == 0 && (valid = false)
|
||||||
|
)
|
||||||
|
|
||||||
|
Object.values(errors).forEach(
|
||||||
|
(error) => error.length > 0 && (valid = false)
|
||||||
|
)
|
||||||
|
|
||||||
|
return valid
|
||||||
|
}
|
||||||
|
|
||||||
|
function submit(event: React.FormEvent) {
|
||||||
|
event.preventDefault()
|
||||||
|
|
||||||
|
const body = {
|
||||||
|
email: emailInput.current?.value,
|
||||||
|
password: passwordInput.current?.value,
|
||||||
|
grant_type: 'password'
|
||||||
|
}
|
||||||
|
|
||||||
|
if (formValid) {
|
||||||
|
api.login(body)
|
||||||
|
.then((response) => {
|
||||||
|
console.log(response)
|
||||||
|
const cookies = props.cookies
|
||||||
|
cookies.set('user', response.data.user, { path: '/'})
|
||||||
|
props.close()
|
||||||
|
}, (error) => {
|
||||||
|
console.error(error)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Portal>
|
createPortal(
|
||||||
<div>
|
<div>
|
||||||
<Modal styleName="LoginForm">
|
<Modal styleName="LoginForm">
|
||||||
<div id="ModalTop">
|
<div id="ModalTop">
|
||||||
<h1>Log in</h1>
|
<h1>Log in</h1>
|
||||||
<i className='close' onClick={props.close}><New /></i>
|
<i className='close' onClick={props.close}><New /></i>
|
||||||
</div>
|
</div>
|
||||||
<form>
|
<form className="form" onSubmit={submit}>
|
||||||
<div className="fieldset">
|
<div id="fields">
|
||||||
<input className="Input" name="email" type="text" placeholder="Email address" />
|
<Fieldset
|
||||||
<input className="Input" name="password" type="password" placeholder="Password" />
|
fieldName="email"
|
||||||
|
placeholder="Email address"
|
||||||
|
onChange={handleChange}
|
||||||
|
error={errors.email}
|
||||||
|
ref={emailInput}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<Fieldset
|
||||||
|
fieldName="password"
|
||||||
|
placeholder="Password"
|
||||||
|
onChange={handleChange}
|
||||||
|
error={errors.password}
|
||||||
|
ref={passwordInput}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div id="ModalBottom">
|
<div id="ModalBottom">
|
||||||
<a>Forgot your password?</a>
|
<a>Forgot your password?</a>
|
||||||
<button className="Button">Log in</button>
|
<Button color="blue" disabled={!formValid}>Log in</Button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</Modal>
|
</Modal>
|
||||||
<Overlay onClick={props.close} />
|
<Overlay onClick={props.close} />
|
||||||
</div>
|
</div>,
|
||||||
</Portal>
|
document.body
|
||||||
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export default LoginModal
|
export default withCookies(LoginModal)
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import React, { useEffect, useState } from 'react'
|
import React from 'react'
|
||||||
import { withCookies, Cookies } from 'react-cookie'
|
import { withCookies, Cookies } from 'react-cookie'
|
||||||
import { createPortal } from 'react-dom'
|
import { createPortal } from 'react-dom'
|
||||||
import api from '~utils/api'
|
import api from '~utils/api'
|
||||||
|
|
|
||||||
|
|
@ -45,6 +45,10 @@ class Api {
|
||||||
} as EndpointMap
|
} as EndpointMap
|
||||||
}
|
}
|
||||||
|
|
||||||
|
login(object: {}) {
|
||||||
|
return axios.post(`${process.env.SIERO_OAUTH_URL}/token` || 'http://127.0.0.1:3000/oauth/token', object)
|
||||||
|
}
|
||||||
|
|
||||||
search(query: string) {
|
search(query: string) {
|
||||||
const resourceUrl = `${this.url}/${name}`
|
const resourceUrl = `${this.url}/${name}`
|
||||||
return axios.get(`${resourceUrl}/search?query=${query}`)
|
return axios.get(`${resourceUrl}/search?query=${query}`)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue