Not working right - too tired right now

This commit is contained in:
2021-03-08 17:01:14 +11:00
parent b5b1b987b1
commit 44553836c7
7 changed files with 244 additions and 18 deletions
+88
View File
@@ -0,0 +1,88 @@
import React, { useState, useRef } from 'react';
import { Redirect } from 'react-router-dom';
import { setToken, getToken } from '../../utilities/token-client';
const LogIn = props => {
//if logged in
const [tok, setTok] = useState(null);
getToken()
.then(token => setTok(token))
.catch(e => console.error(e))
;
if (tok) {
return <Redirect to='/' />;
}
//refs
const emailRef = useRef();
const passwordRef = useRef();
return (
<div className='page'>
<h1 className='centered'>Login</h1>
<form className='constricted' onSubmit={
async evt => {
//on submit
evt.preventDefault();
const [result, redirect] = await handleSubmit(emailRef.current.value, passwordRef.current.value);
if (result) {
alert(result);
}
//redirect
if (redirect) {
props.history.push('/');
}
}
}>
<div>
<label htmlFor="email">Email:</label>
<input type="email" name="email" ref={emailRef} />
</div>
<div>
<label htmlFor="password">Password:</label>
<input type="password" name="password" ref={passwordRef} />
</div>
<button type='submit'>Login</button>
</form>
</div>
);
};
//DOCS: returns two values: response and OK
const handleSubmit = async (email, password) => {
email = email.trim(); //TODO: validate email on login
//send to the auth server
const result = await fetch(`${process.env.AUTH_URI}/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({
email,
password,
})
});
if (!result.ok) {
const err = `${result.status}: ${await result.text()}`;
console.error(err);
return [err, false];
}
//save the auth tokens
const authTokens = await result.json();
await setToken(authTokens.accessToken, authTokens.refreshToken);
return [null, true];
};
export default LogIn;
+20 -11
View File
@@ -1,11 +1,24 @@
import React, { useRef } from 'react';
import React, { useState, useRef } from 'react';
import { Redirect } from 'react-router-dom';
import { getToken } from '../../utilities/token-client';
//utilities
const validateEmail = require('../../../common/utilities/validate-email.js');
const validateUsername = require('../../../common/utilities/validate-username.js');
const SignUp = props => {
//TODO: redirect if logged in
//if logged in
const [tok, setTok] = useState(null)
getToken()
.then(token => setTok(token))
.catch(e => console.error(e))
;
if (tok) {
return <Redirect to='/' />;
}
//refs
const emailRef = useRef();
@@ -18,18 +31,14 @@ const SignUp = props => {
<div className='page'>
<h1 className='centered'>Signup</h1>
<form className='constricted' onSubmit={
async evt => {
//on submit
async evt => { //on submit
evt.preventDefault();
const [redirect, result] = await handleSubmit(emailRef.current.value, usernameRef.current.value, passwordRef.current.value, retypeRef.current.value, contactRef.current.checked);
const [result, redirect] = await handleSubmit(emailRef.current.value, usernameRef.current.value, passwordRef.current.value, retypeRef.current.value, contactRef.current.checked);
if (result) {
alert(result);
}
//cleanup & redirect
emailRef.current.value = usernameRef.current.value = passwordRef.current.value = retypeRef.current.value = ''; //clear input
contactRef.current.checked = false;
//redirect
if (redirect) {
props.history.push('/');
}
@@ -94,10 +103,10 @@ const handleSubmit = async (email, username, password, retype, contact) => {
if (!result.ok) {
const err = `${result.status}: ${await result.text()}`;
console.error(err);
return [false, err];
return [err, false];
}
return [true, await result.text()];
return [await result.text(), true];
};
//returns an error message, or null on success