Skip to content

Commit

Permalink
Fix using NODE_ENV (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikuroXina authored Aug 26, 2024
1 parent cff1563 commit cc96598
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 56 deletions.
27 changes: 20 additions & 7 deletions app/.server/store/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import {
TWITTER_CLIENT_ID,
} from "./consts";

const urlBase =
process.env.NODE_ENV === "production"
const urlBase = (mode: string) =>
mode === "production"
? "https://edit.members.approvers.dev"
: "http://localhost:3000";

Expand All @@ -24,6 +24,7 @@ export type Member = {
export const getAuthenticator = (
cookieSecret: string,
discordClientSecret: string,
mode: string,
) => {
const authenticator = new Authenticator<Member>(
createCookieSessionStorage({
Expand All @@ -44,7 +45,7 @@ export const getAuthenticator = (
clientSecret: discordClientSecret,
authorizationEndpoint: "https://discord.com/oauth2/authorize",
tokenEndpoint: "https://discord.com/api/v10/oauth2/token",
redirectURI: new URL("/redirect", urlBase),
redirectURI: new URL("/redirect", urlBase(mode)),
tokenRevocationEndpoint:
"https://discord.com/api/v10/oauth2/token/revoke",
codeChallengeMethod: "S256",
Expand Down Expand Up @@ -78,7 +79,10 @@ export type GitHubAssociation = {
id: string;
name: string;
};
export const getGithubAssocAuthenticator = (githubClientSecret: string) => {
export const getGithubAssocAuthenticator = (
githubClientSecret: string,
mode: string,
) => {
const assocAuthenticator = new Authenticator<GitHubAssociation>(
createMemorySessionStorage(),
);
Expand All @@ -88,7 +92,10 @@ export const getGithubAssocAuthenticator = (githubClientSecret: string) => {
{
clientId: GITHUB_CLIENT_ID,
clientSecret: githubClientSecret,
redirectURI: new URL("/dashboard/redirect-github", urlBase),
redirectURI: new URL(
"/dashboard/redirect-github",
urlBase(mode),
),
},
async ({ profile }) => {
return { id: profile.id, name: profile.displayName };
Expand All @@ -103,7 +110,10 @@ export type TwitterAssociation = {
id: string;
name: string;
};
export const getTwitterAssocAuthenticator = (twitterClientSecret: string) => {
export const getTwitterAssocAuthenticator = (
twitterClientSecret: string,
mode: string,
) => {
const assocAuthenticator = new Authenticator<TwitterAssociation>(
createMemorySessionStorage(),
);
Expand All @@ -114,7 +124,10 @@ export const getTwitterAssocAuthenticator = (twitterClientSecret: string) => {
clientSecret: twitterClientSecret,
authorizationEndpoint: "https://twitter.com/i/oauth2/authorize",
tokenEndpoint: "https://api.twitter.com/2/oauth2/token",
redirectURI: new URL("/dashboard/redirect-twitter", urlBase),
redirectURI: new URL(
"/dashboard/redirect-twitter",
urlBase(mode),
),
scopes: ["tweet.read", "users.read"],
codeChallengeMethod: "S256",
authenticateWith: "http_basic_auth",
Expand Down
19 changes: 10 additions & 9 deletions app/routes/_index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ export default function Index() {
}

export async function action({ request, context }: ActionFunctionArgs) {
const { COOKIE_SECRET, DISCORD_CLIENT_SECRET } = context.cloudflare.env;
return getAuthenticator(COOKIE_SECRET, DISCORD_CLIENT_SECRET).authenticate(
"discord-oauth",
request,
{
successRedirect: "/dashboard",
failureRedirect: "/",
},
);
const { COOKIE_SECRET, DISCORD_CLIENT_SECRET, NODE_ENV } =
context.cloudflare.env;
return getAuthenticator(
COOKIE_SECRET,
DISCORD_CLIENT_SECRET,
NODE_ENV,
).authenticate("discord-oauth", request, {
successRedirect: "/dashboard",
failureRedirect: "/",
});
}
22 changes: 13 additions & 9 deletions app/routes/dashboard.add-github.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,25 @@ import {
} from "../.server/store/auth";

export async function action({ request, context }: ActionFunctionArgs) {
const { COOKIE_SECRET, DISCORD_CLIENT_SECRET, GITHUB_CLIENT_SECRET } =
context.cloudflare.env;
const {
COOKIE_SECRET,
DISCORD_CLIENT_SECRET,
GITHUB_CLIENT_SECRET,
NODE_ENV,
} = context.cloudflare.env;
await getAuthenticator(
COOKIE_SECRET,
DISCORD_CLIENT_SECRET,
NODE_ENV,
).isAuthenticated(request, {
failureRedirect: "/",
});
return getGithubAssocAuthenticator(GITHUB_CLIENT_SECRET).authenticate(
"github-oauth",
request,
{
failureRedirect: "/dashboard",
},
);
return getGithubAssocAuthenticator(
GITHUB_CLIENT_SECRET,
NODE_ENV,
).authenticate("github-oauth", request, {
failureRedirect: "/dashboard",
});
}

export async function loader() {
Expand Down
22 changes: 13 additions & 9 deletions app/routes/dashboard.add-twitter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,25 @@ import {
} from "../.server/store/auth";

export async function action({ request, context }: ActionFunctionArgs) {
const { COOKIE_SECRET, DISCORD_CLIENT_SECRET, TWITTER_CLIENT_SECRET } =
context.cloudflare.env;
const {
COOKIE_SECRET,
DISCORD_CLIENT_SECRET,
TWITTER_CLIENT_SECRET,
NODE_ENV,
} = context.cloudflare.env;
await getAuthenticator(
COOKIE_SECRET,
DISCORD_CLIENT_SECRET,
NODE_ENV,
).isAuthenticated(request, {
failureRedirect: "/",
});
return getTwitterAssocAuthenticator(TWITTER_CLIENT_SECRET).authenticate(
"twitter-oauth",
request,
{
failureRedirect: "/dashboard",
},
);
return getTwitterAssocAuthenticator(
TWITTER_CLIENT_SECRET,
NODE_ENV,
).authenticate("twitter-oauth", request, {
failureRedirect: "/dashboard",
});
}

export async function loader() {
Expand Down
14 changes: 11 additions & 3 deletions app/routes/dashboard.redirect-github.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,23 @@ export default function Redirect(): JSX.Element {
}

export async function loader({ request, context }: LoaderFunctionArgs) {
const { COOKIE_SECRET, DISCORD_CLIENT_SECRET, GITHUB_CLIENT_SECRET } =
context.cloudflare.env;
const {
COOKIE_SECRET,
DISCORD_CLIENT_SECRET,
GITHUB_CLIENT_SECRET,
NODE_ENV,
} = context.cloudflare.env;
const { discordToken, discordId } = await getAuthenticator(
COOKIE_SECRET,
DISCORD_CLIENT_SECRET,
NODE_ENV,
).isAuthenticated(request, {
failureRedirect: "/",
});
const githubAssocAuth = getGithubAssocAuthenticator(GITHUB_CLIENT_SECRET);
const githubAssocAuth = getGithubAssocAuthenticator(
GITHUB_CLIENT_SECRET,
NODE_ENV,
);
const { id: addingId, name: addingName } =
await githubAssocAuth.authenticate("github-oauth", request, {
failureRedirect: "/dashboard",
Expand Down
10 changes: 8 additions & 2 deletions app/routes/dashboard.redirect-twitter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,22 @@ export default function Redirect(): JSX.Element {
}

export async function loader({ request, context }: LoaderFunctionArgs) {
const { COOKIE_SECRET, DISCORD_CLIENT_SECRET, TWITTER_CLIENT_SECRET } =
context.cloudflare.env;
const {
COOKIE_SECRET,
DISCORD_CLIENT_SECRET,
TWITTER_CLIENT_SECRET,
NODE_ENV,
} = context.cloudflare.env;
const { discordToken, discordId } = await getAuthenticator(
COOKIE_SECRET,
DISCORD_CLIENT_SECRET,
NODE_ENV,
).isAuthenticated(request, {
failureRedirect: "/",
});
const twitterAssocAuth = getTwitterAssocAuthenticator(
TWITTER_CLIENT_SECRET,
NODE_ENV,
);
const { id: addingId, name: addingName } =
await twitterAssocAuth.authenticate("twitter-oauth", request, {
Expand Down
4 changes: 3 additions & 1 deletion app/routes/dashboard.remove.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { getAssociationLinks } from "../.server/store/association";
import { getAuthenticator } from "../.server/store/auth";

export async function action({ request, context }: ActionFunctionArgs) {
const { COOKIE_SECRET, DISCORD_CLIENT_SECRET } = context.cloudflare.env;
const { COOKIE_SECRET, DISCORD_CLIENT_SECRET, NODE_ENV } =
context.cloudflare.env;
const { discordToken, discordId } = await getAuthenticator(
COOKIE_SECRET,
DISCORD_CLIENT_SECRET,
NODE_ENV,
).isAuthenticated(request, {
failureRedirect: "/",
});
Expand Down
6 changes: 4 additions & 2 deletions app/routes/dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import {
import { getAuthenticator } from "../.server/store/auth";

export async function loader({ request, context }: LoaderFunctionArgs) {
const { COOKIE_SECRET, DISCORD_CLIENT_SECRET } = context.cloudflare.env;
const { COOKIE_SECRET, DISCORD_CLIENT_SECRET, NODE_ENV } =
context.cloudflare.env;
const { discordId } = await getAuthenticator(
COOKIE_SECRET,
DISCORD_CLIENT_SECRET,
NODE_ENV,
).isAuthenticated(request, {
failureRedirect: "/",
});
Expand All @@ -28,7 +30,7 @@ const AccountIcon = ({ type }: { type: "github" | "twitter" }): JSX.Element =>
({
github: <FaGithub />,
twitter: <FaTwitter />,
})[type];
}[type]);

const AccountList = ({ list }: { list: AssociationLinks }): JSX.Element =>
list.length === 0 ? (
Expand Down
12 changes: 7 additions & 5 deletions app/routes/logout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import type { LoaderFunctionArgs } from "@remix-run/cloudflare";
import { getAuthenticator } from "../.server/store/auth";

export async function loader({ request, context }: LoaderFunctionArgs) {
const { COOKIE_SECRET, DISCORD_CLIENT_SECRET } = context.cloudflare.env;
await getAuthenticator(COOKIE_SECRET, DISCORD_CLIENT_SECRET).logout(
request,
{ redirectTo: "/" },
);
const { COOKIE_SECRET, DISCORD_CLIENT_SECRET, NODE_ENV } =
context.cloudflare.env;
await getAuthenticator(
COOKIE_SECRET,
DISCORD_CLIENT_SECRET,
NODE_ENV,
).logout(request, { redirectTo: "/" });
}

export default function Redirect(): JSX.Element {
Expand Down
19 changes: 10 additions & 9 deletions app/routes/redirect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ export default function Redirect(): JSX.Element {
}

export async function loader({ request, context }: LoaderFunctionArgs) {
const { COOKIE_SECRET, DISCORD_CLIENT_SECRET } = context.cloudflare.env;
return getAuthenticator(COOKIE_SECRET, DISCORD_CLIENT_SECRET).authenticate(
"discord-oauth",
request,
{
successRedirect: "/dashboard",
failureRedirect: "/",
},
);
const { COOKIE_SECRET, DISCORD_CLIENT_SECRET, NODE_ENV } =
context.cloudflare.env;
return getAuthenticator(
COOKIE_SECRET,
DISCORD_CLIENT_SECRET,
NODE_ENV,
).authenticate("discord-oauth", request, {
successRedirect: "/dashboard",
failureRedirect: "/",
});
}
1 change: 1 addition & 0 deletions load-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { type PlatformProxy } from "wrangler";
// even if no `wrangler.toml` exists.

type Env = {
NODE_ENV: string;
COOKIE_SECRET: string;
DISCORD_CLIENT_SECRET: string;
TWITTER_CLIENT_SECRET: string;
Expand Down

0 comments on commit cc96598

Please sign in to comment.