Skip to content

Commit b8d13a5

Browse files
committed
feat: enhance authentication flow with error handling and enable/disable options for GitHub and Google sign-in
1 parent c886b64 commit b8d13a5

File tree

4 files changed

+35
-13
lines changed

4 files changed

+35
-13
lines changed

apps/platform/src/app/auth/page.tsx

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { useRouter } from "next/navigation";
1717
import { DISABLE_ANONYMOUS_AUTH } from "@/lib/utils";
1818
import { FcGoogle } from "react-icons/fc";
1919
import { FaGithub } from "react-icons/fa";
20+
import { toast } from "sonner";
2021
export default function AuthPage() {
2122
const [isLoading, setIsLoading] = useState(false);
2223

@@ -25,10 +26,18 @@ export default function AuthPage() {
2526
const handleGitHubSignIn = async () => {
2627
setIsLoading(true);
2728
try {
28-
await authClient.signIn.social({
29+
const res = await authClient.signIn.social({
2930
provider: "github",
3031
callbackURL: "/pad",
3132
});
33+
34+
if (res.error) {
35+
if (res.error.code === "PROVIDER_NOT_FOUND") {
36+
toast(`Google sign in is disabled due to not configured.`);
37+
}
38+
console.log("sign in error:", res.error);
39+
return;
40+
}
3241
} catch (error) {
3342
console.error("Sign in error:", error);
3443
} finally {
@@ -43,7 +52,14 @@ export default function AuthPage() {
4352
}
4453
setIsLoading(true);
4554
try {
46-
await authClient.signIn.anonymous();
55+
const res = await authClient.signIn.anonymous();
56+
if (res.error) {
57+
if (res.error.code === "PROVIDER_NOT_FOUND") {
58+
toast(`Google sign in is disabled due to not configured.`);
59+
}
60+
console.log("sign in error:", res.error);
61+
return;
62+
}
4763
router.refresh();
4864
router.push("/pad");
4965
} catch (error) {
@@ -56,12 +72,20 @@ export default function AuthPage() {
5672
async function handleGoogleSignIn() {
5773
setIsLoading(true);
5874
try {
59-
await authClient.signIn.social({
75+
const res = await authClient.signIn.social({
6076
provider: "google",
6177
callbackURL: "/pad",
6278
});
63-
} catch (error) {
64-
console.error("Google sign in error:", error);
79+
80+
if (res.error) {
81+
if (res.error.code === "PROVIDER_NOT_FOUND") {
82+
toast(`Google sign in is disabled due to not configured.`);
83+
}
84+
console.log("sign in error:", res.error);
85+
return;
86+
}
87+
} catch (error: any) {
88+
console.log("Google sign in error:", error);
6589
} finally {
6690
setIsLoading(false);
6791
}

apps/platform/src/components/core/create-map-input.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@ export default function CreateMapInput() {
6464
sessionHelpers.startSearch(topic);
6565
};
6666

67-
console.log("state", state);
68-
console.log("isLoading", isLoading);
69-
7067
return (
7168
<div className="relative">
7269
<form onSubmit={handleCreateMap} className="flex items-stretch gap-2">

apps/platform/src/lib/auth.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ const restrictedUsernames = [
4343
"adminpanel",
4444
];
4545

46+
const ENABLE_GITHUB_AUTH = !!process.env.GITHUB_CLIENT_ID;
47+
const ENABLE_GOOGLE_AUTH = !!process.env.GOOGLE_CLIENT_ID;
48+
4649
export const auth = betterAuth({
4750
database: prismaAdapter(prisma, {
4851
provider: "postgresql",
@@ -58,7 +61,7 @@ export const auth = betterAuth({
5861
username: profile.login.replaceAll("-", "_"),
5962
displayName: profile.name || profile.login,
6063
}),
61-
disableSignUp: SELF_HOST,
64+
enabled: ENABLE_GITHUB_AUTH,
6265
},
6366
google: {
6467
clientId: process.env.GOOGLE_CLIENT_ID || "",
@@ -70,7 +73,7 @@ export const auth = betterAuth({
7073
username: profile.email.split("@")[0].replaceAll("-", "_"),
7174
displayName: profile.name,
7275
}),
73-
disableSignUp: SELF_HOST,
76+
enabled: ENABLE_GOOGLE_AUTH,
7477
},
7578
},
7679
plugins: [

apps/platform/src/lib/utils.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ export const SERVER_URL =
3535
export const WS_SERVER_URL =
3636
process.env.NEXT_PUBLIC_WS_SERVER_URL || "ws://localhost:8000";
3737

38-
export const DISABLE_ANONYMOUS_AUTH = SELF_HOST
39-
? true
40-
: process.env.NEXT_PUBLIC_DISABLE_ANONYMOUS_AUTH === "true";
38+
export const DISABLE_ANONYMOUS_AUTH = !SELF_HOST;
4139

4240
export const getAnimationVariants = (swipeDirection: Direction | null) => {
4341
// Default variants if no direction specified

0 commit comments

Comments
 (0)