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

133 lines
3 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 },
});
2017-06-03 01:09:52 +02:00
2017-06-08 02:56:52 +02:00
dispatch(doRewardList()); //FIXME - where should this happen?
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.setCurrentUser(
user => {
dispatch({
type: types.USER_FETCH_SUCCESS,
data: { user },
});
},
error => {
dispatch({
type: types.USER_FETCH_FAILURE,
data: { error },
});
}
);
};
}
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.call("user_email", "new", { email }, "post").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());
2017-06-08 02:56:52 +02:00
},
error => {
if (
error.xhr &&
(error.xhr.status == 409 ||
error.message == "This email is already in use")
) {
dispatch({
type: types.USER_EMAIL_NEW_EXISTS,
data: { email },
});
} else {
dispatch({
type: types.USER_EMAIL_NEW_FAILURE,
data: { error: error.message },
});
}
}
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-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
const failure = error => {
2017-06-03 01:09:52 +02:00
dispatch({
type: types.USER_EMAIL_VERIFY_FAILURE,
2017-06-08 02:56:52 +02:00
data: { error: error.message },
});
};
2017-06-03 01:09:52 +02:00
2017-06-08 02:56:52 +02:00
lbryio
.call(
"user_email",
"confirm",
{ verification_token: verificationToken, email: email },
"post"
)
.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 {
failure(new Error("Your email is still not verified.")); //shouldn't happen?
}
}, failure);
};
}