Skip to content

Commit

Permalink
Merge pull request #2583 from glific/last-login
Browse files Browse the repository at this point in the history
added last login time
  • Loading branch information
mdshamoon authored Oct 11, 2023
2 parents e667866 + 2feb659 commit 3e42d0e
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 22 deletions.
10 changes: 5 additions & 5 deletions src/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ describe('<App /> ', () => {
const tokenExpiryDate = new Date();
tokenExpiryDate.setDate(new Date().getDate() + 1);

setAuthSession(
'{"access_token":"access","renewal_token":"renew", "token_expiry_time":"' +
tokenExpiryDate +
'"}'
);
setAuthSession({
access_token: 'access',
renewal_token: 'renew',
token_expiry_time: tokenExpiryDate,
});

setUserSession(JSON.stringify({ organization: { id: '1' }, roles: ['Staff'] }));

Expand Down
2 changes: 2 additions & 0 deletions src/components/UI/Layout/Navigation/SideDrawer/SideDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import GlificLogo from 'assets/images/logo/Logo.svg';
import { getUserRolePermissions, getUserAccountMenus, getStaffManagementMenus } from 'context/role';
import { Tooltip } from 'components/UI/Tooltip/Tooltip';
import { WalletBalance } from 'containers/WalletBalance/WalletBalance';
import { LastLogin } from 'containers/LastLogin/LastLogin';
import { isGreaterThanLgBreakpoint } from 'common/utils';
import SideMenus from '../SideMenus/SideMenus';

Expand Down Expand Up @@ -183,6 +184,7 @@ export const SideDrawer = () => {
</div>
</div>
{drawer}
<LastLogin drawerOpen={drawerOpen} />
<WalletBalance fullOpen={drawerOpen} />
</Drawer>
</nav>
Expand Down
2 changes: 1 addition & 1 deletion src/config/apolloclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const gqlClient = (navigate: any) => {
// in case of successful token renewal
if (response.data) {
// lets set the session
setAuthSession(JSON.stringify(response.data.data));
setAuthSession(response.data.data);

// we need to return below as handleFetch expects it
tokenResponse[accessTokenField] = response.data.data.access_token;
Expand Down
3 changes: 1 addition & 2 deletions src/containers/Auth/Login/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,9 @@ export const Login = () => {
},
})
.then((response: any) => {
const responseString = JSON.stringify(response.data.data);
getCurrentUser();
getOrganizationServices();
setAuthSession(responseString);
setAuthSession(response.data.data);
})
.catch((error) => {
setAuthError(t('Invalid phone or password.'));
Expand Down
8 changes: 8 additions & 0 deletions src/containers/LastLogin/LastLogin.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.lastLogin {
position: absolute;
bottom: 26px;
font-size: 12px;
padding-left: 22px;
color: #073f24;
font-weight: 500;
}
21 changes: 21 additions & 0 deletions src/containers/LastLogin/LastLogin.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import moment from 'moment';
import { useTranslation } from 'react-i18next';

import { getAuthSession } from 'services/AuthService';
import { DATE_TIME_FORMAT } from 'common/constants';

import styles from './LastLogin.module.css';

interface LastLoginProps {
drawerOpen: boolean;
}

export const LastLogin = ({ drawerOpen }: LastLoginProps) => {
const { t } = useTranslation();
const lastLogin = getAuthSession('last_login_time');
return drawerOpen ? (
<div className={styles.lastLogin}>
{t('Last login')}: {moment(lastLogin).format(DATE_TIME_FORMAT)}
</div>
) : null;
};
1 change: 1 addition & 0 deletions src/i18n/en/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@
"Add New": "Add New",
"Add another list": "Add another list",
"Add another list item": "Add another list item",
"Last login": "Last login",
"Delete": "Delete",
"Sorry, no results found! Please try a different search.": "Sorry, no results found! Please try a different search.",
"Please create one.": "Please create one.",
Expand Down
19 changes: 10 additions & 9 deletions src/services/AuthService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ describe('AuthService', () => {
// let's create token expiry date for tomorrow
const tokenExpiryDate = new Date();
tokenExpiryDate.setDate(new Date().getDate() + 1);
const session =
'{"access_token":"access","renewal_token":"renew", "token_expiry_time":"' +
tokenExpiryDate +
'"}';
const session = {
access_token: 'access',
renewal_token: 'renew',
token_expiry_time: tokenExpiryDate,
};

test('testing renewAuthToken', async () => {
// set the session
Expand Down Expand Up @@ -69,11 +70,11 @@ describe('AuthService', () => {
// set the session
const expiredTokenDate = new Date();
expiredTokenDate.setDate(new Date().getDate() - 1);
setAuthSession(
'{"access_token":"access","renewal_token":"renew", "token_expiry_time":"' +
expiredTokenDate +
'"}'
);
setAuthSession({
access_token: 'access',
renewal_token: 'renew',
token_expiry_time: expiredTokenDate,
});
const response = checkAuthStatusService();
expect(response).toBeFalsy();
});
Expand Down
19 changes: 14 additions & 5 deletions src/services/AuthService.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ export const getAuthSession = (element?: string) => {
case 'access_token':
returnValue = JSON.parse(session).access_token;
break;
case 'last_login_time':
returnValue = JSON.parse(session).last_login_time;
break;
default:
returnValue = session;
}
Expand Down Expand Up @@ -77,8 +80,14 @@ export const checkAuthStatusService = () => {
};

// set authentication session
export const setAuthSession = (session: string) => {
localStorage.setItem('glific_session', session);
export const setAuthSession = (session: object) => {
const lastLoginTime = getAuthSession('last_login_time');
if (lastLoginTime) {
const existingSession = { ...session, last_login_time: lastLoginTime };
localStorage.setItem('glific_session', JSON.stringify(existingSession));
} else {
localStorage.setItem('glific_session', JSON.stringify(session));
}
};

// clear the authentication session
Expand Down Expand Up @@ -171,7 +180,7 @@ export const getOrganizationServices = (service: string) => {
};

export const setAuthHeaders = () => {
// // add authorization header in all calls
// add authorization header in all calls
let renewTokenCalled = false;
let renewCallInProgress = false;

Expand All @@ -193,7 +202,7 @@ export const setAuthHeaders = () => {
const authToken = await renewAuthToken();
if (authToken.data) {
// update localstore
setAuthSession(JSON.stringify(authToken.data.data));
setAuthSession(authToken.data.data);
renewTokenCalled = false;
}
if (parametersCopy[1]) {
Expand Down Expand Up @@ -255,7 +264,7 @@ export const setAuthHeaders = () => {
const authToken = await renewAuthToken();
if (authToken.data) {
// update localstore
setAuthSession(JSON.stringify(authToken.data.data));
setAuthSession(authToken.data.data);
renewTokenCalled = false;
}
this.setRequestHeader('authorization', getAuthSession('access_token'));
Expand Down

0 comments on commit 3e42d0e

Please sign in to comment.