Store auth info in a JWT token in a cookie

This commit is contained in:
Ken-Håvard Lieng 2016-01-15 02:27:30 +01:00
parent 3e0a1be6bc
commit fb54d4966c
18 changed files with 499 additions and 331 deletions

View file

@ -1,35 +1,22 @@
import React from 'react';
import { render } from 'react-dom';
import { syncReduxAndRouter, replacePath } from 'redux-simple-router';
import { syncReduxAndRouter } from 'redux-simple-router';
import createBrowserHistory from 'history/lib/createBrowserHistory';
import configureStore from './store';
import createRoutes from './routes';
import Socket from './util/Socket';
import handleSocket from './socket';
import { createUUID } from './util';
import Root from './containers/Root';
const host = __DEV__ ? `${window.location.hostname}:1337` : window.location.host;
let uuid = localStorage.uuid;
let newUser = false;
if (!uuid) {
uuid = createUUID();
newUser = true;
}
const socket = new Socket(host, uuid);
const socket = new Socket(host);
const store = configureStore(socket);
handleSocket(socket, store);
const history = createBrowserHistory();
syncReduxAndRouter(history, store);
if (newUser) {
store.dispatch(replacePath('/connect'));
localStorage.uuid = uuid;
}
const routes = createRoutes();
render(<Root store={store} routes={routes} history={history} />, document.getElementById('root'));

View file

@ -2,16 +2,12 @@ import EventEmitter2 from 'eventemitter2';
import Backoff from 'backo';
export default class Socket extends EventEmitter2 {
constructor(host, uuid) {
constructor(host) {
super();
const protocol = window.location.protocol === 'https:' ? 'wss' : 'ws';
this.url = `${protocol}://${host}/ws`;
if (uuid) {
this.url += `?uuid=${uuid}`;
}
this.connectTimeout = 20000;
this.pingTimeout = 30000;
this.backoff = new Backoff({

View file

@ -10,14 +10,6 @@ export function normalizeChannel(channel) {
return channel.split('#').join('').toLowerCase();
}
export function createUUID() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
const r = Math.random() * 16 | 0;
const v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
export function timestamp(date = new Date()) {
const h = padLeft(date.getHours(), 2, '0');
const m = padLeft(date.getMinutes(), 2, '0');