checkAuthToken.ts 2.13 KB
Newer Older
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
1
import * as jwt from 'jsonwebtoken';
2
import { loginAppRequest } from 'actions/login';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
3
import {loginUser} from 'user/actions/public';
4
import ApiError from 'model/ApiError';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
5
6
import config from '../config';

Maxym Borodenko's avatar
Maxym Borodenko committed
7
8

function checkAuthTokenRequest(req, dispatch) {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
9
    const token = req.cookies.access_token || config.access_token.access_token;
Maxym Borodenko's avatar
Maxym Borodenko committed
10
    if (token) {
11
        console.log('Checking cookie token', token);
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
12
13
14
15
16
17
18
19
20
        try {
          const parsedToken = jwt.decode(token);
          console.log('Parsed token data', parsedToken);
          dispatch(loginUser({access_token: token, ...parsedToken}));
          return Promise.resolve({access_token: token, ...parsedToken});
        } catch (e) {
          console.log('Error while parsing token:', e.message);
          return dispatch(loginAppRequest());
        }
Maxym Borodenko's avatar
Maxym Borodenko committed
21
    } else {
22
        console.log('Server: No token in cookie, dispatching loginAppRequest');
Maxym Borodenko's avatar
Maxym Borodenko committed
23
24
25
26
27
28
29
30
        return dispatch(loginAppRequest());
    }
}

export default function checkAuthToken(req, res, dispatch) {
    console.log('Init checkAuthToken method');
    return checkAuthTokenRequest(req, dispatch)
        .then((data) => {
31
32
            console.log(`Setting cookie to expire in ${(data.exp || data.expires_in) / 60}min from`, data);
            res.cookie('access_token', data.access_token, { path: '/', expires: new Date(data.exp * 1000 || new Date().getTime() + ((data.expires_in * 1000) || (/* 1hr */ 1000 * 60 * 60))) });
Maxym Borodenko's avatar
Maxym Borodenko committed
33
            if (data.authorities) {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
34
35
              console.log(`Setting authorities cookies: `, data.authorities);
              res.cookie('authorities', JSON.stringify(data.authorities), { path: '/', expires: new Date(data.exp * 1000 || new Date().getTime() + ((data.expires_in * 1000) || (/* 1hr */ 1000 * 60 * 60))) });
Maxym Borodenko's avatar
Maxym Borodenko committed
36
            } else {
37
                res.clearCookie('authorities');
Maxym Borodenko's avatar
Maxym Borodenko committed
38
39
            }
            return data;
40
41
        }).catch((err) => {
            console.log('Failed checkAuthToken, clearing cookies', ApiError.axiosError(err));
42
43
            res.clearCookie('access_token');
            res.clearCookie('authorities');
44
            return Promise.reject({ message: 'Could not authenticate against the API.' });
45
        });
Maxym Borodenko's avatar
Maxym Borodenko committed
46
}