diff --git a/src/App.js b/src/App.js index 2aae659..7cd61fc 100644 --- a/src/App.js +++ b/src/App.js @@ -5,6 +5,7 @@ import SettingsMenu from './components/SettingsMenu' import SettingsIcon from './components/SettingsIcon' import api from './utils/api' import sortByDate from './utils/sortByDate' +import isLocalHost from './utils/isLocalHost' import './App.css' export default class App extends Component { @@ -15,6 +16,15 @@ export default class App extends Component { componentDidMount() { // Fetch all todos api.readAll().then((todos) => { + if (todos.message === 'unauthorized') { + if (isLocalHost()) { + alert('FaunaDB key is not unauthorized. Make sure you set it in terminal session where you ran `npm start`. Visit http://bit.ly/set-fauna-key for more info') + } else { + alert('FaunaDB key is not unauthorized. Verify the key `FAUNADB_SECRET` set in Netlify enviroment variables is correct') + } + return false + } + console.log('all todos', todos) this.setState({ todos: todos diff --git a/src/utils/api.js b/src/utils/api.js index b609b68..a98078d 100644 --- a/src/utils/api.js +++ b/src/utils/api.js @@ -1,7 +1,6 @@ /* Api methods to call /functions */ const create = (data) => { - console.log('run new create') return fetch('/.netlify/functions/todos-create', { body: JSON.stringify(data), method: 'POST' @@ -12,7 +11,6 @@ const create = (data) => { const readAll = () => { return fetch('/.netlify/functions/todos-read-all').then((response) => { - console.log(response) return response.json() }) } diff --git a/src/utils/isLocalHost.js b/src/utils/isLocalHost.js new file mode 100644 index 0000000..a8faad1 --- /dev/null +++ b/src/utils/isLocalHost.js @@ -0,0 +1,11 @@ + +export default function isLocalHost() { + const isLocalhostName = window.location.hostname === 'localhost'; + const isLocalhostIPv6 = window.location.hostname === '[::1]'; + const isLocalhostIPv4 = window.location.hostname.match( + // 127.0.0.1/8 + /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ + ); + + return isLocalhostName || isLocalhostIPv6 || isLocalhostIPv4; +}