login.ts 3.56 KB
Newer Older
Matija Obreza's avatar
Matija Obreza committed
1
import { LoginService } from 'service/LoginService';
2
import * as Constants from 'constants/login';
Matija Obreza's avatar
Matija Obreza committed
3
4
5
import * as cookies from 'es-cookie';
import { clearCookies, saveCookies } from 'utilities';
import { ROLE_CLIENT } from 'constants/userRoles';
6
7
import { log } from 'utilities/debug';
import { configureBackendApi } from 'utilities/requestUtils';
Matija Obreza's avatar
Matija Obreza committed
8
9

export function checkAccessTokens(dispatch) {
10
11
12
13
14
    const cookieToken: string = typeof window !== 'undefined' && cookies.get('access_token');

    const applicationLogin = () =>
        LoginService.loginApp()
            .then((data) => {
15
16
                // console.log('loginApp token', data);
                saveCookies({access_token: data.access_token, authorities: [ROLE_CLIENT]}, data.exp * 1000 || new Date().getTime() + data.expires_in * 1000);
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
                dispatch(loginApp(data));
            })
            .catch((error) => {
                log('Something went wrong', error);
            });

    if (cookieToken) {
        return LoginService.checkToken(cookieToken)
            .then((data) => {
                dispatch(loginUser({ access_token: cookieToken, ...data }));
                return true;
            })
            .catch((error) => {
                clearCookies();
                return applicationLogin();
            });
    } else {
Matija Obreza's avatar
Matija Obreza committed
34
35
        clearCookies();
        return applicationLogin();
36
    }
Matija Obreza's avatar
Matija Obreza committed
37
}
markchkhotua's avatar
markchkhotua committed
38

39
40
41
42
43
44
45
export function loginAppRequest() {
    return (dispatch) => {
        return LoginService.loginApp()
            .then((data) => {
                return dispatch(loginApp(data));
            });
    };
46
47
}

48
49
50
51
52
53
54
55
export function loginApp(d) {
    // console.log('Login app', d);
    configureBackendApi({ accessToken: d.access_token });
    return {
        type: Constants.LOGIN_APP,
        authorities: [ROLE_CLIENT],
        ...d,
    };
markchkhotua's avatar
markchkhotua committed
56
57
}

58
59
60
61
62
63
64
// Login
export const loginUser = (payload) => {
  // console.log('loginUser', payload);
  configureBackendApi({ accessToken: payload.access_token });
  return {
    type: Constants.LOGIN_USER,
    ...payload,
Matija Obreza's avatar
Matija Obreza committed
65
  };
66
};
markchkhotua's avatar
markchkhotua committed
67

68
69
70
71
const checkToken = (payload) => {
  return {
    type: Constants.CHECK_TOKEN,
    ...payload,
Matija Obreza's avatar
Matija Obreza committed
72
  };
73
};
markchkhotua's avatar
markchkhotua committed
74

75
export const loginRequest = (username, password) => (dispatch) => {
Matija Obreza's avatar
Matija Obreza committed
76

77
78
  return LoginService.login(username, password)
    .then((data) => {
79
80
      // console.log('User login token', data);
      saveCookies(data, data.exp * 1000 || new Date().getTime() + data.expires_in * 1000);
81
82
83
      return dispatch(loginUser(data));
    });
};
Valeriy Panov's avatar
Valeriy Panov committed
84

85
86
87
88
89
90
91
92
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}));
    });
};
93

94
95
96
// Google login
// TODO find usage
export const verifyGoogleToken = (payload) => {
Matija Obreza's avatar
Matija Obreza committed
97
  return {
98
99
    type: Constants.VERIFY_GOOGLE_TOKEN,
    ...payload,
Matija Obreza's avatar
Matija Obreza committed
100
  };
101
};
markchkhotua's avatar
markchkhotua committed
102

103
104
export const verifyGoogleTokenRequest = (accessToken) => (dispatch, getState) => {
  const token = getState().login.access_token;
markchkhotua's avatar
markchkhotua committed
105

106
107
  return LoginService.verifyGoogleToken(token, accessToken)
    .then((data) => {
108
109
      // console.log('Verified Google token', data);
      saveCookies(data, data.exp * 1000 || new Date().getTime() + data.expires_in * 1000);
110
111
112
      return dispatch(loginApp(data));
    });
};
markchkhotua's avatar
markchkhotua committed
113

114
115
// Logout
const logout = () => {
Matija Obreza's avatar
Matija Obreza committed
116
  return {
117
    type: Constants.LOGOUT,
Matija Obreza's avatar
Matija Obreza committed
118
  };
119
};
Valeriy Panov's avatar
Valeriy Panov committed
120

121
122
123
124
125
126
127
export const logoutRequest = () => (dispatch, getState) => {
  const token = getState().login.access_token;
  return LoginService.logout(token)
    .then(() => {
      clearCookies();
      return dispatch(logout());
    });
Matija Obreza's avatar
Matija Obreza committed
128
};