Commit 1e3fe909 authored by Matija Obreza's avatar Matija Obreza

Fix: set correct Auth cookie expiry times

parent 6cecf9fb
Pipeline #7417 passed with stages
in 8 minutes and 31 seconds
......@@ -24,10 +24,10 @@ export default function checkAuthToken(req, res, dispatch) {
console.log('Init checkAuthToken method');
return checkAuthTokenRequest(req, dispatch)
.then((data) => {
console.log(`Setting cookie to expire in ${data.exp}ms from`, data);
res.cookie('access_token', data.access_token, { path: '/', maxAge: data.exp || data.expires_in || /* 1hr */ 1000 * 60 * 60 });
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))) });
if (data.authorities) {
res.cookie('authorities', JSON.stringify(data.authorities), { path: '/', maxAge: data.exp || data.expires_in || /* 1hr */ 1000 * 60 * 60 });
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))) });
} else {
res.clearCookie('authorities');
}
......
......@@ -12,7 +12,8 @@ export function checkAccessTokens(dispatch) {
const applicationLogin = () =>
LoginService.loginApp()
.then((data) => {
saveCookies({access_token: data.access_token, authorities: [ROLE_CLIENT]}, data.exp);
// console.log('loginApp token', data);
saveCookies({access_token: data.access_token, authorities: [ROLE_CLIENT]}, data.exp * 1000 || new Date().getTime() + data.expires_in * 1000);
dispatch(loginApp(data));
})
.catch((error) => {
......@@ -75,7 +76,8 @@ export const loginRequest = (username, password) => (dispatch) => {
return LoginService.login(username, password)
.then((data) => {
saveCookies(data, data.exp);
// console.log('User login token', data);
saveCookies(data, data.exp * 1000 || new Date().getTime() + data.expires_in * 1000);
return dispatch(loginUser(data));
});
};
......@@ -103,7 +105,8 @@ export const verifyGoogleTokenRequest = (accessToken) => (dispatch, getState) =>
return LoginService.verifyGoogleToken(token, accessToken)
.then((data) => {
saveCookies(data, data.exp);
// console.log('Verified Google token', data);
saveCookies(data, data.exp * 1000 || new Date().getTime() + data.expires_in * 1000);
return dispatch(loginApp(data));
});
};
......
......@@ -124,7 +124,8 @@ class Header extends React.Component<IHeaderProps | any, any> {
this.props.logoutRequest()
.then(() => this.props.loginAppRequest())
.then((data) => {
saveCookies({access_token: data.access_token, authorities: [ROLE_CLIENT]}, data.exp);
// console.log('Logout', data);
saveCookies({access_token: data.access_token, authorities: [ROLE_CLIENT]}, data.exp * 1000 || new Date().getTime() + data.expires_in * 1000);
this.props.history.push('/login');
});
}
......
import * as React from 'react';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import {saveCookies} from 'utilities';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import * as _ from 'lodash';
import {log} from 'utilities/debug';
import { log } from 'utilities/debug';
import {loginRequest, checkTokenRequest} from 'actions/login';
import { loginRequest, checkTokenRequest } from 'actions/login';
import ContentHeader from 'ui/common/heading/ContentHeader';
......@@ -33,7 +32,6 @@ class LoginContainer extends React.Component<ILoginContainerProps, void> {
return checkTokenRequest(access_token);
})
.then((data) => {
saveCookies(data, data.exp);
history.push('/dashboard');
return false;
}).catch((e) => {
......
......@@ -96,10 +96,11 @@ export function cleanFilters(filter, keysToSkip?): string {
return result;
}
export function saveCookies(resp, exp: number) {
log(`Saving cookies to expire after ${exp}ms`);
cookies.set('access_token', resp.access_token, { path: '/', expires: new Date(new Date().getTime() + exp) });
cookies.set('authorities', JSON.stringify(resp.authorities), { path: '/', expires: new Date(new Date().getTime() + exp) });
export function saveCookies(resp, expireOn: number) {
const expDate = new Date(expireOn);
log(`Saving cookies to expire on ${expDate}`);
cookies.set('access_token', resp.access_token, { path: '/', expires: expDate });
cookies.set('authorities', JSON.stringify(resp.authorities), { path: '/', expires: expDate });
}
export function clearCookies() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment