Skip to content

Commit

Permalink
Merge pull request #12 from conversionxl/pawel/feat/sso-redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelkmpt authored Oct 18, 2024
2 parents ba988f1 + 90a01d8 commit 746b4cf
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 4 deletions.
9 changes: 7 additions & 2 deletions packages/hooks-react/src/useContentProtection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import AccountController from '@jwp/ott-common/src/controllers/AccountController
import { useConfigStore } from '@jwp/ott-common/src/stores/ConfigStore';
import { isTruthyCustomParamValue } from '@jwp/ott-common/src/utils/common';
import { useAccountStore } from '@jwp/ott-common/src/stores/AccountStore';
import env from '@jwp/ott-common/src/env';

import { generateJwtSignedContentToken, useOAuth } from './useOAuth';

Expand Down Expand Up @@ -57,11 +58,15 @@ const useContentProtection = <T>(
// if self-signed is enabled in jwp dashboard
// and
// isOAuthMode is enabled and user is logged in and in premium mode
if (!!id && signingEnabled && isOAuthMode && !!user && !!user?.isPremium) {
if (!!id && signingEnabled && isOAuthMode && !!user && (env.APP_OAUTH_UNLOCK_ONLY_PREMIUM ? !!user?.isPremium : true)) {
return generateJwtSignedContentToken(id, `Bearer ${bearerToken}`);
}
},
{ enabled: signingEnabled && enabled && !!id && (isOAuthMode ? !!user?.isPremium : false), keepPreviousData: false, staleTime: 15 * 60 * 1000 },
{
enabled: signingEnabled && enabled && !!id && (isOAuthMode ? (env.APP_OAUTH_UNLOCK_ONLY_PREMIUM ? !!user?.isPremium : true) : false),
keepPreviousData: false,
staleTime: 15 * 60 * 1000,
},
);

const queryResult = useQuery<T | undefined>([type, id, params, token], async () => callback(token, drmPolicyId), {
Expand Down
3 changes: 2 additions & 1 deletion packages/hooks-react/src/useProtectedMedia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useEffect, useState } from 'react';
import type { PlaylistItem } from '@jwp/ott-common/types/playlist';
import ApiService from '@jwp/ott-common/src/services/ApiService';
import { getModule } from '@jwp/ott-common/src/modules/container';
import env from '@jwp/ott-common/src/env';

import useContentProtection from './useContentProtection';

Expand All @@ -15,7 +16,7 @@ export default function useProtectedMedia(item: PlaylistItem) {
useEffect(() => {
const m3u8 = contentProtectionQuery.data?.sources.find((source) => source.file.indexOf('.m3u8') !== -1);
if (!m3u8) {
setIsUserBlocked(!contentProtectionQuery?.user?.isPremium);
setIsUserBlocked(env.APP_OAUTH_UNLOCK_ONLY_PREMIUM ? !contentProtectionQuery?.user?.isPremium : false);
return;
}
fetch(m3u8.file, { method: 'HEAD' }).then((response) => {
Expand Down
2 changes: 2 additions & 0 deletions packages/ui-react/src/components/Header/Header.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe('<Header />', () => {
openLanguageMenu={vi.fn()}
closeLanguageMenu={vi.fn()}
isLoggedIn={false}
isPremium={false}
canLogin={true}
showPaymentsMenuItem={true}
supportedLanguages={[]}
Expand Down Expand Up @@ -77,6 +78,7 @@ describe('<Header />', () => {
openLanguageMenu={vi.fn()}
closeLanguageMenu={vi.fn()}
isLoggedIn={false}
isPremium={false}
canLogin={true}
showPaymentsMenuItem={true}
supportedLanguages={[]}
Expand Down
4 changes: 3 additions & 1 deletion packages/ui-react/src/components/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type Props = {
closeLanguageMenu: () => void;
children?: ReactNode;
isLoggedIn: boolean;
isPremium: boolean;
sideBarOpen: boolean;
userMenuOpen: boolean;
languageMenuOpen: boolean;
Expand Down Expand Up @@ -92,6 +93,7 @@ const Header: React.FC<Props> = ({
onCloseSearchButtonClick,
onSignUpButtonClick,
isLoggedIn,
isPremium,
sideBarOpen,
userMenuOpen,
languageMenuOpen,
Expand Down Expand Up @@ -154,7 +156,7 @@ const Header: React.FC<Props> = ({

// FEAT:: back to main account cta if oauth mode
if (isLoggedIn && isOAuthMode) {
return <OAuthBackToAccountButton targetUrl={env.APP_OAUTH_DASHBOARD_URL as string} className={styles.backToAccountButton} />;
return isPremium ? <OAuthBackToAccountButton targetUrl={env.APP_OAUTH_DASHBOARD_URL as string} className={styles.backToAccountButton} /> : null;
}

return isLoggedIn ? (
Expand Down
2 changes: 2 additions & 0 deletions packages/ui-react/src/containers/Layout/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const Layout = () => {
);
const userMenuTitleId = useOpaqueId('usermenu-title');
const isLoggedIn = !!useAccountStore(({ user }) => user);
const isPremium = !!useAccountStore(({ user }) => user)?.isPremium;
const favoritesEnabled = !!config.features?.favoritesList;
const { menu, assets, siteName, description, features, styling, custom } = config;

Expand Down Expand Up @@ -228,6 +229,7 @@ const Layout = () => {
supportedLanguages={supportedLanguages}
currentLanguage={currentLanguage}
isLoggedIn={isLoggedIn}
isPremium={isPremium}
sideBarOpen={sideBarOpen}
userMenuOpen={userMenuOpen}
languageMenuOpen={languageMenuOpen}
Expand Down
16 changes: 16 additions & 0 deletions packages/ui-react/src/pages/RedirectToExternal/RedirectToSSO.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React, { useEffect } from 'react';
import { useOAuth } from '@jwp/ott-hooks-react/src/useOAuth';
import { useNavigate } from 'react-router-dom';

const RedirectToSSO: React.FC = () => {
const navigate = useNavigate();
const { login: oAuthLogin } = useOAuth();

useEffect(() => {
oAuthLogin();
}, [navigate, oAuthLogin]);

return null; // Render nothing
};

export default RedirectToSSO;
2 changes: 2 additions & 0 deletions platforms/web/src/containers/AppRoutes/AppRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Home from '@jwp/ott-ui-react/src/pages/Home/Home';
import Search from '@jwp/ott-ui-react/src/pages/Search/Search';
import User from '@jwp/ott-ui-react/src/pages/User/User';
import LegacySeries from '@jwp/ott-ui-react/src/pages/LegacySeries/LegacySeries';
import RedirectToSSO from '@jwp/ott-ui-react/src/pages/RedirectToExternal/RedirectToSSO';
import MediaScreenRouter from '@jwp/ott-ui-react/src/pages/ScreenRouting/MediaScreenRouter';
import PlaylistScreenRouter from '@jwp/ott-ui-react/src/pages/ScreenRouting/PlaylistScreenRouter';
import Layout from '@jwp/ott-ui-react/src/containers/Layout/Layout';
Expand Down Expand Up @@ -92,6 +93,7 @@ export default function AppRoutes() {
element={<ErrorPage title={t('notfound_error_heading', 'Not found')} message={t('notfound_error_description', "This page doesn't exist.")} />}
/>
</Route>
<Route path="/cxl-sso" element={<RedirectToSSO />} />
</Routes>
);
}

0 comments on commit 746b4cf

Please sign in to comment.