Skip to content

Commit 31d8669

Browse files
committed
Make github auth optional and better name the env vars
1 parent 59e1fbf commit 31d8669

File tree

11 files changed

+99
-563
lines changed

11 files changed

+99
-563
lines changed

apps/webapp/.env.example

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ APP_ORIGIN=http://localhost:3000
1515

1616
NODE_ENV=development
1717

18-
PULSAR_ENABLED=1
19-
2018
SESSION_SECRET=
2119

2220

@@ -25,9 +23,9 @@ SESSION_SECRET=
2523
# ************************************* LOGIN *******************************************
2624
#########################################################################################
2725

28-
GITHUB_CLIENT_ID=
26+
AUTH_GITHUB_CLIENT_ID=
2927

30-
GITHUB_SECRET=
28+
AUTH_GITHUB_CLIENT_SECRET=
3129

3230
LOGIN_ORIGIN=http://localhost:3000
3331

apps/webapp/app/components/stories/Forms.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ function LoginForm() {
128128
Continue with Email
129129
</Button>
130130
<Paragraph variant="small" className="mt-2 text-center">
131-
By connecting your GitHub account you agree to our{" "}
131+
By creating an account you agree to our{" "}
132132
<TextLink href="#">terms</TextLink> and{" "}
133133
<TextLink href="#">privacy</TextLink> policies.
134134
</Paragraph>

apps/webapp/app/env.server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ const EnvironmentSchema = z.object({
2929
SECRET_STORE: SecretStoreOptionsSchema.default("DATABASE"),
3030
POSTHOG_PROJECT_KEY: z.string().optional(),
3131
MAGIC_LINK_SECRET: z.string(),
32-
GITHUB_CLIENT_ID: z.string(),
33-
GITHUB_SECRET: z.string(),
32+
AUTH_GITHUB_CLIENT_ID: z.string().optional(),
33+
AUTH_GITHUB_CLIENT_SECRET: z.string().optional(),
3434
FROM_EMAIL: z.string(),
3535
REPLY_TO_EMAIL: z.string(),
3636
RESEND_API_KEY: z.string(),

apps/webapp/app/routes/login._index/route.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { Fieldset } from "~/components/primitives/Fieldset";
1111
import { FormTitle } from "~/components/primitives/FormTitle";
1212
import { NamedIcon } from "~/components/primitives/NamedIcon";
1313
import { Paragraph, TextLink } from "~/components/primitives/Paragraph";
14+
import { isGithubAuthSupported } from "~/services/auth.server";
1415

1516
import { commitSession, setRedirectTo } from "~/services/redirectTo.server";
1617
import { getUserId } from "~/services/session.server";
@@ -26,15 +27,15 @@ export async function loader({ request }: LoaderArgs) {
2627
const session = await setRedirectTo(request, redirectTo);
2728

2829
return typedjson(
29-
{ redirectTo },
30+
{ redirectTo, isGithubAuthSupported },
3031
{
3132
headers: {
3233
"Set-Cookie": await commitSession(session),
3334
},
3435
}
3536
);
3637
} else {
37-
return typedjson({ redirectTo: null });
38+
return typedjson({ redirectTo: null, isGithubAuthSupported });
3839
}
3940
}
4041

@@ -63,10 +64,12 @@ export default function LoginPage() {
6364
<FormTitle divide={false} title="Create your Trigger.dev account" />
6465
<Fieldset>
6566
<div className="flex flex-col gap-y-2">
66-
<Button type="submit" variant="primary/large" fullWidth>
67-
<NamedIcon name={"github"} className={"mr-1.5 h-4 w-4"} />
68-
Continue with GitHub
69-
</Button>
67+
{isGithubAuthSupported && (
68+
<Button type="submit" variant="primary/large" fullWidth>
69+
<NamedIcon name={"github"} className={"mr-1.5 h-4 w-4"} />
70+
Continue with GitHub
71+
</Button>
72+
)}
7073
<LinkButton
7174
to="/login/magic"
7275
variant="secondary/large"
@@ -82,7 +85,7 @@ export default function LoginPage() {
8285
</LinkButton>
8386
</div>
8487
<Paragraph variant="extra-small" className="mt-2 text-center">
85-
By connecting your GitHub account you agree to our{" "}
88+
By signing up you agree to our{" "}
8689
<TextLink
8790
href="https://trigger.dev/legal/terms"
8891
target="_blank"

apps/webapp/app/services/auth.server.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,24 @@ import type { AuthUser } from "./authUser";
33
import { addEmailLinkStrategy } from "./emailAuth.server";
44
import { addGitHubStrategy } from "./gitHubAuth.server";
55
import { sessionStorage } from "./sessionStorage.server";
6+
import { env } from "~/env.server";
67

78
// Create an instance of the authenticator, pass a generic with what
89
// strategies will return and will store in the session
910
const authenticator = new Authenticator<AuthUser>(sessionStorage);
1011

11-
addGitHubStrategy(authenticator);
12+
const isGithubAuthSupported =
13+
typeof env.AUTH_GITHUB_CLIENT_ID === "string" &&
14+
typeof env.AUTH_GITHUB_CLIENT_SECRET === "string";
15+
16+
if (env.AUTH_GITHUB_CLIENT_ID && env.AUTH_GITHUB_CLIENT_SECRET) {
17+
addGitHubStrategy(
18+
authenticator,
19+
env.AUTH_GITHUB_CLIENT_ID,
20+
env.AUTH_GITHUB_CLIENT_SECRET
21+
);
22+
}
23+
1224
addEmailLinkStrategy(authenticator);
1325

14-
export { authenticator };
26+
export { authenticator, isGithubAuthSupported };

apps/webapp/app/services/gitHubAuth.server.ts

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,44 @@ import { findOrCreateUser } from "~/models/user.server";
55
import type { AuthUser } from "./authUser";
66
import { postAuthentication } from "./postAuth.server";
77

8-
const gitHubStrategy = new GitHubStrategy(
9-
{
10-
clientID: env.GITHUB_CLIENT_ID ?? "",
11-
clientSecret: env.GITHUB_SECRET ?? "",
12-
callbackURL: `${env.LOGIN_ORIGIN}/auth/github/callback`,
13-
},
14-
async ({ accessToken, extraParams, profile }) => {
15-
const emails = profile.emails;
8+
export function addGitHubStrategy(
9+
authenticator: Authenticator<AuthUser>,
10+
clientID: string,
11+
clientSecret: string
12+
) {
13+
const gitHubStrategy = new GitHubStrategy(
14+
{
15+
clientID,
16+
clientSecret,
17+
callbackURL: `${env.LOGIN_ORIGIN}/auth/github/callback`,
18+
},
19+
async ({ accessToken, extraParams, profile }) => {
20+
const emails = profile.emails;
1621

17-
if (!emails) {
18-
throw new Error("GitHub login requires an email address");
19-
}
22+
if (!emails) {
23+
throw new Error("GitHub login requires an email address");
24+
}
2025

21-
try {
22-
const { user, isNewUser } = await findOrCreateUser({
23-
email: emails[0].value,
24-
authenticationMethod: "GITHUB",
25-
accessToken,
26-
authenticationProfile: profile,
27-
authenticationExtraParams: extraParams,
28-
});
26+
try {
27+
const { user, isNewUser } = await findOrCreateUser({
28+
email: emails[0].value,
29+
authenticationMethod: "GITHUB",
30+
accessToken,
31+
authenticationProfile: profile,
32+
authenticationExtraParams: extraParams,
33+
});
2934

30-
await postAuthentication({ user, isNewUser, loginMethod: "GITHUB" });
35+
await postAuthentication({ user, isNewUser, loginMethod: "GITHUB" });
3136

32-
return {
33-
userId: user.id,
34-
};
35-
} catch (error) {
36-
console.error(error);
37-
throw error;
37+
return {
38+
userId: user.id,
39+
};
40+
} catch (error) {
41+
console.error(error);
42+
throw error;
43+
}
3844
}
39-
}
40-
);
45+
);
4146

42-
export function addGitHubStrategy(authenticator: Authenticator<AuthUser>) {
4347
authenticator.use(gitHubStrategy);
4448
}

deploy/fly.toml

Lines changed: 0 additions & 20 deletions
This file was deleted.

docker/.env.example

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ DATABASE_HOST=database:5432
1313
DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${DATABASE_HOST}/${POSTGRES_DB}
1414

1515
# Github sign in OAUTH client id and secret
16-
GITHUB_CLIENT_ID=
17-
GITHUB_SECRET=
16+
AUTH_GITHUB_CLIENT_ID=
17+
AUTH_GITHUB_CLIENT_SECRET=
1818

1919
# E-mail settings
2020
FROM_EMAIL=

0 commit comments

Comments
 (0)