-
Notifications
You must be signed in to change notification settings - Fork 0
Login #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Login #13
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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'); | ||
|
|
||
| const minutesOfCookieLive = 60; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good approach to naming, but better English would be: |
||
| export function loginStart() { | ||
| return { | ||
| type: LOGIN_START, | ||
| }; | ||
| } | ||
| export function loginSuccess(data) { | ||
| cookie.save('token', data.data.token, { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please use |
||
| 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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
| } | ||
| }; | ||
| } | ||
| 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; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://thumbs.gfycat.com/SmoggyHilariousBaiji-poster.jpg
Use
importplease