Skip to content

Commit c0b9a7f

Browse files
authored
chore: enable signing in when developing in CSB (#6882)
1 parent 2f7bfae commit c0b9a7f

File tree

5 files changed

+41
-10
lines changed

5 files changed

+41
-10
lines changed

.codesandbox/tasks.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
"runAtStart": true,
1515
"preview": {
1616
"port": 3000
17+
},
18+
"restartOn": {
19+
"files": ["yarn.lock", "packages/app/scripts/*"]
1720
}
1821
},
1922
"app:home:fast": {

packages/app/scripts/start.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,7 @@ function addMiddleware(devServer, index) {
226226
if (process.env.LOCAL_SERVER) {
227227
devServer.use(
228228
cors({
229-
origin: [
230-
'http://localhost:3000',
231-
'http://localhost:3002',
232-
'http://localhost:8000',
233-
'http://localhost:8001',
234-
],
229+
origin: true,
235230
credentials: true,
236231
})
237232
);
@@ -291,9 +286,9 @@ function runDevServer(port, protocol, index) {
291286
host: process.env.LOCAL_SERVER
292287
? 'localhost'
293288
: process.env.DEV_DOMAIN || 'codesandbox.test',
294-
disableHostCheck: !(
295-
process.env.LOCAL_SERVER || process.env.CODESANDBOX_HOST
296-
),
289+
disableHostCheck:
290+
Boolean(process.env.CSB) ||
291+
!(process.env.LOCAL_SERVER || process.env.CODESANDBOX_HOST),
297292
contentBase: false,
298293
clientLogLevel: 'warning',
299294
overlay: true,

packages/app/src/app/overmind/effects/live/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ class Live {
173173
private connectionPromise: Promise<Socket>;
174174
private async connect(): Promise<Socket> {
175175
if (!this.socket) {
176-
const protocol = process.env.LOCAL_SERVER ? 'ws' : 'wss';
176+
const protocol =
177+
process.env.LOCAL_SERVER && !process.env.CSB ? 'ws' : 'wss';
177178
let jwt = await this.provideJwtCached();
178179
const params = () => ({
179180
guardian_token: jwt,

packages/common/src/config/env.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ export default Object.keys(process.env)
4242
},
4343
{
4444
'process.env.NODE_ENV': NODE_ENV,
45+
'process.env.CSB': Boolean(process.env.CSB || false),
46+
'process.env.ENDPOINT': JSON.stringify(
47+
process.env.ENDPOINT || 'codesandbox.io'
48+
),
4549
'process.env.STAGING_API': STAGING_API,
4650
'process.env.CODESANDBOX_HOST': JSON.stringify(getHost()),
4751
'process.env.LOCAL_SERVER': Boolean(LOCAL_SERVER),

packages/common/src/utils/url-generator.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,35 @@ const buildEncodedUri = (
3434
.map((value, i) => `${encodeURIComponent(value)}${strings[i + 1]}`)
3535
.join('');
3636

37+
const REGEX = /(?<id>\w{5,6})-(?<port>\d{1,5})\.(?<hostname>.*)/;
38+
function getCodeSandboxDevHost(port: number): string | undefined {
39+
if (typeof window === 'undefined') {
40+
// eslint-disable-next-line global-require
41+
const hostname = require('os').hostname();
42+
43+
return `${hostname}-${port}.preview.csb.app`;
44+
}
45+
46+
const currentUrl = location.host;
47+
const currentMatch = currentUrl.match(REGEX);
48+
49+
if (!currentMatch?.groups) {
50+
return undefined;
51+
}
52+
const { id, hostname } = currentMatch.groups;
53+
54+
if (!id || !port || !hostname) {
55+
return undefined;
56+
}
57+
58+
return `${id}-${port}.${hostname}`;
59+
}
60+
3761
export const host = () => {
62+
if (process.env.CSB && getCodeSandboxDevHost(3000)) {
63+
return getCodeSandboxDevHost(3000);
64+
}
65+
3866
if (process.env.NODE_ENV === 'production') {
3967
return process.env.CODESANDBOX_HOST.split('//')[1];
4068
}

0 commit comments

Comments
 (0)