updated format and lint scripts and applied them

This commit is contained in:
JJ Kasper
2018-06-01 16:52:12 -05:00
parent 53ac8a6793
commit 017a9993ee
58 changed files with 1711 additions and 1568 deletions

View File

@@ -1,74 +1,82 @@
import fetch from 'isomorphic-unfetch';
import store from '../store';
import getUrl from '../../util/getUrl';
import fetch from 'isomorphic-unfetch'
import store from '../store'
import getUrl from '../../util/getUrl'
// define action types
export const SET_USER = 'SET_USER';
export const LOGIN_PENDING = 'LOGIN_PENDING';
export const LOGIN_FAILED = 'LOGIN_FAILED';
export const LOGOUT = 'LOGOUT';
export const SET_USER = 'SET_USER'
export const LOGIN_PENDING = 'LOGIN_PENDING'
export const LOGIN_FAILED = 'LOGIN_FAILED'
export const LOGOUT = 'LOGOUT'
export const setUser = user => {
store.dispatch({
type: SET_USER,
data: user
});
}; // setUser
data: user,
})
} // setUser
export const doLogout = () => {
if(typeof window !== 'undefined') {
window.localStorage.removeItem('jwt');
document.cookie = 'jwt=; expires=Thu, 01 Jan 1970 00:00:01 GMT;path=/;';
if (typeof window !== 'undefined') {
window.localStorage.removeItem('jwt')
document.cookie = 'jwt=; expires=Thu, 01 Jan 1970 00:00:01 GMT;path=/;'
}
store.dispatch({ type: LOGOUT });
}; // doLogout
store.dispatch({ type: LOGOUT })
} // doLogout
export async function doLogin (creds, jwt, noPend) {
!noPend && store.dispatch({ type: LOGIN_PENDING });
const authReqOpts = { method: 'POST', credentials: 'include' };
const authReqHead = { headers: jwt ? { Authorization: jwt } : {
'Content-Type': 'application/json' }
};
const authReqBody = jwt ? null : {
body: JSON.stringify({...creds, strategy: 'local'})
};
const authReq = new Request(getUrl('auth'), {
...authReqOpts, ...authReqHead, ...authReqBody
});
const authRes = await fetch(authReq).catch(err => {
store.dispatch({ type: LOGIN_FAILED, data: err.message });
});
if(!authRes.ok) {
let error;
try {
error = await authRes.json();
error = error.message;
}
catch (err) {
error = authRes.status === 429
? 'Max login attempts reached'
: 'An error occurred during login';
}
return store.dispatch({
type: LOGIN_FAILED,
data: error
});
export async function doLogin(creds, jwt, noPend) {
!noPend && store.dispatch({ type: LOGIN_PENDING })
const authReqOpts = { method: 'POST', credentials: 'include' }
const authReqHead = {
headers: jwt
? { Authorization: jwt }
: {
'Content-Type': 'application/json',
},
}
const { accessToken } = await authRes.json();
const payload = accessToken.split('.')[1];
const { userId } = JSON.parse(atob(payload));
const userReq = new Request(getUrl(`/users/${userId}`), {
headers: {
Authorization: accessToken
const authReqBody = jwt
? null
: {
body: JSON.stringify({ ...creds, strategy: 'local' }),
}
const authReq = new Request(getUrl('auth'), {
...authReqOpts,
...authReqHead,
...authReqBody,
})
const authRes = await fetch(authReq).catch(err => {
store.dispatch({ type: LOGIN_FAILED, data: err.message })
})
if (!authRes.ok) {
let error
try {
error = await authRes.json()
error = error.message
} catch (err) {
error =
authRes.status === 429
? 'Max login attempts reached'
: 'An error occurred during login'
}
});
const userRes = await fetch(userReq);
if(!userRes.ok) {
return store.dispatch({
type: LOGIN_FAILED,
data: 'failed to get user'
});
data: error,
})
}
window.localStorage.setItem('jwt', accessToken);
setUser(await userRes.json());
} // doLogin
const { accessToken } = await authRes.json()
const payload = accessToken.split('.')[1]
const { userId } = JSON.parse(atob(payload))
const userReq = new Request(getUrl(`/users/${userId}`), {
headers: {
Authorization: accessToken,
},
})
const userRes = await fetch(userReq)
if (!userRes.ok) {
return store.dispatch({
type: LOGIN_FAILED,
data: 'failed to get user',
})
}
window.localStorage.setItem('jwt', accessToken)
setUser(await userRes.json())
} // doLogin

View File

@@ -1,9 +1,9 @@
import {
SET_USER,
import {
SET_USER,
LOGIN_PENDING,
LOGIN_FAILED,
LOGOUT
} from '../actions/userAct';
LOGOUT,
} from '../actions/userAct'
const initState = {
setup: false,
@@ -11,35 +11,36 @@ const initState = {
email: null,
admin: null,
pending: false,
error: null
};
error: null,
}
function user(state=initState, action) {
switch(action.type) {
case SET_USER: {
return {
...initState,
...action.data
};
}
case LOGIN_PENDING: {
return {
...initState,
pending: true
};
}
case LOGIN_FAILED: {
return {
...state,
pending: false,
error: action.data
};
}
case LOGOUT: {
return initState;
}
default: return state;
function user(state = initState, action) {
switch (action.type) {
case SET_USER: {
return {
...initState,
...action.data,
}
}
case LOGIN_PENDING: {
return {
...initState,
pending: true,
}
}
case LOGIN_FAILED: {
return {
...state,
pending: false,
error: action.data,
}
}
case LOGOUT: {
return initState
}
default:
return state
}
}
export default user;
export default user

View File

@@ -1,23 +1,19 @@
import {
applyMiddleware,
combineReducers,
createStore
} from 'redux';
import { applyMiddleware, combineReducers, createStore } from 'redux'
import user from './reducers/userRed';
import user from './reducers/userRed'
let middleware;
if(process.env.NODE_ENV !== 'production') {
const logger = require('redux-logger').default;
if(typeof window !== 'undefined') {
middleware = applyMiddleware(logger);
let middleware
if (process.env.NODE_ENV !== 'production') {
const logger = require('redux-logger').default
if (typeof window !== 'undefined') {
middleware = applyMiddleware(logger)
}
}
const reducers = combineReducers({
user
});
user,
})
export default middleware
? createStore(reducers, middleware)
: createStore(reducers);
export default (middleware
? createStore(reducers, middleware)
: createStore(reducers))