actionLogin.js 1.7 KB

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