2018-10-01 14:01:20 -07:00
|
|
|
import types from './types';
|
|
|
|
import { Dispatch } from 'redux';
|
2018-11-21 21:45:29 -08:00
|
|
|
import * as Sentry from '@sentry/browser';
|
2018-11-07 11:08:42 -08:00
|
|
|
import {
|
|
|
|
createUser as apiCreateUser,
|
2018-12-14 11:36:22 -08:00
|
|
|
checkUserAuth,
|
2018-11-07 11:08:42 -08:00
|
|
|
authUser as apiAuthUser,
|
2018-12-14 11:36:22 -08:00
|
|
|
logoutUser,
|
2018-11-07 11:08:42 -08:00
|
|
|
} from 'api/api';
|
2018-12-14 11:36:22 -08:00
|
|
|
import { User } from 'types';
|
2018-10-01 14:01:20 -07:00
|
|
|
|
2018-12-14 11:36:22 -08:00
|
|
|
function setSentryScope(user: User) {
|
|
|
|
Sentry.configureScope(scope => {
|
|
|
|
scope.setUser({
|
|
|
|
id: user.userid.toString(),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2018-10-30 16:18:26 -07:00
|
|
|
|
2018-12-14 11:36:22 -08:00
|
|
|
// check if user has authenticated session
|
|
|
|
export function checkUser() {
|
2018-10-01 16:22:56 -07:00
|
|
|
return async (dispatch: Dispatch<any>) => {
|
2018-12-14 11:36:22 -08:00
|
|
|
dispatch({ type: types.CHECK_USER_PENDING });
|
2018-10-01 16:22:56 -07:00
|
|
|
try {
|
2018-12-14 11:36:22 -08:00
|
|
|
const res = await checkUserAuth();
|
|
|
|
setSentryScope(res.data);
|
|
|
|
dispatch({
|
|
|
|
type: types.CHECK_USER_FULFILLED,
|
|
|
|
payload: {
|
|
|
|
user: res.data,
|
|
|
|
},
|
2018-11-07 11:08:42 -08:00
|
|
|
});
|
2018-12-14 11:36:22 -08:00
|
|
|
} catch (err) {
|
|
|
|
dispatch({
|
|
|
|
type: types.CHECK_USER_REJECTED,
|
|
|
|
payload: err.message || err.toString(),
|
|
|
|
error: true,
|
2018-11-21 21:45:29 -08:00
|
|
|
});
|
2018-12-14 11:36:22 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function authUser(email: string, password: string) {
|
|
|
|
return async (dispatch: Dispatch<any>) => {
|
|
|
|
dispatch({ type: types.AUTH_USER_PENDING });
|
|
|
|
try {
|
|
|
|
const res = await apiAuthUser({ email, password });
|
|
|
|
setSentryScope(res.data);
|
2018-10-01 16:22:56 -07:00
|
|
|
dispatch({
|
|
|
|
type: types.AUTH_USER_FULFILLED,
|
2018-10-03 12:08:14 -07:00
|
|
|
payload: {
|
|
|
|
user: res.data,
|
|
|
|
},
|
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: {
|
|
|
|
email: string;
|
2018-12-14 11:36:22 -08:00
|
|
|
password: string;
|
2018-10-01 16:22:56 -07:00
|
|
|
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 {
|
2018-12-14 11:36:22 -08:00
|
|
|
const res = await apiCreateUser(user);
|
2018-10-01 16:22:56 -07:00
|
|
|
dispatch({
|
|
|
|
type: types.CREATE_USER_FULFILLED,
|
|
|
|
payload: {
|
|
|
|
user: res.data,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
dispatch({
|
|
|
|
type: types.CREATE_USER_REJECTED,
|
|
|
|
payload: err.message || err.toString(),
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-12-14 11:36:22 -08:00
|
|
|
export function logout() {
|
2018-10-01 14:01:20 -07:00
|
|
|
return async (dispatch: Dispatch<any>) => {
|
2018-12-14 11:36:22 -08:00
|
|
|
await dispatch({
|
|
|
|
type: types.LOGOUT,
|
|
|
|
payload: logoutUser(),
|
2018-10-01 14:01:20 -07:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|