Handling non safari browsers

This commit is contained in:
Belen Curcio
2017-05-24 17:20:23 -03:00
parent 8266c65043
commit bd21719ea5
5 changed files with 67 additions and 31 deletions
+30 -12
View File
@@ -1,28 +1,44 @@
import browser from 'detect-browser';
import * as actions from '../constants/auth';
import * as Storage from 'coral-framework/helpers/storage';
import coralApi from 'coral-framework/helpers/response';
import * as Storage from 'coral-framework/helpers/storage';
import {handleAuthToken} from 'coral-framework/actions/auth';
//==============================================================================
// SIGN IN
//==============================================================================
export const handleLogin = (email, password, recaptchaResponse) => (dispatch) => {
export const handleLogin = (email, password, recaptchaResponse) => dispatch => {
dispatch({type: actions.LOGIN_REQUEST});
const params = {method: 'POST', body: {email, password}};
const params = {
method: 'POST',
body: {
email,
password
}
};
if (recaptchaResponse) {
params.headers = {'X-Recaptcha-Response': recaptchaResponse};
params.headers = {
'X-Recaptcha-Response': recaptchaResponse
};
}
return coralApi('/auth/local', params)
.then(({user, token}) => {
if (!user) {
Storage.removeItem('token');
if (!browser || browser.name !== 'safari') {
Storage.removeItem('token');
}
return dispatch(checkLoginFailure('not logged in'));
}
dispatch(handleAuthToken(token));
dispatch(checkLoginSuccess(user));
})
.catch((error) => {
.catch(error => {
if (error.translation_key === 'LOGIN_MAXIMUM_EXCEEDED') {
dispatch({
type: actions.LOGIN_MAXIMUM_EXCEEDED,
@@ -50,11 +66,11 @@ const forgotPassowordFailure = () => ({
type: actions.FETCH_FORGOT_PASSWORD_FAILURE
});
export const requestPasswordReset = (email) => (dispatch) => {
export const requestPasswordReset = email => dispatch => {
dispatch(forgotPassowordRequest(email));
return coralApi('/account/password/reset', {method: 'POST', body: {email}})
.then(() => dispatch(forgotPassowordSuccess()))
.catch((error) => dispatch(forgotPassowordFailure(error)));
.catch(error => dispatch(forgotPassowordFailure(error)));
};
//==============================================================================
@@ -71,23 +87,25 @@ const checkLoginSuccess = (user, isAdmin) => ({
isAdmin
});
const checkLoginFailure = (error) => ({
const checkLoginFailure = error => ({
type: actions.CHECK_LOGIN_FAILURE,
error
});
export const checkLogin = () => (dispatch) => {
export const checkLogin = () => dispatch => {
dispatch(checkLoginRequest());
return coralApi('/auth')
.then(({user}) => {
if (!user) {
Storage.removeItem('token');
if (!browser || browser.name !== 'safari') {
Storage.removeItem('token');
}
return dispatch(checkLoginFailure('not logged in'));
}
dispatch(checkLoginSuccess(user));
})
.catch((error) => {
.catch(error => {
console.error(error);
dispatch(checkLoginFailure(`${error.translation_key}`));
});
+15 -9
View File
@@ -1,14 +1,14 @@
import {pym} from 'coral-framework';
import * as Storage from '../helpers/storage';
import * as actions from '../constants/auth';
import coralApi, {base} from '../helpers/response';
import jwtDecode from 'jwt-decode';
import {pym} from 'coral-framework';
import browser from 'detect-browser';
import * as actions from '../constants/auth';
import * as Storage from '../helpers/storage';
import coralApi, {base} from '../helpers/response';
const lang = new I18n(translations);
import translations from './../translations';
import I18n from '../../coral-framework/modules/i18n/i18n';
// Dialog Actions
export const showSignInDialog = () => (dispatch) => {
const signInPopUp = window.open(
'/embed/stream/login',
@@ -112,8 +112,10 @@ const signInFailure = (error) => ({
//==============================================================================
export const handleAuthToken = (token) => (dispatch) => {
Storage.setItem('exp', jwtDecode(token).exp);
Storage.setItem('token', token);
if (!browser || browser.name !== 'safari') {
Storage.setItem('exp', jwtDecode(token).exp);
Storage.setItem('token', token);
}
dispatch({type: 'HANDLE_AUTH_TOKEN'});
};
@@ -269,7 +271,9 @@ export const fetchForgotPassword = (email) => (dispatch) => {
export const logout = () => (dispatch) => {
return coralApi('/auth', {method: 'DELETE'}).then(() => {
Storage.removeItem('token');
if (!browser || browser.name !== 'safari') {
Storage.removeItem('token');
}
dispatch({type: actions.LOGOUT});
});
};
@@ -292,7 +296,9 @@ export const checkLogin = () => (dispatch) => {
coralApi('/auth')
.then((result) => {
if (!result.user) {
Storage.removeItem('token');
if (!browser || browser.name !== 'safari') {
Storage.removeItem('token');
}
throw new Error('Not logged in');
}
+17 -10
View File
@@ -1,22 +1,29 @@
import * as Storage from './storage';
import browser from 'detect-browser';
const buildOptions = (inputOptions = {}) => {
const defaultOptions = {
method: 'GET',
headers: {
Accept: 'application/json',
Authorization: `Bearer ${Storage.getItem('token')}`,
'Content-Type': 'application/json'
},
credentials: 'same-origin'
};
let options = Object.assign({}, defaultOptions, inputOptions);
options.headers = Object.assign(
{},
defaultOptions.headers,
inputOptions.headers
);
let options = {
defaultOptions,
...inputOptions
};
if (!browser || browser.name !== 'safari') {
let authorization = localStorage.getItem('token');
if (authorization) {
options.headers = {
Authorization: `Bearer ${authorization}`
};
}
}
if (options.method.toLowerCase() !== 'get') {
options.body = JSON.stringify(options.body);
@@ -25,9 +32,9 @@ const buildOptions = (inputOptions = {}) => {
return options;
};
const handleResp = (res) => {
const handleResp = res => {
if (res.status > 399) {
return res.json().then((err) => {
return res.json().then(err => {
let message = err.message || res.status;
const error = new Error();
+1
View File
@@ -62,6 +62,7 @@
"csurf": "^1.9.0",
"dataloader": "^1.3.0",
"debug": "^2.6.3",
"detect-browser": "^1.7.0",
"dotenv": "^4.0.0",
"ejs": "^2.5.6",
"env-rewrite": "^1.0.2",
+4
View File
@@ -2390,6 +2390,10 @@ destroy@~1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/destroy/-/destroy-1.0.4.tgz#978857442c44749e4206613e37946205826abd80"
detect-browser@^1.7.0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/detect-browser/-/detect-browser-1.7.0.tgz#11758cd6aa07d76c25784036d19154ae0392c3b3"
detect-indent@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/detect-indent/-/detect-indent-4.0.0.tgz#f76d064352cdf43a1cb6ce619c4ee3a9475de208"