Skip to content

Commit f3dd9a4

Browse files
authored
Add dev auth for localhost (codesandbox#2750)
* Add dev auth * Add error message * Fix ok checking
1 parent f7a25a3 commit f3dd9a4

File tree

4 files changed

+109
-5
lines changed

4 files changed

+109
-5
lines changed

packages/app/src/app/overmind/internalActions.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,11 @@ export const signInGithub: Action<
9898
{ useExtraScopes: boolean },
9999
Promise<string>
100100
> = ({ effects }, options) => {
101-
const popup = effects.browser.openPopup(
102-
`/auth/github${options.useExtraScopes ? '?scope=user:email,repo' : ''}`,
103-
'sign in'
104-
);
101+
const authPath = process.env.LOCAL_SERVER
102+
? '/auth/dev'
103+
: `/auth/github${options.useExtraScopes ? '?scope=user:email,repo' : ''}`;
104+
105+
const popup = effects.browser.openPopup(authPath, 'sign in');
105106

106107
return effects.browser
107108
.waitForMessage<{ jwt: string }>('signin')
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import styled from 'styled-components';
2+
3+
export const Container = styled.div`
4+
display: flex;
5+
flex-direction: column;
6+
align-items: center;
7+
8+
height: 100%;
9+
width: 100%;
10+
margin: 1rem;
11+
margin-top: 10%;
12+
`;
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import React from 'react';
2+
3+
import { Title } from 'app/components/Title';
4+
import { SubTitle } from 'app/components/SubTitle';
5+
import Input from '@codesandbox/common/lib/components/Input';
6+
import { Button } from '@codesandbox/common/lib/components/Button';
7+
import { protocolAndHost } from '@codesandbox/common/lib/utils/url-generator';
8+
9+
import { Container } from './elements';
10+
11+
export const DevAuthPage = () => {
12+
const [authCode, setAuthCode] = React.useState('');
13+
const [error, setError] = React.useState<string | null>(null);
14+
15+
const getJWTToken = () => {
16+
setError(null);
17+
let ok = true;
18+
fetch(`/api/v1/auth/verify/${authCode}`)
19+
.then(res => {
20+
ok = res.ok;
21+
return res.json();
22+
})
23+
.then(res => {
24+
if (!ok) {
25+
throw new Error(res.errors.detail[0]);
26+
}
27+
if (
28+
window.opener &&
29+
window.opener.location.origin === window.location.origin
30+
) {
31+
window.opener.postMessage(
32+
{
33+
type: 'signin',
34+
data: {
35+
jwt: res.data.token,
36+
},
37+
},
38+
protocolAndHost()
39+
);
40+
}
41+
})
42+
.catch(e => {
43+
setError(e.message);
44+
});
45+
};
46+
47+
return (
48+
<Container>
49+
<Title>Developer Sign In</Title>
50+
<SubTitle style={{ width: 800 }}>
51+
Please enter the token you get from{' '}
52+
<a
53+
href="https://codesandbox.io/cli/login"
54+
target="popup"
55+
rel="noreferrer noopener"
56+
onClick={e => {
57+
e.preventDefault();
58+
window.open(
59+
'https://codesandbox.io/cli/login',
60+
'popup',
61+
'width=600,height=600'
62+
);
63+
return false;
64+
}}
65+
>
66+
here
67+
</a>
68+
. This token will sign you in with your account from codesandbox.io.
69+
</SubTitle>
70+
<div
71+
style={{ display: 'flex', justifyContent: 'center', marginTop: '2rem' }}
72+
>
73+
<Input
74+
style={{ width: 600, fontSize: '1.5rem' }}
75+
placeholder="Auth Code"
76+
value={authCode}
77+
onChange={e => {
78+
setAuthCode(e.target.value);
79+
}}
80+
/>
81+
<Button onClick={getJWTToken}>Submit</Button>
82+
</div>
83+
84+
{error && <div style={{ marginTop: '1rem' }}>Error: {error}</div>}
85+
</Container>
86+
);
87+
};

packages/app/src/app/pages/index.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import Modals from './common/Modals';
1515
import Sandbox from './Sandbox';
1616
import NewSandbox from './NewSandbox';
1717
import Dashboard from './Dashboard';
18+
import { DevAuthPage } from './DevAuth';
1819
import { Container, Content } from './elements';
1920

2021
const routeDebugger = _debug('cs:app:router');
@@ -112,7 +113,10 @@ const RoutesComponent: React.FC = () => {
112113
<Route path="/cli/login" component={CLI} />
113114
<Route path="/auth/zeit" component={ZeitSignIn} />
114115
{process.env.NODE_ENV === `development` && (
115-
<Route path="/codesadbox" component={CodeSadbox} />
116+
<>
117+
<Route path="/auth/dev" component={DevAuthPage} />
118+
<Route path="/codesadbox" component={CodeSadbox} />
119+
</>
116120
)}
117121
<Route component={NotFound} />
118122
</Switch>

0 commit comments

Comments
 (0)