Skip to content

Commit b87f05e

Browse files
committed
* improve authentication
* stop use client config in api * eslint display warning on development
1 parent 940e26d commit b87f05e

9 files changed

Lines changed: 29 additions & 59 deletions

File tree

api/actions/auth/index.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

api/actions/auth/load.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

api/actions/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
export loadInfo from './loadInfo';
2-
export * as auth from './auth';
32
export * as widget from './widget';
43
export * as survey from './survey';

api/api.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import rest from 'feathers-rest';
88
import socketio from 'feathers-socketio';
99
import isPromise from 'is-promise';
1010
import PrettyError from 'pretty-error';
11-
import publicConfig from '../src/config';
1211
import config from './config';
1312
import middleware from './middleware';
1413
import services from './services';
@@ -35,16 +34,15 @@ app.set('config', config)
3534

3635
const actionsHandler = (req, res, next) => {
3736
const splittedUrlPath = req.url.split('?')[0].split('/').slice(1);
38-
3937
const { action, params } = mapUrl(actions, splittedUrlPath);
4038

39+
req.app = app;
40+
4141
const catchError = error => {
4242
console.error('API ERROR:', pretty.render(error));
4343
res.status(error.status || 500).json(error);
4444
};
4545

46-
req.app = app;
47-
4846
if (action) {
4947
try {
5048
const handle = action(req, params);
@@ -79,13 +77,13 @@ app.configure(hooks())
7977
.configure(services)
8078
.configure(middleware);
8179

82-
if (publicConfig.apiPort) {
83-
app.listen(publicConfig.apiPort, err => {
80+
if (process.env.APIPORT) {
81+
app.listen(process.env.APIPORT, err => {
8482
if (err) {
8583
console.error(err);
8684
}
87-
console.info('----\n==> 🌎 API is running on port %s', publicConfig.apiPort);
88-
console.info('==> 💻 Send requests to http://%s:%s', publicConfig.apiHost, publicConfig.apiPort);
85+
console.info('----\n==> 🌎 API is running on port %s', process.env.APIPORT);
86+
console.info('==> 💻 Send requests to http://%s:%s', process.env.APIHOST, process.env.APIPORT);
8987
});
9088
} else {
9189
console.error('==> ERROR: No APIPORT environment variable has been specified');

api/services/authentication/index.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,17 @@ import local from 'feathers-authentication-local';
55
import oauth2 from 'feathers-authentication-oauth2';
66
import FacebookTokenStrategy from 'passport-facebook-token';
77
import { discard } from 'feathers-hooks-common';
8-
import { verifyJWT } from 'feathers-authentication/lib/utils';
98

109
export socketAuth from './socketAuth';
1110

1211
function populateUser(authConfig) {
13-
return hook => verifyJWT(hook.result.accessToken, authConfig)
12+
return hook => hook.app.passport.verifyJWT(hook.result.accessToken, authConfig)
1413
.then(payload => hook.app.service('users').get(payload.userId))
1514
.then(user => {
1615
hook.result.user = user;
1716
});
1817
}
1918

20-
function addTokenExpiration() {
21-
return hook => {
22-
if (hook.result.accessToken) {
23-
hook.result.expires = hook.app.get('auth').cookie.maxAge || null;
24-
}
25-
return hook;
26-
};
27-
}
28-
2919
function restToSocketAuth() {
3020
return hook => {
3121
if (hook.params.provider !== 'rest') return hook;
@@ -71,7 +61,6 @@ export default function authenticationService() {
7161
create: [
7262
populateUser(config),
7363
discard('user.password'),
74-
addTokenExpiration(),
7564
restToSocketAuth()
7665
]
7766
}

api/services/authentication/socketAuth.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { verifyJWT } from 'feathers-authentication/lib/utils';
2-
31
export default function socketAuth(app) {
42
return (socket, next) => {
53
const { cookie } = socket.request.headers;
@@ -18,7 +16,7 @@ export default function socketAuth(app) {
1816

1917
if (!accessToken) return next();
2018

21-
verifyJWT(accessToken, app.get('auth'))
19+
app.passport.verifyJWT(accessToken, app.get('auth'))
2220
.then(payload => app.service('users').get(payload.userId))
2321
.then(user => {
2422
Object.assign(socket.feathers, {

src/redux/create.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ export default function createStore(history, { client, app, restApp }, data, per
4646

4747
if (__DEVELOPMENT__ && module.hot) {
4848
module.hot.accept('./reducer', () => {
49-
store.replaceReducer(combineReducers(require('./reducer').default(store.asyncReducers)));
49+
const reducer = require('./reducer');
50+
store.replaceReducer(combineReducers((reducer.default || reducer)(store.asyncReducers)));
5051
});
5152
}
5253

src/redux/modules/auth.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ function setToken({ client, app, restApp }) {
112112
return response => {
113113
const { accessToken } = response;
114114

115-
// set manually the JWT for both instances of feathers/client
116115
app.set('accessToken', accessToken);
117116
restApp.set('accessToken', accessToken);
118117
client.setJwtToken(accessToken);
@@ -122,9 +121,18 @@ function setToken({ client, app, restApp }) {
122121
}
123122

124123
function setCookie({ app }) {
124+
return response => app.passport.verifyJWT(response.accessToken)
125+
.then(payload => {
126+
const options = payload.exp ? { expires: new Date(payload.exp * 1000) } : undefined;
127+
cookie.set('feathers-jwt', app.get('accessToken'), options);
128+
return response;
129+
});
130+
}
131+
132+
function setUser({ app, restApp }) {
125133
return response => {
126-
const options = response.expires ? { expires: response.expires / (60 * 60 * 24 * 1000) } : undefined;
127-
cookie.set('feathers-jwt', app.get('accessToken'), options);
134+
app.set('user', response.user);
135+
restApp.set('user', response.user);
128136
return response;
129137
};
130138
}
@@ -140,7 +148,10 @@ export function isLoaded(globalState) {
140148
export function load() {
141149
return {
142150
types: [LOAD, LOAD_SUCCESS, LOAD_FAIL],
143-
promise: ({ client }) => client.get('/auth/load')
151+
promise: ({ app, restApp, client }) => restApp.authenticate()
152+
.then(setToken({ client, app, restApp }))
153+
.then(setCookie({ app }))
154+
.then(setUser({ app, restApp }))
144155
};
145156
}
146157

@@ -160,12 +171,9 @@ export function login(strategy, data, validation = true) {
160171
strategy,
161172
socketId
162173
})
163-
.then(setToken({ client, restApp, app }))
174+
.then(setToken({ client, app, restApp }))
164175
.then(setCookie({ app }))
165-
.then(response => {
166-
app.set('user', response.user);
167-
return response;
168-
})
176+
.then(setUser({ app, restApp }))
169177
.catch(validation ? catchValidation : error => Promise.reject(error))
170178
};
171179
}

webpack/dev.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ var webpackConfig = module.exports = {
141141
loader: 'babel-loader',
142142
query: babelLoaderQuery
143143
}, {
144-
loader: 'eslint-loader'
144+
loader: 'eslint-loader',
145+
options: { emitWarning: true }
145146
}
146147
]),
147148
helpers.createHappyPlugin('less', [

0 commit comments

Comments
 (0)