Skip to content

Commit cfff639

Browse files
authored
BFF: fix Github auth start (#4984)
* merge headers correctly * redirect to auth * add env vars
1 parent 1e2d973 commit cfff639

File tree

5 files changed

+40
-22
lines changed

5 files changed

+40
-22
lines changed

utopia-remix/.env.sample

+3
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ REACT_APP_EDITOR_URL="http://localhost:8000"
77
AUTH0_ENDPOINT=""
88
AUTH0_CLIENT_ID=""
99
AUTH0_REDIRECT_URI=""
10+
11+
GITHUB_OAUTH_CLIENT_ID=""
12+
GITHUB_OAUTH_REDIRECT_URL=""

utopia-remix/app/env.server.ts

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ export const ServerEnvironment = {
1818
AUTH0_ENDPOINT: optionalEnv('AUTH0_ENDPOINT', '<AUTH0_ENDPOINT>'),
1919
AUTH0_CLIENT_ID: optionalEnv('AUTH0_CLIENT_ID', '<AUTH0_CLIENT_ID>'),
2020
AUTH0_REDIRECT_URI: optionalEnv('AUTH0_REDIRECT_URI', '<AUTH0_REDIRECT_URI>'),
21+
// Github OAuth credentials
22+
GITHUB_OAUTH_CLIENT_ID: optionalEnv('GITHUB_OAUTH_CLIENT_ID', ''),
23+
GITHUB_OAUTH_REDIRECT_URL: optionalEnv('GITHUB_OAUTH_REDIRECT_URL', ''),
2124
}
2225

2326
export type BrowserEnvironment = {
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
import { LoaderFunctionArgs } from '@remix-run/node'
1+
import { LoaderFunctionArgs, redirect } from '@remix-run/node'
22
import { handle, handleOptions } from '../util/api.server'
3-
import { proxy } from '../util/proxy.server'
3+
import { ServerEnvironment } from '../env.server'
44

55
export async function loader(args: LoaderFunctionArgs) {
66
return handle(args, {
77
OPTIONS: handleOptions,
8-
GET: proxy,
8+
GET: redirectToGithubAuthStart,
99
})
1010
}
11+
12+
async function redirectToGithubAuthStart() {
13+
const scopes = 'repo'
14+
15+
return redirect(
16+
`https://github.com/login/oauth/authorize?client_id=${ServerEnvironment.GITHUB_OAUTH_CLIENT_ID}&redirect_uri=${ServerEnvironment.GITHUB_OAUTH_REDIRECT_URL}&scope=${scopes}`,
17+
)
18+
}

utopia-remix/app/util/api.server.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ type EmptyResponse = Record<string, never>
1818

1919
export type ApiResponse<T> = TypedResponse<T | ErrorResponse | EmptyResponse>
2020

21-
const responseHeaders: HeadersInit = {
21+
const defaultResponseHeaders = new Headers({
2222
'Access-Control-Allow-Origin': ServerEnvironment.CORSOrigin,
2323
'Access-Control-Allow-Credentials': 'true',
2424
'Access-Control-Allow-Headers': 'content-type, origin, cookie',
2525
'Access-Control-Allow-Methods': 'GET,POST,PUT,DELETE,OPTIONS',
2626
'Cache-control': 'no-cache',
27-
}
27+
})
2828

2929
export async function handleOptions(): Promise<TypedResponse<EmptyResponse>> {
30-
return json({}, { headers: responseHeaders })
30+
return json({}, { headers: defaultResponseHeaders })
3131
}
3232

3333
interface HandleRequest {
@@ -56,22 +56,24 @@ async function handleMethod<T>(
5656
try {
5757
const resp = await fn(request, params)
5858
if (resp instanceof Response) {
59+
const mergedHeaders = new Headers(defaultResponseHeaders)
60+
resp.headers.forEach((value, key) => {
61+
mergedHeaders.set(key, value)
62+
})
5963
return new Response(resp.body, {
60-
headers: {
61-
...resp.headers,
62-
...responseHeaders,
63-
},
64+
status: resp.status,
65+
headers: mergedHeaders,
6466
})
6567
}
66-
return json(resp, { headers: responseHeaders })
68+
return json(resp, { headers: defaultResponseHeaders })
6769
} catch (err) {
6870
const { message, status, name } = getErrorData(err)
6971

7072
console.error(`${request.method} ${request.url}: ${message}`)
7173

7274
return json(
7375
{ error: name, status: status, message: message },
74-
{ headers: responseHeaders, status: status },
76+
{ headers: defaultResponseHeaders, status: status },
7577
)
7678
}
7779
}

utopia-remix/readme.md

+12-10
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212

1313
These are the required environment variables; for local development they can be put in a `.env` file, using `.env.sample` as a blueprint.
1414

15-
| Name | Description | Example |
16-
| ---------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------- |
17-
| `APP_ENV` | The environment the app is running on, with possible values shown [here](https://github.com/concrete-utopia/utopia/blob/e881cbf330e2ab68f8ea45f5afdbe8ed2c59ebca/utopia-remix/app/env.server.ts#L4). | `local` |
18-
| `BACKEND_URL` | The base URL for the Haskell server, as `<scheme>://<host>:<port>` | `http://127.0.0.1:8001` |
19-
| `CORS_ORIGIN` | The allowed origin for CORS requests, it should match the host of the browser app. | `http://localhost:8000` |
20-
| `DATABASE_URL` | The Postgres database connection string, as `postgres://<username>:<password>@<host>:<port>/<database>`. **For local development** the username field should be populated with your local machine username, as obtained with the `whoami` command. | `postgres://johndoe:postgres@localhost:5432/utopia` |
21-
| `REACT_APP_EDITOR_URL` | The base URL for the editor, as used in the frontend portions of the Remix app. | `http://localhost:8000` |
22-
| AUTH0_ENDPOINT | Auth0 endpoint | `http://foo.bar.auth0.com` |
23-
| AUTH0_CLIENT_ID | Auth0 client ID | `xxyyzz` |
24-
| AUTH0_REDIRECT_URI | Auth0 login redirect URI | `http://localhost:8000/authenticate` |
15+
| Name | Description | Example |
16+
| ------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------- |
17+
| `APP_ENV` | The environment the app is running on, with possible values shown [here](https://github.com/concrete-utopia/utopia/blob/e881cbf330e2ab68f8ea45f5afdbe8ed2c59ebca/utopia-remix/app/env.server.ts#L4). | `local` |
18+
| `BACKEND_URL` | The base URL for the Haskell server, as `<scheme>://<host>:<port>` | `http://127.0.0.1:8001` |
19+
| `CORS_ORIGIN` | The allowed origin for CORS requests, it should match the host of the browser app. | `http://localhost:8000` |
20+
| `DATABASE_URL` | The Postgres database connection string, as `postgres://<username>:<password>@<host>:<port>/<database>`. **For local development** the username field should be populated with your local machine username, as obtained with the `whoami` command. | `postgres://johndoe:postgres@localhost:5432/utopia` |
21+
| `REACT_APP_EDITOR_URL` | The base URL for the editor, as used in the frontend portions of the Remix app. | `http://localhost:8000` |
22+
| AUTH0_ENDPOINT | Auth0 endpoint | `http://foo.bar.auth0.com` |
23+
| AUTH0_CLIENT_ID | Auth0 client ID | `xxyyzz` |
24+
| AUTH0_REDIRECT_URI | Auth0 login redirect URI | `http://localhost:8000/authenticate` |
25+
| GITHUB_OAUTH_CLIENT_ID | Github OAuth app client ID URI | `xxyyzz` |
26+
| GITHUB_OAUTH_REDIRECT_URL | Github OAuth app login redirect URL | `http://localhost:8000/v1/github/authentication/finish` |

0 commit comments

Comments
 (0)