Skip to content

Commit

Permalink
feat: added forum auth integration with directory
Browse files Browse the repository at this point in the history
  • Loading branch information
madan-ideas2it committed Nov 2, 2023
1 parent 46e87c1 commit ff5791f
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 31 deletions.
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,7 @@ LOGIN_URL=
NEXT_PUBLIC_TOKEN_EXPIRY_CHECK_IN_MINUTES= // in minutes

#Announcement banner json fetch token
ANNOUNCEMENT_S3_AUTH_TOKEN=
ANNOUNCEMENT_S3_AUTH_TOKEN=

#Cookie domain
COOKIE_DOMAIN= // domain
15 changes: 12 additions & 3 deletions apps/web-app/components/auth/change-email-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,18 @@ function ChangeEmailModal(props) {
if (refreshToken && accessToken) {
const accessTokenExpiry = decodeToken(accessToken);
const refreshTokenExpiry = decodeToken(refreshToken);
Cookies.set('authToken', JSON.stringify(accessToken), { expires: calculateExpiry(new Date(accessTokenExpiry.exp)) })
Cookies.set('refreshToken', JSON.stringify(refreshToken), { expires: calculateExpiry(new Date(refreshTokenExpiry.exp)) })
Cookies.set('userInfo', JSON.stringify(userInfo), { expires: calculateExpiry(new Date(accessTokenExpiry.exp)) })
Cookies.set('authToken', JSON.stringify(accessToken), {
expires: calculateExpiry(new Date(accessTokenExpiry.exp)),
domain: process.env.COOKIE_DOMAIN || ''
});
Cookies.set('refreshToken', JSON.stringify(refreshToken), {
expires: calculateExpiry(new Date(refreshTokenExpiry.exp)),
domain: process.env.COOKIE_DOMAIN || ''
});
Cookies.set('userInfo', JSON.stringify(userInfo), {
expires: calculateExpiry(new Date(accessTokenExpiry.exp)),
domain: process.env.COOKIE_DOMAIN || ''
});
}
}

Expand Down
35 changes: 24 additions & 11 deletions apps/web-app/components/auth/email-otp-verification-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,18 @@ function EmailOtpVerificationModal() {
if (refreshToken && accessToken) {
const accessTokenExpiry = decodeToken(accessToken);
const refreshTokenExpiry = decodeToken(refreshToken);
Cookies.set('authToken', JSON.stringify(accessToken), { expires: calculateExpiry(new Date(accessTokenExpiry.exp)) })
Cookies.set('refreshToken', JSON.stringify(refreshToken), { expires: calculateExpiry(new Date(refreshTokenExpiry.exp)) })
Cookies.set('userInfo', JSON.stringify(userInfo), { expires: calculateExpiry(new Date(accessTokenExpiry.exp)) })
Cookies.set('authToken', JSON.stringify(accessToken), {
expires: calculateExpiry(new Date(accessTokenExpiry.exp)),
domain: process.env.COOKIE_DOMAIN || ''
});
Cookies.set('refreshToken', JSON.stringify(refreshToken), {
expires: calculateExpiry(new Date(refreshTokenExpiry.exp)),
domain: process.env.COOKIE_DOMAIN || ''
});
Cookies.set('userInfo', JSON.stringify(userInfo), {
expires: calculateExpiry(new Date(accessTokenExpiry.exp)),
domain: process.env.COOKIE_DOMAIN || ''
});
}
}

Expand All @@ -50,14 +59,17 @@ function EmailOtpVerificationModal() {
const data = await verifyEmailOtp(otpPayload, header)
setLoaderStatus(false)
if (data?.userInfo) {
const externalRedirectUrl = Cookies.get('external_redirect_url');
setNewTokensAndUserInfo(data);
clearAllOtpSessionVaribles()
clearAllOtpSessionVaribles();
analytics.captureEvent(APP_ANALYTICS_EVENTS.USER_VERIFICATION_SUCCESS, {})
setDialogStatus(false);
localStorage.removeItem('otp-verification-email');
localStorage.setItem('otp-verify', 'success')
if (data?.userInfo?.isFirstTimeLogin) {
window.location.href = PAGE_ROUTES.SETTINGS;
localStorage.setItem('otp-verify', 'success');
if (externalRedirectUrl) {
window.location.href = externalRedirectUrl;
} else if (data?.userInfo?.isFirstTimeLogin) {
window.location.href = PAGE_ROUTES.SETTINGS;
} else {
window.location.reload();
}
Expand Down Expand Up @@ -193,9 +205,10 @@ function EmailOtpVerificationModal() {
}

const clearAllOtpSessionVaribles = () => {
Cookies.remove('clientToken')
Cookies.remove('uniqueEmailVerifyToken')
Cookies.remove('show-email-verification-box')
Cookies.remove('clientToken');
Cookies.remove('uniqueEmailVerifyToken');
Cookies.remove('show-email-verification-box');
Cookies.remove('external_redirect_url');
localStorage.removeItem('resend-expiry');
localStorage.removeItem('otp-verification-step');
}
Expand Down Expand Up @@ -272,4 +285,4 @@ function EmailOtpVerificationModal() {
</>
}

export default EmailOtpVerificationModal
export default EmailOtpVerificationModal
35 changes: 25 additions & 10 deletions apps/web-app/pages/directory/members/verify-member.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const getServerSideProps = async (
ctx
) => {
const { query } = ctx;
const { state, code, error, landingPage=PAGE_ROUTES.TEAMS, source} = query;
const { state, code, error, landingPage=PAGE_ROUTES.TEAMS, externalRedirectUrl, source} = query;
const cookies = nookies.get(ctx);
// validating state which we gave to auth service to get auth code.
if (cookies.state && cookies.state != state && source != "direct") {
Expand Down Expand Up @@ -90,15 +90,18 @@ export const getServerSideProps = async (
const refreshTokenExpiry = decodeToken(refreshToken);
setCookie(ctx, 'authToken', JSON.stringify(accessToken), {
maxAge: calculateExpiry(accessTokenExpiry.exp),
path: '/'
path: '/',
domain: process.env.COOKIE_DOMAIN || ''
});
setCookie(ctx, 'refreshToken', JSON.stringify(refreshToken), {
maxAge: calculateExpiry(refreshTokenExpiry.exp),
path: '/'
path: '/',
domain: process.env.COOKIE_DOMAIN || ''
});
setCookie(ctx, 'userInfo', JSON.stringify(userInfo), {
maxAge: calculateExpiry(accessTokenExpiry.exp),
path: '/'
path: '/',
domain: process.env.COOKIE_DOMAIN || ''
});
setCookie(ctx, 'verified', 'true' , {
path: '/'
Expand All @@ -107,37 +110,49 @@ export const getServerSideProps = async (
return {
redirect: {
permanent: false,
destination: PAGE_ROUTES.SETTINGS,
destination: externalRedirectUrl ? externalRedirectUrl: PAGE_ROUTES.SETTINGS,
},
};
}
} else if (isAccountLinking && accessToken && refreshToken) {
const accessTokenExpiry = decodeToken(accessToken);
const refreshTokenExpiry = decodeToken(refreshToken);


setCookie(ctx, 'authToken', accessToken, {
maxAge: calculateExpiry(accessTokenExpiry.exp),
path: '/'
path: '/',
domain: process.env.COOKIE_DOMAIN || ''
});
setCookie(ctx, 'idToken', idToken, {
maxAge: calculateExpiry(accessTokenExpiry.exp),
path: '/'
});
setCookie(ctx, 'refreshToken', refreshToken, {
maxAge: calculateExpiry(refreshTokenExpiry.exp),
path: '/'
path: '/',
domain: process.env.COOKIE_DOMAIN || ''
});

setCookie(ctx, 'show-email-verification-box', 'true', {
maxAge: calculateExpiry(accessTokenExpiry.exp),
path: '/'
});

setCookie(ctx, 'external_redirect_url', externalRedirectUrl, {
maxAge: calculateExpiry(accessTokenExpiry.exp),
path: '/'
});
return {
redirect: {
permanent: false,
destination: landingPage
}
};
}
return {
redirect: {
permanent: false,
destination: landingPage
},
destination: externalRedirectUrl ? externalRedirectUrl : landingPage
}
};
};
9 changes: 6 additions & 3 deletions apps/web-app/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,18 @@ api.interceptors.request.use(async (config) => {

setCookie(null, 'authToken', JSON.stringify(accessToken), {
maxAge: calculateExpiry(access_token.exp),
path: '/'
path: '/',
domain: process.env.COOKIE_DOMAIN || ''
});
setCookie(null, 'refreshToken', JSON.stringify(refreshToken), {
maxAge: calculateExpiry(refresh_token.exp),
path: '/'
path: '/',
domain: process.env.COOKIE_DOMAIN || ''
});
setCookie(null, 'userInfo', JSON.stringify(userInfo), {
maxAge: calculateExpiry(access_token.exp),
path: '/'
path: '/',
domain: process.env.COOKIE_DOMAIN || ''
});
config.headers['Authorization'] = `Bearer ${accessToken}`.replace(
/"/g,
Expand Down
9 changes: 6 additions & 3 deletions apps/web-app/utils/services/auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,18 @@ export const renewAndStoreNewAccessToken = async (refrshToken, ctx) => {
const refresh_token = decodeToken(refreshToken);
setCookie(ctx, 'authToken', JSON.stringify(accessToken), {
maxAge: calculateExpiry(access_token.exp),
path: '/'
path: '/',
domain: process.env.COOKIE_DOMAIN || ''
});
setCookie(ctx, 'refreshToken', JSON.stringify(refreshToken), {
maxAge: calculateExpiry(refresh_token.exp),
path: '/'
path: '/',
domain: process.env.COOKIE_DOMAIN || ''
});
setCookie(ctx, 'userInfo', JSON.stringify(userInfo), {
maxAge: calculateExpiry(access_token.exp),
path: '/'
path: '/',
domain: process.env.COOKIE_DOMAIN || ''
});
}
}
Expand Down

0 comments on commit ff5791f

Please sign in to comment.