Skip to content

Commit 0117464

Browse files
authored
♻️ Clean up login page code (#1525)
1 parent fa3f5a9 commit 0117464

File tree

5 files changed

+50
-107
lines changed

5 files changed

+50
-107
lines changed
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
1-
"use client";
21
import { Button } from "@rallly/ui/button";
32
import { signIn } from "next-auth/react";
3+
import { Trans } from "react-i18next/TransWithoutContext";
4+
5+
import { getTranslation } from "@/i18n/server";
6+
7+
export async function LoginWithOIDC({ name }: { name: string }) {
8+
const { t } = await getTranslation();
49

5-
export function LoginWithOIDC({ children }: { children: React.ReactNode }) {
610
return (
711
<Button
812
onClick={() => {
913
signIn("oidc");
1014
}}
1115
variant="link"
1216
>
13-
{children}
17+
<Trans
18+
t={t}
19+
i18nKey="continueWithProvider"
20+
ns="app"
21+
defaultValue="Login with {{provider}}"
22+
values={{ provider: name }}
23+
/>
1424
</Button>
1525
);
1626
}

apps/web/src/app/[locale]/(auth)/login/components/or-divider.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
export function OrDivider({ text }: { text: string }) {
1+
import { getTranslation } from "@/i18n/server";
2+
3+
export async function OrDivider() {
4+
const { t } = await getTranslation();
25
return (
36
<div className="flex items-center gap-x-2.5">
47
<hr className="grow border-gray-100" />
5-
<div className="text-muted-foreground lowercase">{text}</div>
8+
<div className="text-muted-foreground lowercase">{t("or")}</div>
69
<hr className="grow border-gray-100" />
710
</div>
811
);

apps/web/src/app/[locale]/(auth)/login/page.tsx

Lines changed: 18 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { unstable_cache } from "next/cache";
21
import Link from "next/link";
3-
import { getProviders } from "next-auth/react";
42
import { Trans } from "react-i18next/TransWithoutContext";
53

4+
import { getOAuthProviders } from "@/auth";
65
import { getTranslation } from "@/i18n/server";
76

87
import {
@@ -17,44 +16,20 @@ import { AuthErrors } from "./components/auth-errors";
1716
import { LoginWithEmailForm } from "./components/login-email-form";
1817
import { LoginWithOIDC } from "./components/login-with-oidc";
1918
import { OrDivider } from "./components/or-divider";
20-
import { SSOProviders } from "./sso-providers";
21-
22-
export const dynamic = "force-dynamic";
23-
export const revalidate = 0;
24-
25-
async function getOAuthProviders() {
26-
const providers = await getProviders();
27-
if (!providers) {
28-
return [];
29-
}
30-
31-
return Object.values(providers)
32-
.filter((provider) => provider.type === "oauth")
33-
.map((provider) => ({
34-
id: provider.id,
35-
name: provider.name,
36-
}));
37-
}
38-
39-
// Cache the OAuth providers to avoid re-fetching them on every page load
40-
const getCachedOAuthProviders = unstable_cache(
41-
getOAuthProviders,
42-
["oauth-providers"],
43-
{
44-
revalidate: false,
45-
},
46-
);
19+
import { SSOProvider } from "./components/sso-provider";
4720

4821
export default async function LoginPage() {
4922
const { t } = await getTranslation();
50-
const oAuthProviders = await getCachedOAuthProviders();
51-
const socialProviders = oAuthProviders.filter(
52-
(provider) => provider.id !== "oidc",
53-
);
23+
const oAuthProviders = getOAuthProviders();
24+
25+
const hasAlternateLoginMethods = oAuthProviders.length > 0;
5426

5527
const oidcProvider = oAuthProviders.find(
5628
(provider) => provider.id === "oidc",
5729
);
30+
const socialProviders = oAuthProviders.filter(
31+
(provider) => provider.id !== "oidc",
32+
);
5833

5934
return (
6035
<AuthPageContainer>
@@ -73,25 +48,19 @@ export default async function LoginPage() {
7348
</AuthPageHeader>
7449
<AuthPageContent>
7550
<LoginWithEmailForm />
76-
{oidcProvider ? (
77-
<div className="text-center">
78-
<LoginWithOIDC>
79-
<Trans
80-
t={t}
81-
i18nKey="continueWithProvider"
82-
ns="app"
83-
defaultValue="Login with {{provider}}"
84-
values={{ provider: oidcProvider.name }}
51+
{hasAlternateLoginMethods ? <OrDivider /> : null}
52+
{oidcProvider ? <LoginWithOIDC name={oidcProvider.name} /> : null}
53+
{socialProviders ? (
54+
<div className="grid gap-4">
55+
{socialProviders.map((provider) => (
56+
<SSOProvider
57+
key={provider.id}
58+
providerId={provider.id}
59+
name={provider.name}
8560
/>
86-
</LoginWithOIDC>
61+
))}
8762
</div>
8863
) : null}
89-
{socialProviders.length > 0 ? (
90-
<>
91-
<OrDivider text={t("or")} />
92-
<SSOProviders />
93-
</>
94-
) : null}
9564
</AuthPageContent>
9665
<AuthErrors />
9766
<AuthPageExternal>

apps/web/src/app/[locale]/(auth)/login/sso-providers.tsx

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

apps/web/src/auth.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,3 +355,17 @@ export const isEmailBlocked = (email: string) => {
355355
}
356356
return false;
357357
};
358+
359+
export function getOAuthProviders(): {
360+
id: string;
361+
name: string;
362+
}[] {
363+
return providers
364+
.filter((provider) => provider.type === "oauth")
365+
.map((provider) => {
366+
return {
367+
id: provider.id,
368+
name: provider.options?.name || provider.name,
369+
};
370+
});
371+
}

0 commit comments

Comments
 (0)