lbry-desktop/ui/js/actions/user.js

185 lines
4.4 KiB
JavaScript
Raw Normal View History

2017-06-08 02:56:52 +02:00
import * as types from "constants/action_types";
2017-08-03 00:55:58 +02:00
import * as modals from "constants/modal_types";
2017-06-08 02:56:52 +02:00
import lbryio from "lbryio";
import { setLocal } from "utils";
2017-08-03 00:55:58 +02:00
import { doOpenModal } from "actions/app";
2017-07-25 00:59:26 +02:00
import { doRewardList, doClaimRewardType } from "actions/rewards";
2017-07-19 01:00:13 +02:00
import { selectEmailToVerify, selectUser } from "selectors/user";
2017-07-25 00:59:26 +02:00
import rewards from "rewards";
2017-06-01 18:20:12 +02:00
export function doAuthenticate() {
return function(dispatch, getState) {
dispatch({
type: types.AUTHENTICATION_STARTED,
2017-06-08 02:56:52 +02:00
});
lbryio
.authenticate()
.then(user => {
dispatch({
type: types.AUTHENTICATION_SUCCESS,
data: { user },
});
dispatch(doRewardList());
2017-06-01 18:20:12 +02:00
})
2017-06-08 02:56:52 +02:00
.catch(error => {
2017-08-03 00:55:58 +02:00
dispatch(doOpenModal(modals.AUTHENTICATION_FAILURE));
2017-06-08 02:56:52 +02:00
dispatch({
type: types.AUTHENTICATION_FAILURE,
data: { error },
});
});
};
}
2017-06-08 23:15:34 +02:00
export function doUserFetch() {
return function(dispatch, getState) {
dispatch({
type: types.USER_FETCH_STARTED,
});
lbryio
.getCurrentUser()
.then(user => {
2017-06-22 02:09:30 +02:00
dispatch(doRewardList());
2017-06-08 23:15:34 +02:00
dispatch({
type: types.USER_FETCH_SUCCESS,
data: { user },
});
})
.catch(error => {
2017-06-08 23:15:34 +02:00
dispatch({
type: types.USER_FETCH_FAILURE,
data: { error },
});
});
2017-06-08 23:15:34 +02:00
};
}
export function doUserEmailNew(email) {
return function(dispatch, getState) {
dispatch({
type: types.USER_EMAIL_NEW_STARTED,
2017-06-08 02:56:52 +02:00
email: email,
});
lbryio
2017-06-21 14:42:22 +02:00
.call(
"user_email",
"new",
{ email: email, send_verification_email: true },
"post"
)
.catch(error => {
if (error.xhr && error.xhr.status == 409) {
2017-06-21 14:42:22 +02:00
return lbryio.call(
"user_email",
"resend_token",
{ email: email, only_if_expired: true },
"post"
);
}
2017-06-21 14:42:22 +02:00
throw error;
})
.then(() => {
dispatch({
2017-06-08 02:56:52 +02:00
type: types.USER_EMAIL_NEW_SUCCESS,
data: { email },
});
2017-06-08 23:15:34 +02:00
dispatch(doUserFetch());
})
.catch(error => {
dispatch({
type: types.USER_EMAIL_NEW_FAILURE,
2017-06-23 19:47:07 +02:00
data: { error },
});
});
2017-06-08 02:56:52 +02:00
};
}
export function doUserEmailDecline() {
return function(dispatch, getState) {
2017-06-08 02:56:52 +02:00
setLocal("user_email_declined", true);
dispatch({
type: types.USER_EMAIL_DECLINE,
2017-06-08 02:56:52 +02:00
});
};
2017-06-03 01:09:52 +02:00
}
2017-06-08 02:56:52 +02:00
export function doUserEmailVerify(verificationToken) {
2017-06-03 01:09:52 +02:00
return function(dispatch, getState) {
2017-06-08 02:56:52 +02:00
const email = selectEmailToVerify(getState());
2017-06-23 19:47:07 +02:00
verificationToken = verificationToken.toString().trim();
2017-06-08 02:56:52 +02:00
2017-06-03 01:09:52 +02:00
dispatch({
type: types.USER_EMAIL_VERIFY_STARTED,
2017-06-08 02:56:52 +02:00
code: verificationToken,
});
2017-06-03 01:09:52 +02:00
2017-06-08 02:56:52 +02:00
lbryio
2017-06-21 14:42:22 +02:00
.call(
"user_email",
"confirm",
{ verification_token: verificationToken, email: email },
"post"
)
2017-06-08 02:56:52 +02:00
.then(userEmail => {
if (userEmail.is_verified) {
dispatch({
type: types.USER_EMAIL_VERIFY_SUCCESS,
data: { email },
});
2017-06-08 23:15:34 +02:00
dispatch(doUserFetch());
2017-06-08 02:56:52 +02:00
} else {
2017-06-23 19:47:07 +02: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
})
.catch(error => {
dispatch({
type: types.USER_EMAIL_VERIFY_FAILURE,
data: { error },
});
});
2017-06-08 02:56:52 +02:00
};
}
2017-07-19 01:00:13 +02:00
export function doUserIdentityVerify(stripeToken) {
return function(dispatch, getState) {
dispatch({
type: types.USER_IDENTITY_VERIFY_STARTED,
token: stripeToken,
});
lbryio
.call("user", "verify_identity", { stripe_token: stripeToken }, "post")
.then(user => {
if (user.is_identity_verified) {
dispatch({
type: types.USER_IDENTITY_VERIFY_SUCCESS,
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 {
throw new Error(
"Your identity is still not verified. This should not happen."
); //shouldn't happen
}
})
.catch(error => {
dispatch({
type: types.USER_IDENTITY_VERIFY_FAILURE,
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() {
return function(dispatch, getState) {
const success = token =>
dispatch({
type: types.FETCH_ACCESS_TOKEN_SUCCESS,
data: { token },
});
lbryio.getAuthToken().then(success);
};
}