Skip to content

Commit bcd5d1e

Browse files
authored
CG-4678 update music festival react sample site and readme article to use preview tokens for on page editing (#37)
CG-4678
2 parents bbc715d + 9630d8e commit bcd5d1e

File tree

10 files changed

+327
-44
lines changed

10 files changed

+327
-44
lines changed

samples/musicfestival-backend-dotnet/MusicFestival.Backend.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="EPiServer.CMS" Version="12.20.0" />
12-
<PackageReference Include="EPiServer.ContentDefinitionsApi" Version="3.8.0" />
13-
<PackageReference Include="EPiServer.ContentDeliveryApi.Cms" Version="3.8.0" />
14-
<PackageReference Include="EPiServer.ContentManagementApi" Version="3.8.0" />
11+
<PackageReference Include="EPiServer.CMS" Version="12.22.4" />
12+
<PackageReference Include="EPiServer.ContentDefinitionsApi" Version="3.9.0" />
13+
<PackageReference Include="EPiServer.ContentDeliveryApi.Cms" Version="3.9.0" />
14+
<PackageReference Include="EPiServer.ContentManagementApi" Version="3.9.0" />
1515
<PackageReference Include="EPiServer.OpenIDConnect" Version="3.8.0" />
1616
<PackageReference Include="EPiServer.OpenIDConnect.UI" Version="3.8.0" />
17-
<PackageReference Include="Optimizely.ContentGraph.Cms" Version="3.0.0" />
17+
<PackageReference Include="Optimizely.ContentGraph.Cms" Version="3.4.0" />
1818
</ItemGroup>
1919
</Project>

samples/musicfestival-backend-dotnet/Startup.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void ConfigureServices(IServiceCollection services)
3939
User=sa;Password=Admin123!;
4040
Trust Server Certificate=True;Connect Timeout=30";
4141
var connectionstring = _configuration.GetConnectionString("EPiServerDB")
42-
?? (isMacOs? macOsConnString: localDBConnString);
42+
?? (isMacOs ? macOsConnString : localDBConnString);
4343
services.Configure<DataAccessOptions>(o =>
4444
{
4545
o.SetConnectionString(connectionstring);
@@ -133,7 +133,10 @@ public void ConfigureServices(IServiceCollection services)
133133
o.IncludeNumericContentIdentifier = true;
134134
});
135135

136-
services.AddContentGraph(_configuration, OpenIDConnectOptionsDefaults.AuthenticationScheme);
136+
services.AddContentGraph(OpenIDConnectOptionsDefaults.AuthenticationScheme, options =>
137+
{
138+
options.EnablePreviewTokens = true;
139+
});
137140
services.AddHostedService<ProvisionDatabase>();
138141
}
139142

@@ -147,7 +150,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
147150
app.UseStaticFiles();
148151
app.UseRouting();
149152
app.UseCors(b => b
150-
.WithOrigins(new[] { $"{_frontendUri}"})
153+
.WithOrigins(new[] { $"{_frontendUri}" })
151154
.WithExposedContentDeliveryApiHeaders()
152155
.WithExposedContentDefinitionApiHeaders()
153156
.WithHeaders("Authorization")

samples/musicfestival-frontend-react/.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
REACT_APP_CG_PROXY_URL=http://localhost:8082/EpiServer/ContentGraph/CGProxy/Query
1+
REACT_APP_CG_PREVIEW_URL=https://cg.optimizely.com/content/v2
22
REACT_APP_CONTENT_GRAPH_GATEWAY_URL=https://cg.optimizely.com/content/v2?auth=INPUT_SINGLE_KEY_HERE
33

44
REACT_APP_LOGIN_AUTHORITY=http://localhost:8082

samples/musicfestival-frontend-react/src/App.tsx

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import ArtistContainerPage from './pages/ArtistContainerPage';
55
import ArtistDetailsPage from './pages/ArtistDetailsPage';
66
import authService from './authService';
77
import { useState } from 'react';
8-
import { isEditOrPreviewMode } from './helpers/urlHelper'
8+
import { isEditOrPreviewMode, getPreviewTokenFromUrl } from './helpers/urlHelper'
99
import './App.css';
1010
import Footer from './components/Footer';
1111
import { useMutation, useQueryClient } from '@tanstack/react-query';
@@ -16,7 +16,7 @@ import { BlockPage } from './pages/BlockPage';
1616

1717
let previousSavedMessage: any = null;
1818
const singleKeyUrl = process.env.REACT_APP_CONTENT_GRAPH_GATEWAY_URL as string
19-
const hmacKeyUrl = process.env.REACT_APP_CG_PROXY_URL as string
19+
const previewUrl = process.env.REACT_APP_CG_PREVIEW_URL as string
2020

2121
const App = () => {
2222
const queryClient = useQueryClient();
@@ -36,21 +36,18 @@ const App = () => {
3636
}
3737
});
3838

39-
authService.getAccessToken().then((_token) => {
40-
_token && setToken(_token)
41-
modeEdit && !_token && !data && authService.login()
42-
})
39+
const previewToken = getPreviewTokenFromUrl(window.location.search);
4340

44-
variables = generateGQLQueryVars(token, window.location.pathname)
41+
variables = generateGQLQueryVars(previewToken, window.location.pathname)
4542
if (modeEdit) {
46-
if (token) {
47-
headers = { 'Authorization': 'Bearer ' + token };
43+
if (previewToken) {
44+
headers = { 'Authorization': 'Bearer ' + previewToken };
4845
}
49-
url = hmacKeyUrl
46+
url = previewUrl
5047
subcribeContentSavedEvent((message: any) => mutate(message))
5148
}
5249

53-
const { data: queryData } = useStartQuery({ endpoint: url, fetchParams: { headers: headers } }, variables, { staleTime: 2000, enabled: !modeEdit || !!token });
50+
const { data: queryData } = useStartQuery({ endpoint: url, fetchParams: { headers: headers } }, variables, { staleTime: 2000, enabled: !modeEdit || !!previewToken });
5451
data = queryData
5552

5653
if (!data) {

samples/musicfestival-frontend-react/src/components/SearchButton.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {useEffect, useRef, useState} from "react";
22
import {useSearchParams} from "react-router-dom";
33
import {ArtistAutocompleteQuery, Ranking, useArtistAutocompleteQuery} from "../generated";
44
import {generateGQLSearchQueryVars} from "../helpers/queryCacheHelper";
5-
import {getRankingFromSearchParams, isEditOrPreviewMode} from "../helpers/urlHelper";
5+
import {getRankingFromSearchParams, isEditOrPreviewMode, getPreviewTokenFromUrl } from "../helpers/urlHelper";
66

77
type CustomString = string | number | readonly string[] | undefined
88

@@ -18,10 +18,12 @@ function SearchButton({filterValue}: any): JSX.Element {
1818
const [orderBy] = useState("ASC")
1919

2020
let modeEdit = isEditOrPreviewMode()
21-
let variables = generateGQLSearchQueryVars(token, window.location.pathname, searchValue as string | null, orderBy, ranking);
21+
const previewToken = getPreviewTokenFromUrl(window.location.search);
22+
23+
let variables = generateGQLSearchQueryVars(previewToken, window.location.pathname, searchValue as string | null, orderBy, ranking);
2224
const autocompleteData = useArtistAutocompleteQuery({endpoint: singleKeyUrl}, variables, {
2325
staleTime: 2000,
24-
enabled: !modeEdit || !!token
26+
enabled: !modeEdit || !!previewToken
2527
}).data;
2628

2729
const onSearch = (event: any) => {

0 commit comments

Comments
 (0)