Imported the directory structure from egg trainer

This commit is contained in:
2021-08-22 02:22:28 +10:00
parent f415a7ece2
commit a0dbe0aee1
41 changed files with 1034 additions and 612 deletions
@@ -0,0 +1,75 @@
import React, { useState, useContext, useRef } from 'react';
import { TokenContext } from '../../utilities/token-provider';
//DOCS: isolated the delete account button into it's own panel, so it can be easily moved as needed
const DeleteAccount = props => {
const authTokens = useContext(TokenContext);
const [open, setOpen] = useState(false);
const passwordRef = useRef();
if (!open) {
return (
<button onClick={() => setOpen(true)}>Delete Account</button>
);
}
return (
<div className='panel centered middle'>
<h2 className='text centered'>Delete Your Account?</h2>
<form className='constrained' onSubmit={async evt => {
evt.preventDefault();
const [err] = await handleSubmit(passwordRef.current.value, authTokens);
if (err) {
alert(err);
}
}}>
<input type="password" name="password" placeholder='Password' ref={passwordRef} />
<button type='submit' style={{backgroundColor: 'red'}}>Delete Account</button>
<button type='cancel' onClick={() => { passwordRef.current.value = ''; setOpen(false); }}>Cancel</button>
</form>
</div>
);
};
const handleSubmit = async (password, authTokens) => {
//schedule a deletion
const result = await authTokens.tokenFetch(`${process.env.AUTH_URI}/auth/account`, {
method: 'DELETE',
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json'
},
body: JSON.stringify({
password
})
});
if (!result.ok) {
return [`${await result.status}: ${await result.text()}`];
}
//force a logout
const result2 = await authTokens.tokenFetch(`${process.env.AUTH_URI}/auth/logout`, {
method: 'DELETE',
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json'
},
body: JSON.stringify({
token: authTokens.refreshToken
})
});
if (!result2.ok) {
return [`${await result2.status}: ${await result2.text()}`];
}
authTokens.setAccessToken('');
authTokens.setRefreshToken('');
return [null];
};
export default DeleteAccount;
+37
View File
@@ -0,0 +1,37 @@
import React, { useContext, useRef } from 'react';
import { Link } from 'react-router-dom';
import { TokenContext } from '../../utilities/token-provider';
//TODO: make this an ACTUAL BUTTON
const Logout = () => {
const authTokens = useContext(TokenContext);
return (
<>
{ /* Logout logs you out of the server too */ }
<Link to='/' onClick={async () => {
const result = await authTokens.tokenFetch(`${process.env.AUTH_URI}/auth/logout`, { //NOTE: this gets overwritten as a bugfix
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({
token: authTokens.refreshToken
})
});
//any problems?
if (!result.ok) {
console.error(await result.text());
} else {
authTokens.setAccessToken('');
authTokens.setRefreshToken('');
}
}}>Logout</Link>
</>
);
};
export default Logout;