import React, { useContext, useRef } from 'react'; import { Link, Redirect } from 'react-router-dom'; import queryString from 'query-string'; import ApplyToBody from '../utilities/apply-to-body'; import { TokenContext } from '../utilities/token-provider'; const Reset = props => { //context const authTokens = useContext(TokenContext); //query const query = queryString.parse(props.location.search); //misplaced? if (authTokens.accessToken || !query.email || !query.token) { return ; } //refs const passwordRef = useRef(); const retypeRef = useRef(); //render the thing return ( <>

Reset Password

{ evt.preventDefault(); const [err, redirect] = await update(passwordRef.current.value, retypeRef.current.value, query); if (err) { alert(err); return; } alert('Details updated'); //TODO: replace with a message from the auth server //redirect if (redirect) { props.history.push('/'); } }}>
Return Home
); }; const update = async (password, retype, query) => { if (password != retype) { return ['Passwords do not match']; } if (password && password.length < 8) { return ['Password is too short']; } const result = await fetch(`${process.env.AUTH_URI}/auth/reset?email=${query.email}&token=${query.token}`, { method: 'PATCH', headers: { 'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json' }, body: JSON.stringify({ password: password ? password : null, }) }); if (!result.ok) { return [`${await result.status}: ${await result.text()}`]; } else { return [null, true]; } } export default Reset;