2017-12-21 18:32:51 +01:00
|
|
|
import * as ACTIONS from 'constants/action_types';
|
|
|
|
import * as MODALS from 'constants/modal_types';
|
|
|
|
import Lbryio from 'lbryio';
|
|
|
|
import { doOpenModal, doShowSnackBar } from 'redux/actions/app';
|
2017-12-28 00:48:11 +01:00
|
|
|
import { doClaimRewardType, doRewardList } from 'redux/actions/rewards';
|
2018-01-15 14:32:01 +01:00
|
|
|
import { selectEmailToVerify, selectPhoneToVerify } from 'redux/selectors/user';
|
2017-12-21 18:32:51 +01:00
|
|
|
import rewards from 'rewards';
|
|
|
|
|
|
|
|
export function doFetchInviteStatus() {
|
2017-12-28 00:48:11 +01:00
|
|
|
return dispatch => {
|
2017-12-21 18:32:51 +01:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_INVITE_STATUS_FETCH_STARTED,
|
|
|
|
});
|
|
|
|
|
|
|
|
Lbryio.call('user', 'invite_status')
|
|
|
|
.then(status => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_INVITE_STATUS_FETCH_SUCCESS,
|
|
|
|
data: {
|
|
|
|
invitesRemaining: status.invites_remaining ? status.invites_remaining : 0,
|
|
|
|
invitees: status.invitees,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_INVITE_STATUS_FETCH_FAILURE,
|
|
|
|
data: { error },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2017-06-01 18:20:12 +02:00
|
|
|
|
|
|
|
export function doAuthenticate() {
|
2017-12-28 00:48:11 +01:00
|
|
|
return dispatch => {
|
2017-06-01 18:20:12 +02:00
|
|
|
dispatch({
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.AUTHENTICATION_STARTED,
|
2017-06-08 02:56:52 +02:00
|
|
|
});
|
2017-12-21 18:32:51 +01:00
|
|
|
Lbryio.authenticate()
|
2017-06-08 02:56:52 +02:00
|
|
|
.then(user => {
|
|
|
|
dispatch({
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.AUTHENTICATION_SUCCESS,
|
2017-06-08 02:56:52 +02:00
|
|
|
data: { user },
|
|
|
|
});
|
2017-06-23 07:30:26 +02:00
|
|
|
dispatch(doRewardList());
|
2017-08-20 23:42:00 +02:00
|
|
|
dispatch(doFetchInviteStatus());
|
2017-06-01 18:20:12 +02:00
|
|
|
})
|
2017-06-08 02:56:52 +02:00
|
|
|
.catch(error => {
|
2017-12-21 18:32:51 +01:00
|
|
|
dispatch(doOpenModal(MODALS.AUTHENTICATION_FAILURE));
|
2017-06-08 02:56:52 +02:00
|
|
|
dispatch({
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.AUTHENTICATION_FAILURE,
|
2017-06-08 02:56:52 +02:00
|
|
|
data: { error },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2017-06-02 02:51:52 +02:00
|
|
|
}
|
|
|
|
|
2017-06-08 23:15:34 +02:00
|
|
|
export function doUserFetch() {
|
2017-12-28 00:48:11 +01:00
|
|
|
return dispatch => {
|
2017-06-08 23:15:34 +02:00
|
|
|
dispatch({
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.USER_FETCH_STARTED,
|
2017-06-08 23:15:34 +02:00
|
|
|
});
|
2017-12-21 18:32:51 +01:00
|
|
|
Lbryio.getCurrentUser()
|
2017-06-05 18:29:17 +02:00
|
|
|
.then(user => {
|
2017-06-22 02:09:30 +02:00
|
|
|
dispatch(doRewardList());
|
|
|
|
|
2017-06-08 23:15:34 +02:00
|
|
|
dispatch({
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.USER_FETCH_SUCCESS,
|
2017-06-08 23:15:34 +02:00
|
|
|
data: { user },
|
|
|
|
});
|
2017-06-05 18:29:17 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-06-08 23:15:34 +02:00
|
|
|
dispatch({
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.USER_FETCH_FAILURE,
|
2017-06-08 23:15:34 +02:00
|
|
|
data: { error },
|
|
|
|
});
|
2017-06-05 18:29:17 +02:00
|
|
|
});
|
2017-06-08 23:15:34 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-01-15 14:32:01 +01:00
|
|
|
export function doUserPhoneNew(phone) {
|
|
|
|
return dispatch => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_PHONE_NEW_STARTED,
|
|
|
|
phone,
|
|
|
|
});
|
|
|
|
|
|
|
|
const success = () => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_PHONE_NEW_SUCCESS,
|
|
|
|
data: { phone },
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-01-15 15:30:10 +01:00
|
|
|
const failure = () => {
|
2018-01-15 14:32:01 +01:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_PHONE_NEW_FAILURE,
|
2018-01-15 15:30:10 +01:00
|
|
|
data: { error: 'An error occurred while processing this phone number.' },
|
2018-01-15 14:32:01 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-01-18 03:22:52 +01:00
|
|
|
Lbryio.call('user', 'phone_number_new', { phone_number: phone, country_code: 1 }, 'post').then(
|
2018-01-15 14:32:01 +01:00
|
|
|
success,
|
|
|
|
failure
|
|
|
|
);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doUserPhoneVerifyFailure(error) {
|
|
|
|
return {
|
|
|
|
type: ACTIONS.USER_PHONE_VERIFY_FAILURE,
|
|
|
|
data: { error },
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doUserPhoneVerify(verificationCode) {
|
|
|
|
return (dispatch, getState) => {
|
2018-01-18 03:22:52 +01:00
|
|
|
const phoneNumber = selectPhoneToVerify(getState());
|
2018-01-15 14:32:01 +01:00
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_PHONE_VERIFY_STARTED,
|
|
|
|
code: verificationCode,
|
|
|
|
});
|
|
|
|
|
|
|
|
Lbryio.call(
|
2018-01-18 03:22:52 +01:00
|
|
|
'user',
|
|
|
|
'phone_number_confirm',
|
2018-01-15 14:32:01 +01:00
|
|
|
{
|
|
|
|
verification_code: verificationCode,
|
2018-01-18 03:22:52 +01:00
|
|
|
phone_number: phoneNumber,
|
2018-01-15 14:32:01 +01:00
|
|
|
country_code: '1',
|
|
|
|
},
|
|
|
|
'post'
|
|
|
|
)
|
2018-01-18 03:22:52 +01:00
|
|
|
.then(() => {
|
2018-01-15 14:32:01 +01:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_PHONE_VERIFY_SUCCESS,
|
2018-01-18 03:22:52 +01:00
|
|
|
data: { phone_number: phoneNumber },
|
2018-01-15 14:32:01 +01:00
|
|
|
});
|
|
|
|
dispatch(doUserFetch());
|
|
|
|
})
|
|
|
|
.catch(error => dispatch(doUserPhoneVerifyFailure(error)));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doUserEmailNew(email) {
|
2017-12-28 00:48:11 +01:00
|
|
|
return dispatch => {
|
2017-06-02 02:51:52 +02:00
|
|
|
dispatch({
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.USER_EMAIL_NEW_STARTED,
|
2017-12-13 22:36:30 +01:00
|
|
|
email,
|
2017-06-08 02:56:52 +02:00
|
|
|
});
|
2017-12-07 19:07:30 +01:00
|
|
|
|
|
|
|
const success = () => {
|
|
|
|
dispatch({
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.USER_EMAIL_NEW_SUCCESS,
|
2017-12-07 19:07:30 +01:00
|
|
|
data: { email },
|
|
|
|
});
|
|
|
|
dispatch(doUserFetch());
|
2017-12-13 22:36:30 +01:00
|
|
|
};
|
2017-12-07 19:07:30 +01:00
|
|
|
|
|
|
|
const failure = error => {
|
|
|
|
dispatch({
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.USER_EMAIL_NEW_FAILURE,
|
2017-12-07 19:07:30 +01:00
|
|
|
data: { error },
|
|
|
|
});
|
2017-12-13 22:36:30 +01:00
|
|
|
};
|
2017-12-07 19:07:30 +01:00
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
Lbryio.call('user_email', 'new', { email, send_verification_email: true }, 'post')
|
2017-06-05 18:29:17 +02:00
|
|
|
.catch(error => {
|
2017-12-21 18:32:51 +01:00
|
|
|
if (error.response && error.response.status === 409) {
|
|
|
|
return Lbryio.call(
|
|
|
|
'user_email',
|
|
|
|
'resend_token',
|
|
|
|
{ email, only_if_expired: true },
|
|
|
|
'post'
|
|
|
|
).then(success, failure);
|
2017-06-05 18:29:17 +02:00
|
|
|
}
|
2017-06-21 14:42:22 +02:00
|
|
|
throw error;
|
2017-06-05 18:29:17 +02:00
|
|
|
})
|
2017-12-07 19:07:30 +01:00
|
|
|
.then(success, failure);
|
2017-06-08 02:56:52 +02:00
|
|
|
};
|
2017-06-02 02:51:52 +02:00
|
|
|
}
|
|
|
|
|
2018-01-15 14:32:01 +01:00
|
|
|
export function doUserEmailVerifyFailure(error) {
|
2017-12-26 14:25:26 +01:00
|
|
|
return {
|
|
|
|
type: ACTIONS.USER_EMAIL_VERIFY_FAILURE,
|
|
|
|
data: { error },
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-01-15 14:32:01 +01:00
|
|
|
export function doUserEmailVerify(verificationToken, recaptcha) {
|
2017-12-28 00:48:11 +01:00
|
|
|
return (dispatch, getState) => {
|
2017-06-08 02:56:52 +02:00
|
|
|
const email = selectEmailToVerify(getState());
|
|
|
|
|
2017-06-03 01:09:52 +02:00
|
|
|
dispatch({
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.USER_EMAIL_VERIFY_STARTED,
|
2017-06-08 02:56:52 +02:00
|
|
|
code: verificationToken,
|
2017-12-26 14:25:26 +01:00
|
|
|
recaptcha,
|
2017-06-08 02:56:52 +02:00
|
|
|
});
|
2017-06-03 01:09:52 +02:00
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
Lbryio.call(
|
|
|
|
'user_email',
|
|
|
|
'confirm',
|
2017-12-26 14:25:26 +01:00
|
|
|
{
|
|
|
|
verification_token: verificationToken,
|
|
|
|
email,
|
|
|
|
recaptcha,
|
|
|
|
},
|
2017-12-21 18:32:51 +01:00
|
|
|
'post'
|
|
|
|
)
|
2017-06-08 02:56:52 +02:00
|
|
|
.then(userEmail => {
|
|
|
|
if (userEmail.is_verified) {
|
|
|
|
dispatch({
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.USER_EMAIL_VERIFY_SUCCESS,
|
2017-06-08 02:56:52 +02:00
|
|
|
data: { email },
|
|
|
|
});
|
2017-12-21 18:32:51 +01:00
|
|
|
dispatch(doUserFetch());
|
2017-06-08 02:56:52 +02:00
|
|
|
} else {
|
2017-12-21 18:32:51 +01:00
|
|
|
throw new Error('Your email is still not verified.'); // shouldn't happen
|
2017-06-08 02:56:52 +02:00
|
|
|
}
|
2017-06-23 19:47:07 +02:00
|
|
|
})
|
2018-01-15 14:32:01 +01:00
|
|
|
.catch(error => dispatch(doUserEmailVerifyFailure(error)));
|
2017-06-08 02:56:52 +02:00
|
|
|
};
|
|
|
|
}
|
2017-07-19 01:00:13 +02:00
|
|
|
|
|
|
|
export function doUserIdentityVerify(stripeToken) {
|
2017-12-28 00:48:11 +01:00
|
|
|
return dispatch => {
|
2017-07-19 01:00:13 +02:00
|
|
|
dispatch({
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.USER_IDENTITY_VERIFY_STARTED,
|
2017-07-19 01:00:13 +02:00
|
|
|
token: stripeToken,
|
|
|
|
});
|
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
Lbryio.call('user', 'verify_identity', { stripe_token: stripeToken }, 'post')
|
2017-07-19 01:00:13 +02:00
|
|
|
.then(user => {
|
|
|
|
if (user.is_identity_verified) {
|
|
|
|
dispatch({
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.USER_IDENTITY_VERIFY_SUCCESS,
|
2017-07-19 01:00:13 +02:00
|
|
|
data: { user },
|
|
|
|
});
|
2017-07-25 00:59:26 +02:00
|
|
|
dispatch(doClaimRewardType(rewards.TYPE_NEW_USER));
|
2017-07-19 01:00:13 +02:00
|
|
|
} else {
|
2017-12-21 18:32:51 +01:00
|
|
|
throw new Error('Your identity is still not verified. This should not happen.'); // shouldn't happen
|
2017-07-19 01:00:13 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-07-20 03:07:17 +02:00
|
|
|
dispatch({
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.USER_IDENTITY_VERIFY_FAILURE,
|
2017-07-20 03:07:17 +02:00
|
|
|
data: { error: error.toString() },
|
|
|
|
});
|
2017-07-19 01:00:13 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2017-07-21 23:45:37 +02:00
|
|
|
|
2017-07-21 08:23:39 +02:00
|
|
|
export function doFetchAccessToken() {
|
2017-12-28 00:48:11 +01:00
|
|
|
return dispatch => {
|
2017-07-21 08:23:39 +02:00
|
|
|
const success = token =>
|
|
|
|
dispatch({
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.FETCH_ACCESS_TOKEN_SUCCESS,
|
2017-07-21 08:23:39 +02:00
|
|
|
data: { token },
|
|
|
|
});
|
2017-12-21 18:32:51 +01:00
|
|
|
Lbryio.getAuthToken().then(success);
|
2017-08-18 05:31:44 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doUserInviteNew(email) {
|
2017-12-28 00:48:11 +01:00
|
|
|
return dispatch => {
|
2017-08-18 05:31:44 +02:00
|
|
|
dispatch({
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.USER_INVITE_NEW_STARTED,
|
2017-08-18 05:31:44 +02:00
|
|
|
});
|
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
Lbryio.call('user', 'invite', { email }, 'post')
|
|
|
|
.then(() => {
|
2017-08-18 05:31:44 +02:00
|
|
|
dispatch({
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.USER_INVITE_NEW_SUCCESS,
|
2017-08-18 05:31:44 +02:00
|
|
|
data: { email },
|
|
|
|
});
|
|
|
|
|
|
|
|
dispatch(
|
|
|
|
doShowSnackBar({
|
2017-12-21 18:32:51 +01:00
|
|
|
message: __('Invite sent to %s', email),
|
2017-08-18 05:31:44 +02:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
dispatch(doFetchInviteStatus());
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
dispatch({
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.USER_INVITE_NEW_FAILURE,
|
2017-08-18 05:31:44 +02:00
|
|
|
data: { error },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|