|
| 1 | +import { toastr } from 'react-redux-toastr'; |
| 2 | +import { |
| 3 | + FLAG_SIGNING_IN, |
| 4 | + FLAG_SIGNING_OUT, |
| 5 | + FLAG_GETTING_AUTHENTICATED, |
| 6 | + SIGN_IN_SUCCEEDED, |
| 7 | + SIGN_OUT_SUCCEEDED, |
| 8 | + IS_AUTHENTICATED_SUCCEEDED, |
| 9 | +} from '../types'; |
| 10 | +import { |
| 11 | + ERROR_SIGNING_IN, |
| 12 | + ERROR_MESSAGE_HEADER, |
| 13 | + ERROR_SIGNING_OUT, |
| 14 | + ERROR_GETTING_AUTHENTICATED, |
| 15 | +} from '../config/messages'; |
| 16 | +import { |
| 17 | + SIGN_IN_CHANNEL, |
| 18 | + SIGN_OUT_CHANNEL, |
| 19 | + IS_AUTHENTICATED_CHANNEL, |
| 20 | +} from '../config/channels'; |
| 21 | +import { createFlag } from './common'; |
| 22 | +import { ERROR_GENERAL } from '../config/errors'; |
| 23 | + |
| 24 | +const flagSigningInUser = createFlag(FLAG_SIGNING_IN); |
| 25 | +const flagSigningOutUser = createFlag(FLAG_SIGNING_OUT); |
| 26 | +const flagGettingAuthenticated = createFlag(FLAG_GETTING_AUTHENTICATED); |
| 27 | + |
| 28 | +const signIn = async ({ username, password }) => async dispatch => { |
| 29 | + try { |
| 30 | + dispatch(flagSigningInUser(true)); |
| 31 | + window.ipcRenderer.send(SIGN_IN_CHANNEL, { username, password }); |
| 32 | + window.ipcRenderer.once(SIGN_IN_CHANNEL, async (event, user) => { |
| 33 | + if (user === ERROR_GENERAL) { |
| 34 | + toastr.error(ERROR_MESSAGE_HEADER, ERROR_SIGNING_IN); |
| 35 | + } else { |
| 36 | + // obtain user cookie |
| 37 | + // await fetch(REACT_APP_GRAASP_LOGIN, { |
| 38 | + // body: `email=${encodeURIComponent( |
| 39 | + // user.username |
| 40 | + // )}&password=${encodeURIComponent(user.password)}`, |
| 41 | + // headers: { |
| 42 | + // 'Content-Type': 'application/x-www-form-urlencoded', |
| 43 | + // }, |
| 44 | + // method: 'post', |
| 45 | + // }); |
| 46 | + |
| 47 | + dispatch({ |
| 48 | + type: SIGN_IN_SUCCEEDED, |
| 49 | + payload: user, |
| 50 | + }); |
| 51 | + } |
| 52 | + dispatch(flagSigningInUser(false)); |
| 53 | + }); |
| 54 | + } catch (e) { |
| 55 | + toastr.error(ERROR_MESSAGE_HEADER, ERROR_SIGNING_IN); |
| 56 | + dispatch(flagSigningInUser(false)); |
| 57 | + } |
| 58 | +}; |
| 59 | + |
| 60 | +const signOutUser = () => dispatch => { |
| 61 | + try { |
| 62 | + dispatch(flagSigningOutUser(true)); |
| 63 | + window.ipcRenderer.send(SIGN_OUT_CHANNEL); |
| 64 | + window.ipcRenderer.once(SIGN_OUT_CHANNEL, (event, response) => { |
| 65 | + if (response === ERROR_GENERAL) { |
| 66 | + toastr.error(ERROR_MESSAGE_HEADER, ERROR_SIGNING_OUT); |
| 67 | + } else { |
| 68 | + dispatch({ |
| 69 | + type: SIGN_OUT_SUCCEEDED, |
| 70 | + payload: response, |
| 71 | + }); |
| 72 | + } |
| 73 | + dispatch(flagSigningOutUser(false)); |
| 74 | + }); |
| 75 | + } catch (e) { |
| 76 | + console.error(e); |
| 77 | + toastr.error(ERROR_MESSAGE_HEADER, ERROR_SIGNING_OUT); |
| 78 | + dispatch(flagSigningOutUser(false)); |
| 79 | + } |
| 80 | +}; |
| 81 | + |
| 82 | +const isAuthenticated = async () => dispatch => { |
| 83 | + try { |
| 84 | + dispatch(flagGettingAuthenticated(true)); |
| 85 | + window.ipcRenderer.send(IS_AUTHENTICATED_CHANNEL); |
| 86 | + window.ipcRenderer.once( |
| 87 | + IS_AUTHENTICATED_CHANNEL, |
| 88 | + (event, authenticated) => { |
| 89 | + if (authenticated === ERROR_GENERAL) { |
| 90 | + toastr.error(ERROR_MESSAGE_HEADER, ERROR_GETTING_AUTHENTICATED); |
| 91 | + } else { |
| 92 | + dispatch({ |
| 93 | + type: IS_AUTHENTICATED_SUCCEEDED, |
| 94 | + payload: authenticated, |
| 95 | + }); |
| 96 | + } |
| 97 | + dispatch(flagGettingAuthenticated(false)); |
| 98 | + } |
| 99 | + ); |
| 100 | + } catch (e) { |
| 101 | + console.error(e); |
| 102 | + toastr.error(ERROR_MESSAGE_HEADER, ERROR_GETTING_AUTHENTICATED); |
| 103 | + dispatch(flagGettingAuthenticated(false)); |
| 104 | + } |
| 105 | +}; |
| 106 | + |
| 107 | +export { signIn, signOutUser, isAuthenticated }; |
0 commit comments