Skip to content
This repository has been archived by the owner on Oct 11, 2022. It is now read-only.

Re-try #2135

Merged
merged 2 commits into from
Dec 4, 2017
Merged

Re-try #2135

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions iris/authentication.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -32,9 +31,11 @@ const init = () => {
getUser({ id })
.then(user => {
done(null, user);
return null;
})
.catch(err => {
done(err);
return null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've fixed this on the #2096 branch too, but I did

return getUser({ id })
  .then(user => {
    done(null, user);
    return user;
  })

Unsure if that makes any difference tho.

});
});

Expand All @@ -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) => {
Expand Down Expand Up @@ -89,9 +90,11 @@ const init = () => {
createOrFindUser(user, 'providerId')
.then(user => {
done(null, user);
return null;
})
.catch(err => {
done(err);
return null;
});
}
)
Expand All @@ -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',
Expand Down Expand Up @@ -154,9 +157,11 @@ const init = () => {
createOrFindUser(user, 'fbProviderId')
.then(user => {
done(null, user);
return null;
})
.catch(err => {
done(err);
return null;
});
}
)
Expand All @@ -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 = {
Expand Down Expand Up @@ -219,9 +224,11 @@ const init = () => {
createOrFindUser(user, 'googleProviderId')
.then(user => {
done(null, user);
return null;
})
.catch(err => {
done(err);
return null;
});
}
)
Expand All @@ -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) => {
Expand All @@ -258,9 +265,11 @@ const init = () => {
createOrFindUser(user, 'githubProviderId')
.then(user => {
done(null, user);
return null;
})
.catch(err => {
done(err);
return null;
});
}
)
Expand Down
64 changes: 42 additions & 22 deletions iris/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
Expand All @@ -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();
}
33 changes: 3 additions & 30 deletions iris/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,6 @@ const getUsersBySearchString = (string: string): Promise<Array<DBUser>> => {
);
};

// 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<DBUser> => {
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<DBUser> => {
return db
.table('users')
Expand All @@ -122,7 +108,7 @@ const storeUser = (user: Object): Promise<DBUser> => {
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) => {
Expand Down Expand Up @@ -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);
}
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions iris/renderer/browser-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ global.window = {
global.localStorage = {
getItem: () => null,
setItem: () => {},
removeItem: () => null,
};
global.navigator = {
userAgent: '',
Expand Down
27 changes: 21 additions & 6 deletions src/helpers/localStorage.js
Original file line number Diff line number Diff line change
@@ -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);
}
};