Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat: Google oauth login

Richie McIlroye8b345df8c5c
diff --git a/.env.example b/.env.example
index 7231f53d1..6ed4f524e 100644
--- a/.env.example
+++ b/.env.example
@@ -71,3 +71,7 @@ RUST_BACKTRACE=full
NEXT_LOOPS_KEY=
CAP_DESKTOP_SENTRY_URL=https://efd3156d9c0a8a49bee3ee675bec80d8@o4506859771527168.ingest.us.sentry.io/4506859844403200
+
+# Google OAuth
+GOOGLE_CLIENT_ID="your-google-client-id"
+GOOGLE_CLIENT_SECRET="your-google-client-secret"
diff --git a/apps/web/app/login/form.tsx b/apps/web/app/login/form.tsx
index 40023264a..5bb23e5ea 100644
--- a/apps/web/app/login/form.tsx
+++ b/apps/web/app/login/form.tsx
@@ -12,10 +12,14 @@ export function LoginForm() {
const [email, setEmail] = useState("");
const [loading, setLoading] = useState(false);
const [emailSent, setEmailSent] = useState(false);
+ const [oauthError, setOauthError] = useState(false);
useEffect(() => {
const error = searchParams?.get("error");
- error && toast.error(error);
+ if (error === "OAuthAccountNotLinked") {
+ setOauthError(true);
+ toast.error(error);
+ }
}, [searchParams]);
useEffect(() => {
@@ -38,100 +42,163 @@ export function LoginForm() {
if (data.url) {
window.location.href = data.url;
}
- }, 2000); // Wait 2 seconds after email is sent
+ }, 2000);
}
}, [emailSent]);
+ const handleGoogleSignIn = () => {
+ signIn("google", {
+ ...(next && next.length > 0 ? { callbackUrl: next } : {}),
+ });
+ };
+
return (
<>
- <form
- onSubmit={async (e) => {
- e.preventDefault();
- if (!email) return;
+ <div className="flex flex-col space-y-3 fade-in-down animate-delay-2">
+ {process.env.NODE_ENV === "development" && !oauthError && (
+ <>
+ <Button
+ variant="dark"
+ size="lg"
+ className="h-12 text-lg flex items-center justify-center space-x-2"
+ onClick={handleGoogleSignIn}
+ disabled={loading}
+ >
+ <svg
+ className="w-4 h-4"
+ xmlns="http://www.w3.org/2000/svg"
+ fill="none"
+ viewBox="0 0 12 12"
+ >
+ <g fill="#E1E1E6" clipPath="url(#clip0)">
+ <path d="M11.762 6.138c0-.408-.033-.818-.104-1.22h-5.66V7.23H9.24a2.78 2.78 0 0 1-1.2 1.823v1.5h1.934c1.135-1.046 1.788-2.589 1.788-4.414"></path>
+ <path d="M5.999 12c1.618 0 2.983-.531 3.977-1.448l-1.933-1.5c-.538.367-1.233.574-2.042.574-1.565 0-2.892-1.056-3.369-2.476H.637v1.545A6 6 0 0 0 6 12"></path>
+ <path d="M2.63 7.15a3.6 3.6 0 0 1 0-2.297V3.307H.637a6 6 0 0 0 0 5.388z"></path>
+ <path d="M5.999 2.374a3.26 3.26 0 0 1 2.302.9l1.713-1.713A5.77 5.77 0 0 0 5.999 0 6 6 0 0 0 .637 3.307L2.63 4.852C3.104 3.43 4.434 2.374 6 2.374"></path>
+ </g>
+ <defs>
+ <clipPath id="clip0">
+ <path fill="#fff" d="M0 0h11.762v12H0z"></path>
+ </clipPath>
+ </defs>
+ </svg>
+ <span className="text-gray-50">Continue with Google</span>
+ </Button>
- setLoading(true);
- signIn("email", {
- email,
- redirect: false,
- ...(next && next.length > 0 ? { callbackUrl: next } : {}),
- })
- .then((res) => {
- setLoading(false);
- if (res?.ok && !res?.error) {
- setEmail("");
- setEmailSent(true);
- toast.success("Email sent - check your inbox!");
- } else {
- toast.error("Error sending email - try again?");
- }
- })
- .catch((err) => {
- setEmailSent(false);
- setLoading(false);
- toast.error("Error sending email - try again?");
- });
- }}
- className="flex flex-col space-y-3 fade-in-down animate-delay-2"
- >
- <div>
- <input
- id="email"
- name="email"
- autoFocus
- type="email"
- placeholder={emailSent ? "" : "[email protected]"}
- autoComplete="email"
- required
- value={email}
- disabled={emailSent}
- onChange={(e) => {
- setEmail(e.target.value);
- }}
- className="block w-full appearance-none rounded-full border border-gray-300 px-3 h-12 placeholder-gray-400 shadow-sm focus:border-black focus:outline-none focus:ring-black text-lg"
- />
- {process.env.NODE_ENV === "development" && (
- <div className="py-3 px-6 flex items-center justify-center bg-red-600 rounded-xl mt-3">
- <p className="text-white text-lg">
- <span className="font-bold text-white">Development mode:</span>{" "}
- Auth URL will be logged to your dev console.
- </p>
+ <div className="relative">
+ <div className="absolute inset-0 flex items-center">
+ <div className="w-full border-t border-blue-100" />
+ </div>
+ <div className="relative flex justify-center">
+ <span className="bg-white px-2 text-gray-500 rounded-xl text-xs">
+ Or
+ </span>
+ </div>
</div>
- )}
- </div>
- <Button
- variant="primary"
- size="lg"
- className="h-12 text-lg"
- type="submit"
- disabled={loading || emailSent}
+ </>
+ )}
+
+ {oauthError && (
+ <div className="mb-4 p-4 bg-red-600 rounded-lg">
+ <p className="text-sm text-gray-50">
+ It looks like you've previously used this email to sign up via
+ email login. Please enter your email below to receive a sign in
+ link.
+ </p>
+ </div>
+ )}
+
+ <form
+ onSubmit={async (e) => {
+ e.preventDefault();
+ if (!email) return;
+
+ setLoading(true);
+ signIn("email", {
+ email,
+ redirect: false,
+ ...(next && next.length > 0 ? { callbackUrl: next } : {}),
+ })
+ .then((res) => {
+ setLoading(false);
+ if (res?.ok && !res?.error) {
+ setEmail("");
+ setEmailSent(true);
+ toast.success("Email sent - check your inbox!");
+ } else {
+ toast.error("Error sending email - try again?");
+ }
+ })
+ .catch((err) => {
+ setEmailSent(false);
+ setLoading(false);
+ toast.error("Error sending email - try again?");
+ });
+ }}
+ className="flex flex-col space-y-3"
>
- {emailSent
- ? process.env.NODE_ENV === "development"
- ? "Email sent to your terminal"
- : "Email sent to your inbox"
- : "Continue with Email"}
- </Button>
- <p className="text-xs text-gray-500 pt-2">
- By typing your email and clicking continue, you acknowledge that you
- have both read and agree to Cap's{" "}
- <a
- href="/terms"
- target="_blank"
- className="text-gray-600 font-semibold text-xs"
- >
- Terms of Service
- </a>{" "}
- and{" "}
- <a
- href="/privacy"
- target="_blank"
- className="text-gray-600 font-semibold text-xs"
+ <div>
+ <input
+ id="email"
+ name="email"
+ autoFocus
+ type="email"
+ placeholder={emailSent ? "" : "[email protected]"}
+ autoComplete="email"
+ required
+ value={email}
+ disabled={emailSent}
+ onChange={(e) => {
+ setEmail(e.target.value);
+ }}
+ className="block w-full appearance-none rounded-full border border-gray-300 px-3 h-12 placeholder-gray-400 shadow-sm focus:border-black focus:outline-none focus:ring-black text-lg"
+ />
+ {process.env.NODE_ENV === "development" && (
+ <div className="py-3 px-6 flex items-center justify-center bg-red-600 rounded-xl mt-3">
+ <p className="text-white text-lg">
+ <span className="font-bold text-white">
+ Development mode:
+ </span>{" "}
+ Auth URL will be logged to your dev console.
+ </p>
+ </div>
+ )}
+ </div>
+ <Button
+ variant="primary"
+ size="lg"
+ className="h-12 text-lg"
+ type="submit"
+ disabled={loading || emailSent}
>
- Privacy Policy
- </a>
- .
- </p>
- </form>
+ {emailSent
+ ? process.env.NODE_ENV === "development"
+ ? "Email sent to your terminal"
+ : "Email sent to your inbox"
+ : "Continue with Email"}
+ </Button>
+ <p className="text-xs text-gray-500 pt-2">
+ By typing your email and clicking continue, you acknowledge that you
+ have both read and agree to Cap's{" "}
+ <a
+ href="/terms"
+ target="_blank"
+ className="text-gray-600 font-semibold text-xs"
+ >
+ Terms of Service
+ </a>{" "}
+ and{" "}
+ <a
+ href="/privacy"
+ target="_blank"
+ className="text-gray-600 font-semibold text-xs"
+ >
+ Privacy Policy
+ </a>
+ .
+ </p>
+ </form>
+ </div>
{emailSent && (
<div>
<button
diff --git a/packages/database/auth/auth-options.ts b/packages/database/auth/auth-options.ts
index adfed2698..d73ca831d 100644
--- a/packages/database/auth/auth-options.ts
+++ b/packages/database/auth/auth-options.ts
@@ -3,6 +3,7 @@ import { DrizzleAdapter } from "./drizzle-adapter";
import { db } from "../";
import { users } from "../schema";
import EmailProvider from "next-auth/providers/email";
+import GoogleProvider from "next-auth/providers/google";
import type { NextAuthOptions } from "next-auth";
import { sendEmail } from "../emails/config";
import { LoginLink } from "../emails/login-link";
@@ -27,6 +28,19 @@ export const authOptions: NextAuthOptions = {
signIn: "/login",
},
providers: [
+ GoogleProvider({
+ clientId: process.env.GOOGLE_CLIENT_ID!,
+ clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
+ authorization: {
+ params: {
+ scope: [
+ 'https://www.googleapis.com/auth/userinfo.email',
+ 'https://www.googleapis.com/auth/userinfo.profile',
+ ].join(' '),
+ prompt: "select_account"
+ }
+ }
+ }),
EmailProvider({
sendVerificationRequest({ identifier, url }) {
console.log({ NODE_ENV: process.env.NODE_ENV });
diff --git a/packages/ui/src/components/Button.tsx b/packages/ui/src/components/Button.tsx
index 9acbdf8af..85374ebc9 100644
--- a/packages/ui/src/components/Button.tsx
+++ b/packages/ui/src/components/Button.tsx
@@ -21,6 +21,7 @@ const buttonVariants = cva(
white:
"bg-white text-gray-500 hover:bg-gray-100 disabled:bg-gray-100 border-black/10",
gray: "bg-gray-200 text-gray-500 hover:bg-gray-200 disabled:bg-gray-100 border-transparent",
+ dark: "bg-[#2e2e2e] text-gray-50 hover:bg-[#383838] disabled:bg-[#383838] border-transparent",
},
size: {
xs: "font-[400] text-[0.75rem] px-[0.5rem] h-[1.25rem] ",