Conflict protection

This commit is contained in:
oitsjustjose 2021-06-22 20:50:52 -05:00
commit 3fd340d14d
4 changed files with 45 additions and 15 deletions

View file

@ -13933,7 +13933,8 @@
},
"ssri": {
"version": "6.0.1",
"resolved": "",
"resolved": "https://registry.npmjs.org/ssri/-/ssri-6.0.1.tgz",
"integrity": "sha512-3Wge10hNcT1Kur4PDFwEieXSCMCJs/7WvSACcrMYrNp+b8kDL1/0wJch5Ni2WrtwEa2IO8OsVfeKIciKCDx/QA==",
"requires": {
"figgy-pudding": "^3.5.1"
}

View file

@ -9,21 +9,32 @@ import UrlShortener from './components/UrlShortener';
import CheckUrlRedirect from './axios/getUrlHandler';
const slug = window.location.pathname.match(/[^/][^/]*/g);
export const routes = [
{ path: '/', component: UrlShortener },
{ path: '/code', component: TextUpload },
{ path: '/files', component: FileUpload },
];
export default () => {
// Check to see if slug is worth trying :)
!!slug && CheckUrlRedirect(slug).then((url) => {
window.location.href = url;
}).catch(() => {});
/* Ensure that there's a slug worth trying, and that it isn't
already a route that should be "protected"
*/
if (!!slug && !routes.map(
({ path }) => path,
).includes(window.location.pathname)) {
CheckUrlRedirect(slug).then((url) => {
window.location.href = url;
}).catch(() => {});
}
ReactDOM.render(
<React.StrictMode>
<Router>
<NavBar />
<div style={{ paddingTop: `${3.5}em` }} />
<Route exact path="/" component={UrlShortener} />
<Route exact path="/code" component={TextUpload} />
<Route exact path="/files" component={FileUpload} />
{routes.map(({ path, component }) => (
<Route exact path={path} component={component} />
))}
</Router>
</React.StrictMode>,
document.getElementById('root'),

View file

@ -1,3 +1,12 @@
import axios from 'axios';
import { routes } from '../App';
export default (url, slug) => axios.put('/api/url', { url, slug }).then((resp) => resp.data);
export default (url, slug) => {
const paths = routes.map(({ path }) => path.substr(1));
if (slug && paths.includes(slug)) {
return Promise.resolve(null);
}
return axios.put('/api/url', { url, slug }).then((resp) => resp.data);
};

View file

@ -21,12 +21,21 @@ export default () => {
putUrlHandler(
state.url, state.slug,
).then((data) => {
setState({
...state,
result: `${window.location.origin}/${data}`,
loading: false,
error: false,
});
if (data) {
setState({
...state,
result: `${window.location.origin}/${data}`,
loading: false,
error: false,
});
} else {
setState({
...state,
result: null,
loading: false,
error: 'There is a conflict with this slug. Please try a different one!',
});
}
}).catch((ex) => {
if (ex.response?.data?.error) {
setState({