actionLogin.jsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { actionPromise } from "./actionsPromise";
  2. import { gql } from "../helpers/gql";
  3. import { history } from '../App';
  4. import { actionAboutMe } from "./actionAboutMe";
  5. export const socket = window.io("ws://chat.ed.asmer.org.ua/")
  6. const actionAuthLogin = (token) => ({type: 'AUTH_LOGIN', token});
  7. export const actionAuthLogout = () => {
  8. history.push('/')
  9. return ({type: 'AUTH_LOGOUT'})
  10. }
  11. export const actionFullLogin = (log, pass) =>
  12. async (dispatch) => {
  13. let token = await dispatch(
  14. actionPromise('login', gql(`query login($login: String, $password: String) {
  15. login(login: $login, password: $password)
  16. }`, {login: log, password: pass}))
  17. )
  18. if(token){
  19. socket.emit('jwt', token)
  20. dispatch(actionAuthLogin(token))
  21. history.push("/");
  22. }
  23. return token
  24. }
  25. export const actionCheckPassword = (log, pass) =>
  26. async (dispatch) => {
  27. let token = await dispatch(
  28. actionPromise('checkedPassword', gql(`query login($login: String, $password: String) {
  29. login(login: $login, password: $password)
  30. }`, {login: log, password: pass}))
  31. );
  32. console.log(token)
  33. }
  34. export const actionFullRegister = (log, pass, nick) =>
  35. async dispatch => {
  36. let user = await dispatch(
  37. actionPromise('register', gql( `mutation register($user: UserInput) {
  38. UserUpsert(user: $user) {
  39. _id
  40. login
  41. }
  42. }`, {
  43. user: {
  44. login: log,
  45. password: pass,
  46. nick: nick
  47. }
  48. }))
  49. )
  50. if(user){
  51. dispatch(actionFullLogin(log, pass));
  52. }
  53. }