Skip to content

Commit

Permalink
session fix
Browse files Browse the repository at this point in the history
  • Loading branch information
antkim003 committed Aug 18, 2016
1 parent cd2579f commit 836e8e5
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 95 deletions.
26 changes: 26 additions & 0 deletions browser/data/collections.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module.exports = [
"women",
"men",
"kids",
"shoes",
"jewlery&watches",
"handbags&accessories",
"juniors",
"beauty",
"for_the_home",
"kitchen&dining",
"bed&bath",
"luggage&accessories",
"furniture&mattresses",
"IntlWomen",
"intlmen",
"intlkids",
"intlshoes",
"intljewelry&Watches",
"intlhandbags&Accessories",
"intljuniors",
"intlforthehome",
"intlkitchen&dining",
"intlbed&bath",
"intlluggage&accessories"
]
6 changes: 5 additions & 1 deletion browser/full_table.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var scrolling = require('./scrolling.js');
var sockets = require('./sockets.js');
import { browserHistory } from 'react-router';
import Loader from '../src/components/loader/loader.js';
import defaultCollections from './data/collections.js';

module.exports = React.createClass({
displayName: 'FullTable',
Expand All @@ -33,7 +34,10 @@ module.exports = React.createClass({
var timeNow = Date.now();
var self = this;
scrolling();
window.user = JSON.parse(localStorage.getItem('user'));
window.user = JSON.parse(localStorage.all).user;
if (window.user.collections.length < 1) {
window.user.collections = defaultCollections;
}
window.statedata = [];
sockets();

Expand Down
18 changes: 13 additions & 5 deletions browser/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,23 @@ import { Router, Route, IndexRoute, browserHistory } from 'react-router';
import ReduxThunk from 'redux-thunk';

import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import { createStore, applyMiddleware, compose, combineReducers } from 'redux';
import RequireAuth from '../src/components/auth/require_auth';
import reducers from '../src/reducers';
import ReduxPromise from 'redux-promise';
import { AUTH_USER } from '../src/actions/type';
import { fetchSession }from '../src/actions';

const createStoreWithMiddleware = applyMiddleware(ReduxPromise, ReduxThunk)(createStore);
const store = createStoreWithMiddleware(reducers);
// const createStoreWithMiddleware = applyMiddleware(ReduxPromise, ReduxThunk)(createStore);
// const store = createStoreWithMiddleware(reducers);

const store = createStore(
reducers,
{},
compose(
applyMiddleware(ReduxPromise, ReduxThunk),
window.devToolsExtension ? window.devToolsExtension() : f => f
));

const token = localStorage.getItem('token');
if(token) {
Expand All @@ -30,7 +38,7 @@ const Admin = require('../src/components/admin/admin.js');
const Login = require('../src/components/auth/login.js');
const Logout = require('../src/components/auth/logout.js');
const Landing = require('../src/components/landing.js');
// const Combobulator = require('../src/components/combobulator.js');
const Combobulator = require('../src/components/combobulator.js');
// ReactDOM.render(<App />, document.getElementById('root'));

ReactDOM.render(
Expand All @@ -40,7 +48,7 @@ ReactDOM.render(
<IndexRoute component={Landing} />
<Route path="admin" component={RequireAuth(Admin)}/>
<Route path="finnick" component={RequireAuth(App)} />
{/*<Route path="combobulator" component={Combobulator} />*/}
<Route path="combobulator" component={Combobulator} />
<Route path="login" component={Login}/>
<Route path="logout" component={Logout}/>
</Route>
Expand Down
1 change: 1 addition & 0 deletions browser/scope_table.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module.exports = React.createClass({
var self = this;
scrolling();
window.user = JSON.parse(localStorage.getItem('user'));

window.statedata = [];
sockets();
if (window.location.search == '') {
Expand Down
19 changes: 0 additions & 19 deletions dist/index.html

This file was deleted.

11 changes: 0 additions & 11 deletions dist/main-1a09fc3eab36813db3ed.min.css

This file was deleted.

46 changes: 0 additions & 46 deletions dist/main-1a09fc3eab36813db3ed.min.js

This file was deleted.

1 change: 0 additions & 1 deletion dist/webpack.stats.json

This file was deleted.

12 changes: 11 additions & 1 deletion server/app/routes/members/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var router = require('express').Router();
var User = require('mongoose').model('User');
module.exports = router;
var _ = require('lodash');
var jwt = require('jwt-simple');

var ensureAuthenticated = function (req, res, next) {
if (req.isAuthenticated()) {
Expand All @@ -12,6 +13,11 @@ var ensureAuthenticated = function (req, res, next) {
}
};

function tokenForUser(user) {
var timestamp = new Date().getTime();
return jwt.encode({ sub: user.id, iat: timestamp }, 'secret');
}

var findUserType = function(req, res, next) {
User.findById(req.session.passport.user).then(function(user) {
req.session.user = user;
Expand Down Expand Up @@ -39,7 +45,11 @@ router.get('/', findUserType, function(req, res, next) {

router.get('/session', function (req, res) {
if (req.user) {
res.json({ user: req.user.sanitize() });
var obj = {
user: req.user.sanitize(),
token: tokenForUser(req.user)
};
res.json(obj);
} else {
res.status(401).send('No authenticated user.');
}
Expand Down
18 changes: 7 additions & 11 deletions src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
FETCH_TYPES,
FETCH_PERMISSIONS
} from './type';
import _ from 'lodash';

export function loginUser( { username, password }) {
// redux thunk here
Expand All @@ -24,17 +25,6 @@ export function loginUser( { username, password }) {
.then(response => {
dispatch({ type: AUTH_USER });
dispatch(fetchSession());
localStorage.setItem('token', response.data.token);
localStorage.setItem('all', JSON.stringify(response.data));
localStorage.setItem('user', JSON.stringify({
name: response.data.user.name,
username: username,
type: response.data.user.type,
locked: response.data.user.locked,
collections: response.data.user.collections,
lead: response.data.user.lead
})
);
browserHistory.push('/');
})
.catch(response => {
Expand Down Expand Up @@ -73,6 +63,12 @@ export function fetchSession() {
.then( response => {
dispatch({ type: AUTH_USER });
localStorage.setItem('token',response.data.token);

var oldUser = JSON.parse(localStorage.all).user;
if (oldUser.updatedAt != response.data.user.updatedAt) {
localStorage.setItem('user', JSON.stringify(response.data.user));
}

dispatch({ type: FETCH_SESSION, payload: response });
})
.catch( response => {
Expand Down

0 comments on commit 836e8e5

Please sign in to comment.