Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

added github oauth

versecafe70a38672f566
diff --git a/apps/web/.env.example b/apps/web/.env.example
index c264386724..6265ece699 100644
--- a/apps/web/.env.example
+++ b/apps/web/.env.example
@@ -54,6 +54,11 @@ STRIPE_WEBHOOK_SECRET=
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
+# Used for GitHub Login
+# Learn how to set this up here: https://next-auth.js.org/providers/github
+GITHUB_CLIENT_ID=
+GITHUB_CLIENT_SECRET=
+
# Edge Config – used for admin features like banning users, rate limiting etc.
# Create one here: https://vercel.com/docs/storage/edge-config
# Alternatively, if you don't need this, you can also remove `lib/edge-config.ts`
diff --git a/apps/web/app/app.dub.co/(auth)/login/form.tsx b/apps/web/app/app.dub.co/(auth)/login/form.tsx
index a97b5bbaed..b2cc5a2ff0 100644
--- a/apps/web/app/app.dub.co/(auth)/login/form.tsx
+++ b/apps/web/app/app.dub.co/(auth)/login/form.tsx
@@ -1,6 +1,6 @@
"use client";
-import { Button, Google, InfoTooltip } from "@dub/ui";
+import { Button, Github, Google, InfoTooltip } from "@dub/ui";
import { signIn } from "next-auth/react";
import Link from "next/link";
import { useSearchParams } from "next/navigation";
@@ -15,6 +15,7 @@ export default function LoginForm() {
const [noSuchAccount, setNoSuchAccount] = useState(false);
const [email, setEmail] = useState("");
const [clickedGoogle, setClickedGoogle] = useState(false);
+ const [clickedGitub, setClickedGithub] = useState(false);
const [clickedEmail, setClickedEmail] = useState(false);
const [clickedSSO, setClickedSSO] = useState(false);
@@ -37,6 +38,18 @@ export default function LoginForm() {
disabled={clickedEmail || clickedSSO}
icon={<Google className="h-4 w-4" />}
/>
+ <Button
+ text="Continue with GitHub"
+ onClick={() => {
+ setClickedGithub(true);
+ signIn("github", {
+ ...(next && next.length > 0 ? { callbackUrl: next } : {}),
+ });
+ }}
+ loading={clickedGitub}
+ disabled={clickedEmail || clickedSSO}
+ icon={<Github className="h-4 w-4" />}
+ />
<form
onSubmit={async (e) => {
e.preventDefault();
diff --git a/apps/web/app/app.dub.co/(auth)/register/form.tsx b/apps/web/app/app.dub.co/(auth)/register/form.tsx
index 1a44aac170..18f8a4f45f 100644
--- a/apps/web/app/app.dub.co/(auth)/register/form.tsx
+++ b/apps/web/app/app.dub.co/(auth)/register/form.tsx
@@ -1,6 +1,6 @@
"use client";
-import { Button, Google } from "@dub/ui";
+import { Button, Github, Google } from "@dub/ui";
import { signIn } from "next-auth/react";
import Link from "next/link";
import { useSearchParams } from "next/navigation";
@@ -10,6 +10,7 @@ export default function RegisterForm() {
const searchParams = useSearchParams();
const next = searchParams?.get("next");
const [clickedGoogle, setClickedGoogle] = useState(false);
+ const [clickedGitub, setClickedGithub] = useState(false);
return (
<>
@@ -24,6 +25,17 @@ export default function RegisterForm() {
loading={clickedGoogle}
icon={<Google className="h-4 w-4" />}
/>
+ <Button
+ text="Continue with GitHub"
+ onClick={() => {
+ setClickedGithub(true);
+ signIn("github", {
+ ...(next && next.length > 0 ? { callbackUrl: next } : {}),
+ });
+ }}
+ loading={clickedGitub}
+ icon={<Github className="h-4 w-4" />}
+ />
<p className="text-center text-sm text-gray-500">
Already have an account?{" "}
<Link
diff --git a/apps/web/lib/auth/options.ts b/apps/web/lib/auth/options.ts
index c782432d0b..166101b892 100644
--- a/apps/web/lib/auth/options.ts
+++ b/apps/web/lib/auth/options.ts
@@ -8,6 +8,7 @@ import WelcomeEmail from "emails/welcome-email";
import { type NextAuthOptions } from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
import EmailProvider from "next-auth/providers/email";
+import GithubProvider from "next-auth/providers/github";
import GoogleProvider from "next-auth/providers/google";
const VERCEL_DEPLOYMENT = !!process.env.VERCEL_URL;
@@ -33,6 +34,11 @@ export const authOptions: NextAuthOptions = {
clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
allowDangerousEmailAccountLinking: true,
}),
+ GithubProvider({
+ clientId: process.env.GITHUB_CLIENT_ID as string,
+ clientSecret: process.env.GITHUB_CLIENT_SECRET as string,
+ allowDangerousEmailAccountLinking: true,
+ }),
{
id: "saml",
name: "BoxyHQ",
@@ -196,6 +202,24 @@ export const authOptions: NextAuthOptions = {
},
});
}
+ } else if (account?.provider === "github") {
+ console.log("github user!");
+ const userExists = await prisma.user.findUnique({
+ where: { email: user.email },
+ select: { name: true },
+ });
+ // if the user already exists via email,
+ // update the user with their name and image from Github
+ if (userExists && !userExists.name) {
+ await prisma.user.update({
+ where: { email: user.email },
+ data: {
+ name: profile?.name,
+ // @ts-ignore - this is a bug in the types, `picture` is a valid on the `Profile` type
+ image: profile?.picture,
+ },
+ });
+ }
} else if (
account?.provider === "saml" ||
account?.provider === "saml-idp"