Skip to content

Commit db5ce92

Browse files
committed
fix(storefront): Fix handling session tokens for DecapCMS
Setting `backend.squash_merges: true` by default
1 parent 18b513f commit db5ce92

2 files changed

Lines changed: 42 additions & 29 deletions

File tree

packages/storefront/src/decap-cms/get-cms-config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ export const getCmsConfig = async () => {
8686
let config = {
8787
locale,
8888
load_config_file: false,
89-
publish_mode: 'editorial_workflow',
9089
media_folder: `${baseDir}public/img/uploads`,
9190
public_folder: '/img/uploads',
9291
site_url: `https://${domain}/~preview`,

packages/storefront/src/lib/scripts/decap-cms.ts

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ const authAndInitCms = async () => {
5151
initCmsWithPreview();
5252
return;
5353
}
54-
const storageKey = '__cms_token';
55-
let token = sessionStorage.getItem(storageKey);
54+
const tokenStorageKey = '__cms_token';
55+
let token = sessionStorage.getItem(tokenStorageKey);
56+
const repoStorageKey = '__cms_repo';
57+
let repository = sessionStorage.getItem(repoStorageKey);
5658
const searchParams = new URLSearchParams(location.search);
5759
const ssoToken = searchParams.get('sso_token') || searchParams.get('access_token');
5860
if (!cmsConfig.backend?.base_url) {
@@ -70,6 +72,7 @@ const authAndInitCms = async () => {
7072
base_url: `https://${location.hostname}`,
7173
auth_endpoint: location.pathname, // self
7274
branch: 'main',
75+
squash_merges: true,
7376
commit_messages: {
7477
create: 'Create {{collection}} “{{slug}}”',
7578
update: 'Update {{collection}} “{{slug}}”',
@@ -81,34 +84,49 @@ const authAndInitCms = async () => {
8184
},
8285
...cmsConfig.backend,
8386
};
87+
if (cmsConfig.publish_mode === undefined) {
88+
cmsConfig.publish_mode = 'editorial_workflow';
89+
}
8490
if (cmsConfig.backend.api_root?.startsWith('https://ecomplus.app/')) {
85-
const res = await afetch('https://ecomplus.app/api/github-installations', {
86-
headers: {
87-
'X-Store-ID': `${ECOM_STORE_ID}`,
88-
Authorization: `Bearer ${ssoToken}`,
89-
},
90-
});
91-
if (res.ok) {
92-
const { installations } = await res.json();
93-
if (Array.isArray(installations)) {
94-
const { repo } = cmsConfig.backend;
95-
const installation = repo !== '_owner/_name'
96-
? installations.find(({ repository }) => repository === repo)
97-
: installations[0];
98-
if (installation?.gh_token && installation.gh_token.charAt(0) !== '*') {
99-
// Consume GitHub REST API directly
100-
token = installation.gh_token as string;
101-
// ghToken = token;
102-
delete cmsConfig.backend.api_root;
103-
cmsConfig.backend.repo = installation.repository;
104-
cmsConfig.backend.name = 'github';
91+
if (!token) {
92+
const res = await afetch('https://ecomplus.app/api/github-installations', {
93+
headers: {
94+
'X-Store-ID': `${ECOM_STORE_ID}`,
95+
Authorization: `Bearer ${ssoToken}`,
96+
},
97+
});
98+
if (res.ok) {
99+
const { installations } = await res.json();
100+
if (Array.isArray(installations)) {
101+
const { repo } = cmsConfig.backend;
102+
const installation = repo !== '_owner/_name'
103+
? installations.find((isnt) => isnt.repository === repo)
104+
: installations[0];
105+
if (installation?.gh_token && installation.gh_token.charAt(0) !== '*') {
106+
// Consume GitHub REST API directly
107+
token = installation.gh_token as string;
108+
if (repo === '_owner/_name') {
109+
repository = installation.repository
110+
|| `${installation.organization}/${installation.organization}`;
111+
}
112+
}
105113
}
106114
}
107115
}
116+
if (token) {
117+
delete cmsConfig.backend.api_root;
118+
cmsConfig.backend.name = 'github';
119+
if (repository) {
120+
cmsConfig.backend.repo = repository;
121+
}
122+
}
108123
}
109124
}
110125
if (token) {
111-
sessionStorage.removeItem(storageKey);
126+
sessionStorage.setItem(tokenStorageKey, token);
127+
if (repository) {
128+
sessionStorage.setItem(repoStorageKey, repository);
129+
}
112130
// Ref.: https://github.com/decaporg/decap-cms/blob/e93c94f1ce707719dfb7750af82b17c38b461831/packages/decap-cms-lib-auth/src/netlify-auth.js#L46
113131
// E.g.: https://github.com/Herohtar/netlify-cms-oauth-firebase/blob/master/functions/index.js#L9-L25
114132
window.addEventListener('message', (event) => {
@@ -129,12 +147,8 @@ const authAndInitCms = async () => {
129147
window.history?.pushState(
130148
'hide-token',
131149
document.title,
132-
`${location.pathname}${location.search}${location.hash}`,
150+
`${location.pathname}${location.hash}`,
133151
);
134-
const provider = cmsConfig.backend.name;
135-
if (provider && provider !== 'git-gateway') {
136-
sessionStorage.setItem(storageKey, ssoToken);
137-
}
138152
}
139153
initCmsWithPreview();
140154
};

0 commit comments

Comments
 (0)