From dc6ae088bf2685c8e5a33ccc5bac4857aa555ad5 Mon Sep 17 00:00:00 2001 From: Moritz Rengert <42251569+moritzrengert@users.noreply.github.com> Date: Thu, 13 Jul 2023 11:54:47 +0200 Subject: [PATCH] Fix allow invited users to signin/signup with Github (#534) * pass inviteToken through github / google redirect url * keep invite token when switching between signup and login --- apps/web/components/auth/GithubButton.tsx | 10 ++++++++-- apps/web/components/auth/GoogleButton.tsx | 10 ++++++++-- apps/web/components/auth/SigninForm.tsx | 11 ++++++++--- apps/web/components/auth/SignupForm.tsx | 13 +++++++++---- pnpm-lock.yaml | 8 ++++---- 5 files changed, 37 insertions(+), 15 deletions(-) diff --git a/apps/web/components/auth/GithubButton.tsx b/apps/web/components/auth/GithubButton.tsx index 0234923dd8..2dbd005bde 100644 --- a/apps/web/components/auth/GithubButton.tsx +++ b/apps/web/components/auth/GithubButton.tsx @@ -4,11 +4,17 @@ import { Button } from "@formbricks/ui"; import { signIn } from "next-auth/react"; import { FaGithub } from "react-icons/fa"; -export const GithubButton = ({ text = "Continue with Github" }) => { +export const GithubButton = ({ + text = "Continue with Github", + inviteUrl, +}: { + text?: string; + inviteUrl?: string | null; +}) => { const handleLogin = async () => { await signIn("github", { redirect: true, - callbackUrl: "/", // redirect after login to / + callbackUrl: inviteUrl ? inviteUrl : "/", // redirect after login to / }); }; diff --git a/apps/web/components/auth/GoogleButton.tsx b/apps/web/components/auth/GoogleButton.tsx index c9b6348c02..1a8bff78ac 100644 --- a/apps/web/components/auth/GoogleButton.tsx +++ b/apps/web/components/auth/GoogleButton.tsx @@ -4,11 +4,17 @@ import { Button } from "@formbricks/ui"; import { signIn } from "next-auth/react"; import { FaGoogle } from "react-icons/fa"; -export const GoogleButton = ({ text = "Continue with Google" }) => { +export const GoogleButton = ({ + text = "Continue with Google", + inviteUrl, +}: { + text?: string; + inviteUrl?: string | null; +}) => { const handleLogin = async () => { await signIn("google", { redirect: true, - callbackUrl: "/", // redirect after login to / + callbackUrl: inviteUrl ? inviteUrl : "/", // redirect after login to / }); }; diff --git a/apps/web/components/auth/SigninForm.tsx b/apps/web/components/auth/SigninForm.tsx index 46fc2a02ff..6f0c15fe61 100644 --- a/apps/web/components/auth/SigninForm.tsx +++ b/apps/web/components/auth/SigninForm.tsx @@ -30,6 +30,9 @@ export const SigninForm = () => { const [isPasswordFocused, setIsPasswordFocused] = useState(false); const formRef = useRef(null); + const callbackUrl = searchParams?.get("callbackUrl"); + const inviteToken = callbackUrl ? new URL(callbackUrl).searchParams.get("token") : null; + const checkFormValidity = () => { // If both fields are filled, enable the button if (formRef.current) { @@ -108,12 +111,12 @@ export const SigninForm = () => { {env.NEXT_PUBLIC_GOOGLE_AUTH_ENABLED === "1" && ( <> - + )} {env.NEXT_PUBLIC_GITHUB_AUTH_ENABLED === "1" && ( <> - + )} @@ -121,7 +124,9 @@ export const SigninForm = () => {
New to Formbricks?
- + Create an account
diff --git a/apps/web/components/auth/SignupForm.tsx b/apps/web/components/auth/SignupForm.tsx index 4e772c7df8..efc567e25d 100644 --- a/apps/web/components/auth/SignupForm.tsx +++ b/apps/web/components/auth/SignupForm.tsx @@ -18,6 +18,9 @@ export const SignupForm = () => { const [signingUp, setSigningUp] = useState(false); const nameRef = useRef(null); + const inviteToken = searchParams?.get("inviteToken"); + const callbackUrl = env.NEXT_PUBLIC_WEBAPP_URL + "/invite?token=" + inviteToken; + const handleSubmit = async (e: any) => { e.preventDefault(); @@ -30,7 +33,7 @@ export const SignupForm = () => { e.target.elements.name.value, e.target.elements.email.value, e.target.elements.password.value, - searchParams?.get("inviteToken") + inviteToken ); const url = env.NEXT_PUBLIC_EMAIL_VERIFICATION_DISABLED === "1" @@ -165,12 +168,12 @@ export const SignupForm = () => { {env.NEXT_PUBLIC_GOOGLE_AUTH_ENABLED === "1" && ( <> - + )} {env.NEXT_PUBLIC_GITHUB_AUTH_ENABLED === "1" && ( <> - {" "} + )} @@ -207,7 +210,9 @@ export const SignupForm = () => {
Have an account?
- + Log in.
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a95441ad0b..90491d6e2b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1,9 +1,5 @@ lockfileVersion: '6.0' -settings: - autoInstallPeers: true - excludeLinksFromLockfile: false - importers: .: @@ -20781,3 +20777,7 @@ packages: /zwitch@2.0.4: resolution: {integrity: sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A==} dev: false + +settings: + autoInstallPeers: true + excludeLinksFromLockfile: false