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

139 lines
3.1 KiB
JavaScript
Raw Normal View History

2017-06-08 02:56:52 +02:00
import * as types from "constants/action_types";
import lbryio from "lbryio";
import { setLocal } from "utils";
import { doRewardList } from "actions/rewards";
import { selectEmailToVerify } from "selectors/user";
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 => {
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
};
}