From e88972a02ca327653888aa48396612bbd80ab3a0 Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Fri, 25 Sep 2020 10:40:50 -0700 Subject: [PATCH] Fixed and implemented login --- .env.sample | 1 + src/components/LoginModal/LoginModal.tsx | 122 +++++++++++++++++++-- src/components/SignupModal/SignupModal.tsx | 2 +- src/utils/api.tsx | 4 + 4 files changed, 117 insertions(+), 12 deletions(-) diff --git a/.env.sample b/.env.sample index 361cfe48..e655b9c0 100644 --- a/.env.sample +++ b/.env.sample @@ -4,4 +4,5 @@ NODE_PATH='src/' # App URLs # Don't add a trailing slash to these URLs. SIERO_API_URL='' +SIERO_OAUTH_URL='' SIERO_IMG_URL='' \ No newline at end of file diff --git a/src/components/LoginModal/LoginModal.tsx b/src/components/LoginModal/LoginModal.tsx index cb3f2bbb..abc1fbf0 100644 --- a/src/components/LoginModal/LoginModal.tsx +++ b/src/components/LoginModal/LoginModal.tsx @@ -1,6 +1,10 @@ -import React from 'react' -import Portal from '~utils/Portal' +import React, { useState } from 'react' +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 Overlay from '~components/Overlay/Overlay' @@ -9,33 +13,129 @@ import './LoginForm.css' import New from '../../../assets/new' interface Props { + cookies: Cookies 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 emailInput: React.RefObject = React.createRef() + const passwordInput: React.RefObject = React.createRef() + const form: React.RefObject[] = [emailInput, passwordInput] + + const [formValid, setFormValid] = useState(false) + const [errors, setErrors] = useState({ + email: '', + password: '' + }) + + function handleChange(event: React.ChangeEvent) { + 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 ( - + createPortal(

Log in

-
-
- - + +
+
+ +
Forgot your password? - +
-
- +
, + document.body + ) ) } -export default LoginModal \ No newline at end of file +export default withCookies(LoginModal) \ No newline at end of file diff --git a/src/components/SignupModal/SignupModal.tsx b/src/components/SignupModal/SignupModal.tsx index 858f162e..7704365f 100644 --- a/src/components/SignupModal/SignupModal.tsx +++ b/src/components/SignupModal/SignupModal.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useState } from 'react' +import React from 'react' import { withCookies, Cookies } from 'react-cookie' import { createPortal } from 'react-dom' import api from '~utils/api' diff --git a/src/utils/api.tsx b/src/utils/api.tsx index 79eb9810..e829180d 100644 --- a/src/utils/api.tsx +++ b/src/utils/api.tsx @@ -45,6 +45,10 @@ class Api { } 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) { const resourceUrl = `${this.url}/${name}` return axios.get(`${resourceUrl}/search?query=${query}`)