2018-10-01 14:01:20 -07:00
|
|
|
import types from './types';
|
|
|
|
import { Dispatch } from 'redux';
|
|
|
|
import { sleep } from 'utils/helpers';
|
|
|
|
import { AppState } from 'store/reducers';
|
2018-10-01 16:22:56 -07:00
|
|
|
import { createUser as apiCreateUser, getUser as apiGetUser } from 'api/api';
|
2018-10-01 14:01:20 -07:00
|
|
|
|
|
|
|
type GetState = () => AppState;
|
|
|
|
|
2018-10-01 16:22:56 -07:00
|
|
|
export function authUser(address: string) {
|
|
|
|
return async (dispatch: Dispatch<any>) => {
|
2018-10-01 14:01:20 -07:00
|
|
|
dispatch({ type: types.AUTH_USER_PENDING });
|
2018-10-01 16:22:56 -07:00
|
|
|
|
|
|
|
// TODO: Actually auth using a signed token
|
|
|
|
try {
|
|
|
|
const res = await apiGetUser(address);
|
|
|
|
dispatch({
|
|
|
|
type: types.AUTH_USER_FULFILLED,
|
2018-10-03 12:08:14 -07:00
|
|
|
payload: {
|
|
|
|
user: res.data,
|
|
|
|
token: '123fake', // TODO: Use real token
|
|
|
|
},
|
2018-10-01 16:22:56 -07:00
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
dispatch({
|
|
|
|
type: types.AUTH_USER_REJECTED,
|
|
|
|
payload: err.message || err.toString(),
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
2018-10-01 14:01:20 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-10-01 16:22:56 -07:00
|
|
|
export function createUser(user: {
|
|
|
|
address: string;
|
|
|
|
email: string;
|
|
|
|
name: string;
|
|
|
|
title: string;
|
|
|
|
}) {
|
2018-10-01 14:01:20 -07:00
|
|
|
return async (dispatch: Dispatch<any>) => {
|
|
|
|
dispatch({ type: types.CREATE_USER_PENDING });
|
2018-10-01 16:22:56 -07:00
|
|
|
|
|
|
|
try {
|
|
|
|
// TODO: Pass real token
|
|
|
|
const token = Math.random().toString();
|
|
|
|
const res = await apiCreateUser({
|
|
|
|
accountAddress: user.address,
|
|
|
|
emailAddress: user.email,
|
|
|
|
displayName: user.name,
|
|
|
|
title: user.title,
|
|
|
|
token,
|
|
|
|
});
|
|
|
|
dispatch({
|
|
|
|
type: types.CREATE_USER_FULFILLED,
|
|
|
|
payload: {
|
|
|
|
user: res.data,
|
|
|
|
token,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
dispatch({
|
|
|
|
type: types.CREATE_USER_REJECTED,
|
|
|
|
payload: err.message || err.toString(),
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function checkUser(address: string) {
|
|
|
|
return async (dispatch: Dispatch<any>, getState: GetState) => {
|
|
|
|
const checkedUsers = getState().auth.checkedUsers;
|
|
|
|
if (checkedUsers[address] !== undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch({ type: types.CHECK_USER_PENDING });
|
|
|
|
|
|
|
|
try {
|
|
|
|
const res = await apiGetUser(address);
|
|
|
|
dispatch({
|
|
|
|
type: types.CHECK_USER_FULFILLED,
|
|
|
|
payload: {
|
2018-10-01 14:01:20 -07:00
|
|
|
address,
|
2018-10-01 16:22:56 -07:00
|
|
|
user: res.data,
|
2018-10-01 14:01:20 -07:00
|
|
|
},
|
2018-10-01 16:22:56 -07:00
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
if (err.response && err.response.status === 404) {
|
|
|
|
dispatch({
|
|
|
|
type: types.CHECK_USER_FULFILLED,
|
|
|
|
payload: {
|
|
|
|
address,
|
|
|
|
user: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
dispatch({
|
|
|
|
type: types.CHECK_USER_REJECTED,
|
|
|
|
payload: err.message || err.toString(),
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-10-01 14:01:20 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function signToken(address: string) {
|
|
|
|
return async (dispatch: Dispatch<any>) => {
|
|
|
|
// TODO: Implement signing
|
|
|
|
dispatch({ type: types.SIGN_TOKEN_PENDING });
|
|
|
|
await sleep(500);
|
|
|
|
dispatch({
|
|
|
|
type: types.SIGN_TOKEN_FULFILLED,
|
|
|
|
payload: {
|
|
|
|
token: Math.random(),
|
|
|
|
address,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setToken(address: string, signedMessage: string) {
|
|
|
|
// TODO: Check token for errors
|
|
|
|
return {
|
|
|
|
type: types.SIGN_TOKEN_FULFILLED,
|
|
|
|
payload: {
|
|
|
|
token: signedMessage,
|
|
|
|
address,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function logout() {
|
|
|
|
return { type: types.LOGOUT };
|
|
|
|
}
|