Skip to content

Commit

Permalink
feat: added check for app id for auth
Browse files Browse the repository at this point in the history
  • Loading branch information
prtkjakhar committed Apr 18, 2024
1 parent bd78c37 commit f7bcf65
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 32 deletions.
1 change: 1 addition & 0 deletions apps/amakrushi/src/components/OTPpage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ const OTPpage: React.FC = () => {
logEvent(analytics, 'console_error', {
error_message: err.message,
});
toast.error(`${t("message.no_link")}`);
}
);
}
Expand Down
73 changes: 42 additions & 31 deletions apps/amakrushi/src/hooks/useLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,42 +24,53 @@ export const useLogin = () => {
// No need to check for auth if access token is not present
if (cookies.access_token) {
const decodedToken: any = jwt.decode(cookies.access_token);
const expires = new Date(decodedToken?.exp * 1000);
// if token not expired then check for auth
if (expires > new Date()) {
const token = cookies.access_token;
axios
.get(`/api/auth?token=${token}`)
.then((response) => {
if (response.data === null) {
toast.error('Invalid Access Token');
removeCookie('access_token', { path: '/' });
localStorage.clear();
sessionStorage.clear();
router.push('/login');
console.log('response null');
} else {
setIsAuthenticated(true);
console.log('authenticated true');
}
})
.catch((err) => {
//@ts-ignore
logEvent(analytics, 'console_error', {
error_message: err.message,
});
removeCookie('access_token', { path: '/' });
localStorage.clear();
sessionStorage.clear();
router.push('/login');
console.log('catch err');
});
} else {
if (
decodedToken.applicationId !==
process.env.NEXT_PUBLIC_USER_SERVICE_APP_ID
) {
removeCookie('access_token', { path: '/' });
localStorage.clear();
sessionStorage.clear();
router.push('/login');
if (typeof window !== 'undefined') window.location.reload();
} else {
const expires = new Date(decodedToken?.exp * 1000);
// if token not expired then check for auth
if (expires > new Date()) {
const token = cookies.access_token;
axios
.get(`/api/auth?token=${token}`)
.then((response) => {
if (response.data === null) {
toast.error('Invalid Access Token');
removeCookie('access_token', { path: '/' });
localStorage.clear();
sessionStorage.clear();
router.push('/login');
console.log('response null');
} else {
setIsAuthenticated(true);
console.log('authenticated true');
}
})
.catch((err) => {
//@ts-ignore
logEvent(analytics, 'console_error', {
error_message: err.message,
});
removeCookie('access_token', { path: '/' });
localStorage.clear();
sessionStorage.clear();
router.push('/login');
console.log('catch err');
});
} else {
removeCookie('access_token', { path: '/' });
localStorage.clear();
sessionStorage.clear();
router.push('/login');
if (typeof window !== 'undefined') window.location.reload();
}
}
}
}, [cookies.access_token, removeCookie, router]);
Expand Down
2 changes: 1 addition & 1 deletion packages/chat-ui/dist/index.js

Large diffs are not rendered by default.

0 comments on commit f7bcf65

Please sign in to comment.