Skip to content

Commit c0f532a

Browse files
authored
Fix auth deploy (#3030)
1 parent 933ef1b commit c0f532a

File tree

5 files changed

+59
-37
lines changed

5 files changed

+59
-37
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
- Fixes issue where `host` header was being incorrectly set when proxying to Cloud Run or Cloud Functions for Firebase from the Hosting emulator. (#3012)
33
- Fixes issue where emulated HTTP functions would crash when the URL contained query parameters (#3032)
44
- Fixes issue with routing to emulated HTTP functions in regions outside of `us-central1` (#3031)
5+
- Fixes issue where authorized domains were not being correctly updated when deploying to Hosting channels. (#3002)

src/commands/hosting-channel-create.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { bold, yellow } from "cli-color";
22

3-
import { Channel, createChannel, addAuthDomain, normalizeName } from "../hosting/api";
3+
import { Channel, createChannel, addAuthDomains, normalizeName } from "../hosting/api";
44
import { Command } from "../command";
55
import { DEFAULT_DURATION, calculateChannelExpireTTL } from "../hosting/expireUtils";
66
import { FirebaseError } from "../error";
@@ -74,7 +74,7 @@ export default new Command("hosting:channel:create [channelId]")
7474
}
7575

7676
try {
77-
await addAuthDomain(projectId, channel.url);
77+
await addAuthDomains(projectId, [channel.url]);
7878
} catch (e) {
7979
logLabeledWarning(
8080
LOG_TAG,

src/commands/hosting-channel-deploy.ts

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
getChannel,
88
createChannel,
99
updateChannelTtl,
10-
addAuthDomain,
10+
addAuthDomains,
1111
cleanAuthState,
1212
normalizeName,
1313
} from "../hosting/api";
@@ -120,26 +120,6 @@ export default new Command("hosting:channel:deploy [channelId]")
120120
LOG_TAG,
121121
`Channel ${bold(channelId)} has been created on site ${bold(site)}.`
122122
);
123-
try {
124-
await addAuthDomain(projectId, chan.url);
125-
} catch (e) {
126-
logLabeledWarning(
127-
LOG_TAG,
128-
marked(
129-
`Unable to add channel domain to Firebase Auth. Visit the Firebase Console at ${consoleUrl(
130-
projectId,
131-
"/authentication/providers"
132-
)}`
133-
)
134-
);
135-
logger.debug("[hosting] unable to add auth domain", e);
136-
}
137-
}
138-
try {
139-
await cleanAuthState(projectId, site);
140-
} catch (e) {
141-
logLabeledWarning(LOG_TAG, "Unable to sync Firebase Auth state.");
142-
logger.debug("[hosting] unable to sync auth domain", e);
143123
}
144124
siteInfo.url = chan.url;
145125
siteInfo.expireTime = chan.expireTime;
@@ -150,6 +130,7 @@ export default new Command("hosting:channel:deploy [channelId]")
150130
await deploy(["hosting"], options, { hostingChannel: channelId });
151131

152132
logger.info();
133+
await syncAuthState(projectId, sites);
153134
const deploys: { [key: string]: ChannelInfo } = {};
154135
sites.forEach((d) => {
155136
deploys[d.target || d.site] = d;
@@ -162,7 +143,37 @@ export default new Command("hosting:channel:deploy [channelId]")
162143
`Channel URL (${bold(d.site || d.target)}): ${d.url} ${expires}`
163144
);
164145
});
165-
166146
return deploys;
167147
}
168148
);
149+
150+
/**
151+
* Helper function to sync authorized domains for deployed sites.
152+
* @param projectId the project id.
153+
* @param sites list of sites & url to sync auth state for.
154+
*/
155+
async function syncAuthState(projectId: string, sites: ChannelInfo[]) {
156+
const siteNames = sites.map((d) => d.site);
157+
const urlNames = sites.map((d) => d.url);
158+
try {
159+
await addAuthDomains(projectId, urlNames);
160+
logger.debug("[hosting] added auth domain for urls", urlNames);
161+
} catch (e) {
162+
logLabeledWarning(
163+
LOG_TAG,
164+
marked(
165+
`Unable to add channel domain to Firebase Auth. Visit the Firebase Console at ${consoleUrl(
166+
projectId,
167+
"/authentication/providers"
168+
)}`
169+
)
170+
);
171+
logger.debug("[hosting] unable to add auth domain", e);
172+
}
173+
try {
174+
await cleanAuthState(projectId, siteNames);
175+
} catch (e) {
176+
logLabeledWarning(LOG_TAG, "Unable to sync Firebase Auth state.");
177+
logger.debug("[hosting] unable to sync auth domain", e);
178+
}
179+
}

src/commands/hosting-clone.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
createChannel,
99
cloneVersion,
1010
createRelease,
11-
addAuthDomain,
11+
addAuthDomains,
1212
normalizeName,
1313
} from "../hosting/api";
1414
import * as utils from "../utils";
@@ -87,7 +87,7 @@ export default new Command("hosting:clone <source> <targetChannel>")
8787
utils.logSuccess(`Created new channel ${targetChannelId}`);
8888
try {
8989
const tProjectId = getProjectId(tChannel.name);
90-
await addAuthDomain(tProjectId, tChannel.url);
90+
await addAuthDomains(tProjectId, [tChannel.url]);
9191
} catch (e) {
9292
utils.logLabeledWarning(
9393
"hosting:clone",

src/hosting/api.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -353,18 +353,20 @@ export async function createRelease(
353353
}
354354

355355
/**
356-
* Adds channel domain to Firebase Auth list.
356+
* Adds list of channel domains to Firebase Auth list.
357357
* @param project the project ID.
358-
* @param url the url of the channel.
358+
* @param urls the list of urls of the channel.
359359
*/
360-
export async function addAuthDomain(project: string, url: string): Promise<string[]> {
360+
export async function addAuthDomains(project: string, urls: string[]): Promise<string[]> {
361361
const domains = await getAuthDomains(project);
362-
const domain = url.replace("https://", "");
363362
const authDomains = domains || [];
364-
if (authDomains.includes(domain)) {
365-
return authDomains;
363+
for (const url of urls) {
364+
const domain = url.replace("https://", "");
365+
if (authDomains.includes(domain)) {
366+
continue;
367+
}
368+
authDomains.push(domain);
366369
}
367-
authDomains.push(domain);
368370
return await updateAuthDomains(project, authDomains);
369371
}
370372

@@ -432,9 +434,17 @@ export async function getCleanDomains(project: string, site: string): Promise<st
432434
* updates Firebase Auth Api with aforementioned
433435
* list.
434436
* @param project the project ID.
435-
* @param site the site for the channel.
437+
* @param sites the list of sites for the channel.
436438
*/
437-
export async function cleanAuthState(project: string, site: string): Promise<string[]> {
438-
const authDomains = await getCleanDomains(project, site);
439-
return await updateAuthDomains(project, authDomains);
439+
export async function cleanAuthState(
440+
project: string,
441+
sites: string[]
442+
): Promise<Map<string, Array<string>>> {
443+
const siteDomainMap = new Map();
444+
for (const site of sites) {
445+
const authDomains = await getCleanDomains(project, site);
446+
const updatedDomains = await updateAuthDomains(project, authDomains);
447+
siteDomainMap.set(site, updatedDomains);
448+
}
449+
return siteDomainMap;
440450
}

0 commit comments

Comments
 (0)