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": { "ssri": {
"version": "6.0.1", "version": "6.0.1",
"resolved": "", "resolved": "https://registry.npmjs.org/ssri/-/ssri-6.0.1.tgz",
"integrity": "sha512-3Wge10hNcT1Kur4PDFwEieXSCMCJs/7WvSACcrMYrNp+b8kDL1/0wJch5Ni2WrtwEa2IO8OsVfeKIciKCDx/QA==",
"requires": { "requires": {
"figgy-pudding": "^3.5.1" "figgy-pudding": "^3.5.1"
} }

View file

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

View file

@ -1,3 +1,12 @@
import axios from 'axios'; 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( putUrlHandler(
state.url, state.slug, state.url, state.slug,
).then((data) => { ).then((data) => {
setState({ if (data) {
...state, setState({
result: `${window.location.origin}/${data}`, ...state,
loading: false, result: `${window.location.origin}/${data}`,
error: false, 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) => { }).catch((ex) => {
if (ex.response?.data?.error) { if (ex.response?.data?.error) {
setState({ setState({