fixes and updates for authentication flow #2

Merged
akinwale merged 2 commits from authentication-flow into master 2018-08-17 20:10:53 +02:00
8 changed files with 10882 additions and 168 deletions

1037
dist/bundle.js vendored

File diff suppressed because it is too large Load diff

9886
package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -15,7 +15,17 @@ export {
doClaimEligiblePurchaseRewards,
doClaimRewardClearError,
} from 'redux/actions/rewards';
export { doFetchInviteStatus, doInstallNew, doAuthenticate, doUserFetch } from 'redux/actions/user';
export {
doFetchInviteStatus,
doInstallNew,
doAuthenticate,
doUserFetch,
doUserEmailNew,
doUserEmailToVerify,
doUserEmailVerifyFailure,
doUserEmailVerify,
doFetchAccessToken,
} from 'redux/actions/user';
// reducers
export { authReducer } from 'redux/reducers/auth';
@ -23,7 +33,7 @@ export { rewardsReducer } from 'redux/reducers/rewards';
export { userReducer } from 'redux/reducers/user';
// selectors
export { selectAuthToken } from 'redux/selectors/auth';
export { selectAuthToken, selectIsAuthenticating } from 'redux/selectors/auth';
export {
makeSelectClaimRewardError,
makeSelectIsRewardClaimPending,

View file

@ -127,6 +127,7 @@ Lbryio.authenticate = () => {
return reject();
});
})
.then(Lbryio.getCurrentUser)
.then(resolve, reject);
});
}

View file

@ -1,4 +1,5 @@
import { ACTIONS, MODALS, Lbry, doNotify } from 'lbry-redux';
import { selectEmailToVerify } from 'redux/selectors/user';
import { doRewardList } from 'redux/actions/rewards';
import Lbryio from 'lbryio';
@ -91,3 +92,103 @@ export function doUserFetch() {
});
};
}
export function doUserEmailToVerify(email) {
return dispatch => {
dispatch({
type: ACTIONS.USER_EMAIL_VERIFY_SET,
data: { email },
});
};
}
export function doUserEmailNew(email) {
return dispatch => {
dispatch({
type: ACTIONS.USER_EMAIL_NEW_STARTED,
email,
});
const success = () => {
dispatch({
type: ACTIONS.USER_EMAIL_NEW_SUCCESS,
data: { email },
});
dispatch(doUserFetch());
};
const failure = error => {
dispatch({
type: ACTIONS.USER_EMAIL_NEW_FAILURE,
data: { error },
});
};
Lbryio.call('user_email', 'new', { email, send_verification_email: true }, 'post')
.catch(error => {
if (error.response && error.response.status === 409) {
return Lbryio.call(
'user_email',
'resend_token',
{ email, only_if_expired: true },
'post'
).then(success, failure);
}
throw error;
})
.then(success, failure);
};
}
export function doUserEmailVerifyFailure(error) {
return {
type: ACTIONS.USER_EMAIL_VERIFY_FAILURE,
data: { error },
};
}
export function doUserEmailVerify(verificationToken, recaptcha) {
return (dispatch, getState) => {
const email = selectEmailToVerify(getState());
dispatch({
type: ACTIONS.USER_EMAIL_VERIFY_STARTED,
code: verificationToken,
recaptcha,
});
Lbryio.call(
'user_email',
'confirm',
{
verification_token: verificationToken,
email,
recaptcha,
},
'post'
)
.then(userEmail => {
if (userEmail.is_verified) {
dispatch({
type: ACTIONS.USER_EMAIL_VERIFY_SUCCESS,
data: { email },
});
dispatch(doUserFetch());
} else {
throw new Error('Your email is still not verified.'); // shouldn't happen
}
})
.catch(error => dispatch(doUserEmailVerifyFailure(error)));
};
}
export function doFetchAccessToken() {
return dispatch => {
const success = token =>
dispatch({
type: ACTIONS.FETCH_ACCESS_TOKEN_SUCCESS,
data: { token },
});
Lbryio.getAuthToken().then(success);
};
}

View file

@ -18,7 +18,7 @@ reducers[ACTIONS.GENERATE_AUTH_TOKEN_STARTED] = state =>
reducers[ACTIONS.GENERATE_AUTH_TOKEN_SUCCESS] = (state, action) =>
Object.assign({}, state, {
authToken: action.authToken,
authToken: action.data.authToken,
authenticating: false,
});

View file

@ -151,6 +151,11 @@ reducers[ACTIONS.USER_EMAIL_VERIFY_FAILURE] = (state, action) =>
emailVerifyErrorMessage: action.data.error,
});
reducers[ACTIONS.USER_EMAIL_VERIFY_SET] = (state, action) =>
Object.assign({}, state, {
emailToVerify: action.data.email,
});
reducers[ACTIONS.USER_IDENTITY_VERIFY_STARTED] = state =>
Object.assign({}, state, {
identityVerifyIsPending: true,

View file

@ -1,5 +1,7 @@
import { createSelector } from 'reselect';
const selectState = state => state.authToken || {};
const selectState = state => state.auth || {};
export const selectAuthToken = createSelector(selectState, state => state.authToken);
export const selectIsAuthenticating = createSelector(selectState, state => state.authenticating);