Skip to content
Open
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
121 changes: 121 additions & 0 deletions src/actions/login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import {
LOGIN_START,
LOGIN_SUCCESS,
LOGIN_FAILURE,
REGISTRATION_START,
REGISTRATION_SUCCESS,
REGISTRATION_FAILURE,
apiURL,
ALERT_SHOW,
ALERT_HIDE,
} from '../constants';
import fetchAsync from '../utils/fetch';
import createExpiresCookie from '../utils/create-expires-cookie';

const cookie = require('isomorphic-cookie');
Copy link
Contributor

Choose a reason for hiding this comment

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


const minutesOfCookieLive = 60;
Copy link
Contributor

Choose a reason for hiding this comment

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

Good approach to naming, but better English would be: minutesCookiesLive

export function loginStart() {
return {
type: LOGIN_START,
};
}
export function loginSuccess(data) {
cookie.save('token', data.data.token, {
Copy link
Contributor

Choose a reason for hiding this comment

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

please use lodash/get for data.data.token so your code doesn't break when your server fails

expires: createExpiresCookie(minutesOfCookieLive),
secure: false,
});
return {
type: LOGIN_SUCCESS,
data,
};
}
export function alertShow(text) {
return {
type: ALERT_SHOW,
text,
};
}
export function alertHide() {
return {
type: ALERT_HIDE,
};
}
export function alertCreator(text) {
const millisecondsToAlertDisapear = 3000;
Copy link
Contributor

Choose a reason for hiding this comment

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

Hm, better move this constant one level out

return dispatch => {
dispatch(alertShow(text));
setTimeout(() => {
dispatch(alertHide());
}, millisecondsToAlertDisapear);
};
}

export function loginFailure(data) {
return {
type: LOGIN_FAILURE,
data,
};
}
export function login(loginValue) {
return async dispatch => {
dispatch(loginStart());
try {
const payload = await fetchAsync(`${apiURL}/login`, 'POST', loginValue);
if (!payload.data) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd expect you to check HTTP response status code... If you provide any on error

dispatch(alertCreator(payload.message));
return dispatch(loginFailure(payload));
}
dispatch(alertCreator(payload.message));
return dispatch(loginSuccess(payload));
} catch (error) {
dispatch(alertCreator('Wow, some error appear'));
return dispatch(loginFailure(error));
}
};
}

export function registrationStart() {
return {
type: REGISTRATION_START,
};
}
export function registrationSuccess(data) {
cookie.save('token', data.data.token, {
Copy link
Contributor

Choose a reason for hiding this comment

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

please use lodash/get for data.data.token so your code doesn't break when your server fails

expires: createExpiresCookie(minutesOfCookieLive),
secure: false,
});
return {
type: REGISTRATION_SUCCESS,
data,
};
}

export function registrationFailure(data) {
return {
type: REGISTRATION_FAILURE,
data,
};
}

export function registration(regitrationValue) {
Copy link
Contributor

Choose a reason for hiding this comment

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

typo (regitration)

return async dispatch => {
dispatch(registrationStart());
try {
const payload = await fetchAsync(
`${apiURL}/registration`,
'POST',
regitrationValue,
);
if (!payload.data) {
dispatch(alertCreator(payload.message));
return dispatch(registrationFailure(payload));
}
dispatch(alertCreator(payload.message));
return dispatch(registrationSuccess(payload));
} catch (error) {
dispatch(alertCreator('Wow, some error appear'));
return dispatch(registrationFailure(error));
}
};
}
11 changes: 11 additions & 0 deletions src/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ export const GET_ALL_MESSAGES = 'GET_ALL_MESSAGES';
export const CHAT_ADD_MESSAGE = 'CHAT_ADD_MESSAGE';
export const CHAT_MESSAGE_IS_TYPING = 'CHAT_MESSAGE_IS_TYPING';
export const CHAT_REMOVE_MESSAGE = 'CHAT_REMOVE_MESSAGE';
// login
export const LOGIN_START = 'LOGIN_START';
export const LOGIN_SUCCESS = 'LOGIN_SUCCESS';
export const LOGIN_FAILURE = 'LOGIN_FAILURE';
export const REGISTRATION_SUCCESS = 'REGISTRATION_SUCCESS';
export const REGISTRATION_FAILURE = 'REGISTRATION_FAILURE';
export const REGISTRATION_START = 'REGISTRATION_START';
export const ALERT_SHOW = 'ALERT_SHOW';
export const ALERT_HIDE = 'ALERT_HIDE';

// login end
export const CHAT_UPDATE_MESSAGE_BY_ID = 'CHAT_UPDATE_MESSAGE_BY_ID';
export const EDIT_MESSAGE = 'EDIT_MESSAGE';

Expand Down
2 changes: 2 additions & 0 deletions src/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { combineReducers } from 'redux';
import { reducer as formReducer } from 'redux-form';
import movementDetection from './movement-detection';
import chat from './chat';
import login from './login';
import windowSensors from './window-sensors';

export default combineReducers({
chat,
windowSensors,
movementDetection,
form: formReducer,
login,
});
70 changes: 70 additions & 0 deletions src/reducers/login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import {
LOGIN_START,
LOGIN_SUCCESS,
LOGIN_FAILURE,
REGISTRATION_START,
REGISTRATION_FAILURE,
REGISTRATION_SUCCESS,
ALERT_HIDE,
ALERT_SHOW,
} from '../constants';

const initialState = {
loading: false,
isLogedIn: false,
alert: null,
};
export default function login(state = initialState, action) {
switch (action.type) {
case LOGIN_START:
return {
...state,
loading: true,
};
case LOGIN_SUCCESS:
return {
...state,
loading: false,
...action.data,
isLogedIn: true,
};
case LOGIN_FAILURE:
return {
...state,
loading: false,
...action.data,
isLogedIn: false,
};
case REGISTRATION_START:
return {
...state,
loading: true,
};
case REGISTRATION_FAILURE:
return {
...state,
loading: false,
...action.data,
isLogedIn: false,
};
case REGISTRATION_SUCCESS:
return {
...state,
loading: false,
...action.data,
isLogedIn: true,
};
case ALERT_HIDE:
return {
...state,
alert: null,
};
case ALERT_SHOW:
return {
...state,
alert: action.text,
};
default:
return state;
}
}
14 changes: 13 additions & 1 deletion src/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
import invoke from 'lodash/invoke';
import nth from 'lodash/nth';
// import cookie from 'isomorphic-cookie';

const routes = [
{
action({ next }) {
return next();
},
children: [],
children: [
{
load: () => import('./login'),
path: '/login',
},
],
path: '',
},
{
async action({ next }) {
// Execute each child route until one of them return the result
const route = await next();
if (
// !cookie.load('token')
false // at this time false!!!
) {
route.redirect = '/login';
}

// Provide default values for title, description etc.
route.title = `${route.title || 'Untitled Page'}`;
Expand Down
11 changes: 1 addition & 10 deletions src/routes/login/Login.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,13 @@
}

.formContainer button {
border: none;
border-radius: 5px;
height: 25px;
background: #eee;
margin-right: 10px;
outline: none;
width: 50%;
align-self: center;
}

.formContainer button:hover {
cursor: pointer;
}

.textLogin {
color: #cecece;
margin-top: 15px;
display: flex;
width: 100%;
justify-content: center;
Expand Down
Loading