diff --git a/iris/authentication.js b/iris/authentication.js index 95a06c36bf..f2850611bd 100644 --- a/iris/authentication.js +++ b/iris/authentication.js @@ -1,7 +1,6 @@ // @flow // $FlowFixMe const env = require('node-env-file'); -const IS_PROD = process.env.NODE_ENV === 'production'; const path = require('path'); env(path.resolve(__dirname, './.env'), { raise: false }); // $FlowFixMe @@ -32,9 +31,11 @@ const init = () => { getUser({ id }) .then(user => { done(null, user); + return null; }) .catch(err => { done(err); + return null; }); }); @@ -44,7 +45,7 @@ const init = () => { { consumerKey: 'vxmsICGyIIoT5NEYi1I8baPrf', consumerSecret: TWITTER_OAUTH_CLIENT_SECRET, - callbackURL: `/auth/twitter/callback`, + callbackURL: '/auth/twitter/callback', includeEmail: true, }, (token, tokenSecret, profile, done) => { @@ -89,9 +90,11 @@ const init = () => { createOrFindUser(user, 'providerId') .then(user => { done(null, user); + return null; }) .catch(err => { done(err); + return null; }); } ) @@ -103,7 +106,7 @@ const init = () => { { clientID: FACEBOOK_OAUTH_CLIENT_ID, clientSecret: FACEBOOK_OAUTH_CLIENT_SECRET, - callbackURL: `/auth/facebook/callback`, + callbackURL: '/auth/facebook/callback', profileFields: [ 'id', 'displayName', @@ -154,9 +157,11 @@ const init = () => { createOrFindUser(user, 'fbProviderId') .then(user => { done(null, user); + return null; }) .catch(err => { done(err); + return null; }); } ) @@ -169,7 +174,7 @@ const init = () => { clientID: '923611718470-chv7p9ep65m3fqqjr154r1p3a5j6oidc.apps.googleusercontent.com', clientSecret: GOOGLE_OAUTH_CLIENT_SECRET, - callbackURL: `/auth/google/callback`, + callbackURL: '/auth/google/callback', }, (token, tokenSecret, profile, done) => { const user = { @@ -219,9 +224,11 @@ const init = () => { createOrFindUser(user, 'googleProviderId') .then(user => { done(null, user); + return null; }) .catch(err => { done(err); + return null; }); } ) @@ -233,7 +240,7 @@ const init = () => { { clientID: '208a2e8684d88883eded', clientSecret: GITHUB_OAUTH_CLIENT_SECRET, - callbackURL: `/auth/github/callback`, + callbackURL: '/auth/github/callback', scope: ['user'], }, (token, tokenSecret, profile, done) => { @@ -258,9 +265,11 @@ const init = () => { createOrFindUser(user, 'githubProviderId') .then(user => { done(null, user); + return null; }) .catch(err => { done(err); + return null; }); } ) diff --git a/iris/index.js b/iris/index.js index d7fe59c800..ee1a3577bd 100644 --- a/iris/index.js +++ b/iris/index.js @@ -7,27 +7,21 @@ const compression = require('compression'); const debug = require('debug')('iris'); debug('logging with debug enabled!'); import path from 'path'; -import fs from 'fs'; import { createServer } from 'http'; -//$FlowFixMe import express from 'express'; -import * as graphql from 'graphql'; import Loadable from 'react-loadable'; - -import schema from './schema'; +import Raven from 'shared/raven'; import { init as initPassport } from './authentication.js'; -import createLoaders from './loaders'; -import getMeta from './utils/get-page-meta'; - const IS_PROD = process.env.NODE_ENV === 'production'; - const PORT = 3001; // Initialize authentication initPassport(); + // API server const app = express(); +// Send all responses as gzip app.use(compression()); import middlewares from './routes/middlewares'; @@ -53,20 +47,8 @@ if (IS_PROD || process.env.SSR) { console.log('Server-side rendering disabled for development'); } -import type { Loader } from './loaders/types'; -export type GraphQLContext = { - user: Object, - loaders: { - [key: string]: Loader, - }, -}; - const server = createServer(app); -// Create subscriptions server at /websocket -import createSubscriptionsServer from './routes/create-subscription-server'; -const subscriptionsServer = createSubscriptionsServer(server, '/websocket'); - const boot = () => { // Start webserver server.listen(PORT); @@ -75,8 +57,46 @@ const boot = () => { console.log(`GraphQL server running at http://localhost:${PORT}/api`); }; +import type { Loader } from './loaders/types'; +export type GraphQLContext = { + user: Object, + loaders: { + [key: string]: Loader, + }, +}; + +process.on('unhandledRejection', async err => { + console.error('Unhandled rejection', err); + try { + await Raven.captureException(err); + } catch (err) { + console.error('Raven error', err); + } finally { + process.exit(1); + } +}); + +process.on('uncaughtException', async err => { + console.error('Uncaught exception', err); + try { + await Raven.captureException(err); + } catch (err) { + console.error('Raven error', err); + } finally { + process.exit(1); + } +}); + if (IS_PROD || process.env.SSR) { - Loadable.preloadAll().then(boot); + Loadable.preloadAll() + .then(boot) + .catch(async err => { + try { + await Raven.captureException(err); + } catch (err) { + console.error('Raven error', err); + } + }); } else { boot(); } diff --git a/iris/models/user.js b/iris/models/user.js index 389067771c..cf1b4cfd85 100644 --- a/iris/models/user.js +++ b/iris/models/user.js @@ -95,20 +95,6 @@ const getUsersBySearchString = (string: string): Promise> => { ); }; -// leaving the filter here as an index on providerId would be a waste of -// space. This function is only invoked for signups when checking -// for an existing user on the previous Firebase stack. -const getUserByProviderId = (providerId: string): Promise => { - return db - .table('users') - .filter({ providerId }) - .run() - .then(result => { - if (result && result.length > 0) return result[0]; - throw new new UserError('No user found with this providerId')(); - }); -}; - const storeUser = (user: Object): Promise => { return db .table('users') @@ -122,7 +108,7 @@ const storeUser = (user: Object): Promise => { addQueue('send new user welcome email', { user }); return Promise.all([user, createNewUsersSettings(user.id)]); }) - .then(([user, settings]) => user); + .then(([user]) => user); }; const saveUserProvider = (userId, providerMethod, providerId) => { @@ -202,7 +188,7 @@ const createOrFindUser = ( storedUser.id, providerMethod, user[providerMethod] - ).then(user => Promise.resolve(storedUser)); + ).then(() => Promise.resolve(storedUser)); } else { return Promise.resolve(storedUser); } @@ -213,26 +199,13 @@ const createOrFindUser = ( }) .catch(err => { if (user.id) { + console.log(err); throw new UserError(`No user found for id ${user.id}.`); } return storeUser(user); }); }; -const setUsername = (id: string, username: string) => { - return db - .table('users') - .get(id) - .update( - { - username, - }, - { returnChanges: true } - ) - .run() - .then(result => result.changes[0].new_val); -}; - const getEverything = ( userId: string, { first, after }: PaginationOptions diff --git a/iris/renderer/browser-shim.js b/iris/renderer/browser-shim.js index 1aae6f77da..bd48325a68 100644 --- a/iris/renderer/browser-shim.js +++ b/iris/renderer/browser-shim.js @@ -11,6 +11,7 @@ global.window = { global.localStorage = { getItem: () => null, setItem: () => {}, + removeItem: () => null, }; global.navigator = { userAgent: '', diff --git a/src/helpers/localStorage.js b/src/helpers/localStorage.js index 2f08d64b73..a9c6bfeb3d 100644 --- a/src/helpers/localStorage.js +++ b/src/helpers/localStorage.js @@ -1,10 +1,25 @@ export const clearStorage = () => localStorage.clear(); -export const getItemFromStorage = (key: string) => - JSON.parse(localStorage.getItem(key)); +export const getItemFromStorage = (key: string) => { + try { + JSON.parse(localStorage.getItem(key)); + } catch (err) { + console.log(`Error getting item ${key} from localStoragee`, err); + } +}; -export const storeItem = (key: string, item: any) => - localStorage.setItem(key, JSON.stringify(item)); +export const storeItem = (key: string, item: any) => { + try { + localStorage.setItem(key, JSON.stringify(item)); + } catch (err) { + console.log(`Error storing item ${item} to localStoragee`, err); + } +}; -export const removeItemFromStorage = (item: string) => - localStorage.removeItem(item); +export const removeItemFromStorage = (key: string) => { + try { + localStorage.removeItem(key); + } catch (err) { + console.log(`Error removing item ${key} from localStoragee`, err); + } +};