actionLogin.js 918 B

1234567891011121314151617181920212223242526272829
  1. import { actionPromise } from "../reducers";
  2. import { gql } from "../helpers";
  3. import { actionAuthLogin } from "../reducers";
  4. import { actionAboutMe } from "./actionAboutMe";
  5. import { actionLogout } from "./actionLogout";
  6. export const actionLogin = (username, password) => async (dispatch, getState) => {
  7. await dispatch(actionLogout());
  8. const token = await dispatch(
  9. actionPromise(
  10. "login",
  11. gql(
  12. `mutation Login($username:String!,$password:String!){
  13. tokenAuth(username:$username,password:$password){
  14. token
  15. }
  16. }`,
  17. { username, password }
  18. )
  19. )
  20. );
  21. if (typeof token === "string") {
  22. await dispatch(actionAuthLogin(token));
  23. } else {
  24. await dispatch(actionAuthLogin(token.token));
  25. }
  26. await dispatch(actionAboutMe());
  27. };