public.ts 2.22 KB
Newer Older
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
1
2
3
4
5
6
7
// Actions
import {loginApp} from 'actions/login';

// Constants
import * as Constants from 'constants/login';
// Service
import {LoginService} from 'service/LoginService';
Maxym Borodenko's avatar
Maxym Borodenko committed
8
import {UserService} from 'service/UserService';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
9
10
11
// Util
import {clearCookies, saveCookies} from 'utilities';
import {log} from 'utilities/debug';
12
import { configureBackendApi } from 'utilities/requestUtils';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
13
14
15

// Registration
export const registrationRequest = (email, pass, fullName, captcha) => (dispatch) => {
Maxym Borodenko's avatar
Maxym Borodenko committed
16
   return UserService.register(email, pass, fullName, captcha);
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
17
18
19
20
21
};


// Login
export const loginUser = (payload) => {
22
23
  // console.log('loginUser', payload);
  configureBackendApi({ accessToken: payload.access_token });
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  return {
    type: Constants.LOGIN_USER,
    ...payload,
  };
};

const checkToken = (payload) => {
  return {
    type: Constants.CHECK_TOKEN,
    ...payload,
  };
};

export const loginRequest = (username, password) => (dispatch) => {

  return LoginService.login(username, password)
    .then((data) => {
41
      saveCookies(data, data.exp * 1000 || new Date().getTime() + data.expires_in * 1000);
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
      return dispatch(loginUser(data));
    });
};

export const checkTokenRequest = (token) => (dispatch) => {
  log('checkTokenRequest verifying ', token);
  return LoginService.checkToken(token)
    .then((data) => {
      log('checkTokenRequest got', data);
      return dispatch(checkToken({access_token: token, ...data}));
    });
};

// Google login
// TODO find usage
export const verifyGoogleToken = (payload) => {
  return {
    type: Constants.VERIFY_GOOGLE_TOKEN,
    ...payload,
  };
};

64
export const verifyGoogleTokenRequest = (tokenId) => (dispatch, getState) => {
65
66
  const clientId = getState().applicationConfig.clientId;
  return LoginService.verifyGoogleToken(clientId, tokenId)
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
67
    .then((data) => {
68
      saveCookies(data, data.exp * 1000 || new Date().getTime() + data.expires_in * 1000);
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
      return dispatch(loginApp(data));
    });
};

// Logout
const logout = () => {
  return {
    type: Constants.LOGOUT,
  };
};

export const logoutRequest = () => (dispatch, getState) => {
  const token = getState().login.access_token;
  return LoginService.logout(token)
    .then(() => {
      clearCookies();
      return dispatch(logout());
    });
};