Skip to content

Commit

Permalink
fix google invites, after signup redirect to homepage if an org exists
Browse files Browse the repository at this point in the history
  • Loading branch information
Lamperoyge committed Feb 6, 2024
1 parent 6deab5f commit 61e9bce
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 15 deletions.
5 changes: 2 additions & 3 deletions wondrous-bot-admin/src/components/Layout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { GET_LOGGED_IN_USER_FULL_ACCESS_ORGS } from "graphql/queries";
import { useEffect, useState } from "react";
import { Outlet, useNavigate } from "react-router";
import { useLocation } from "react-router-dom";
import { EXCLUDED_PATHS, PAGES_WITHOUT_HEADER } from "utils/constants";
import { EXCLUDED_PATHS, LOCAL_STORAGE_ORG_ID_KEY, PAGES_WITHOUT_HEADER } from "utils/constants";
import { matchRoute } from "utils/common";
import ErrorCatcher from "components/ErrorCatcher";
import GlobalContext from "utils/context/GlobalContext";
Expand Down Expand Up @@ -47,7 +47,6 @@ const DefaultFallback = () => {
);
};

const LOCAL_STORAGE_ORG_ID_KEY = "default-org-id";
const Layout = () => {
const defaultActiveOrgId = localStorage.getItem(LOCAL_STORAGE_ORG_ID_KEY);
const location = useLocation();
Expand Down Expand Up @@ -82,7 +81,7 @@ const Layout = () => {
notifyOnNetworkStatusChange: true,
onCompleted: ({ getLoggedInUserFullAccessOrgs }) => {
if (getLoggedInUserFullAccessOrgs.length === 0) {
navigate("/onboarding");
navigate("/onboarding/welcome?ref=login");
return;
}
if (defaultActiveOrgId) {
Expand Down
7 changes: 6 additions & 1 deletion wondrous-bot-admin/src/components/Login/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,12 @@ function Login() {
{notSupportedChain && (
<ErrorTypography>Unsupported network, change to mainnet or a supported network</ErrorTypography>
)}
<GoogleOAuthButton />
<GoogleOAuthButton
params={{
token,
type,
}}
/>
<FormControl
fullWidth
sx={{
Expand Down
36 changes: 26 additions & 10 deletions wondrous-bot-admin/src/components/OAuth/GoogleOAuth/index.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
import { ButtonBase, Typography } from "@mui/material";
import { useGoogleLogin, useGoogleOneTapLogin } from "@react-oauth/google";
import GoogleIcon from "./icon";
import { useMutation } from "@apollo/client";
import { useLazyQuery, useMutation } from "@apollo/client";
import { GOOGLE_LOGIN_MUTATION } from "graphql/mutations";
import { storeAuthHeader } from "components/Auth";
import { useNavigate } from "react-router-dom";
import { getBaseUrl } from "utils/common";
const GoogleOAuthButton = ({ isSignup = false }) => {
import { getBaseUrl, handleUserOnboardingRedirect } from "utils/common";
import { GET_LOGGED_IN_USER_FULL_ACCESS_ORGS } from "graphql/queries";

const GoogleOAuthButton = ({ params = {} }) => {
const navigate = useNavigate();

const [getLoggedInUserFullAccessOrgs] = useLazyQuery(GET_LOGGED_IN_USER_FULL_ACCESS_ORGS, {
fetchPolicy: "cache-and-network",
nextFetchPolicy: "cache-first",
notifyOnNetworkStatusChange: true,
onCompleted: (data) => {
if (data?.getLoggedInUserFullAccessOrgs.length === 0) {
handleUserOnboardingRedirect(null, navigate, params, "/onboarding/welcome?ref=signup");
return;
}
handleUserOnboardingRedirect(null, navigate, params, "/");
},
});
const handleCompleted = async ({ token, user, callback }) => {
await storeAuthHeader(token, user);
callback?.();
};

const navigate = useNavigate();

const handleRedirect = () => {
if (isSignup) {
navigate("/onboarding/welcome?ref=signup");
}
navigate("/");
getLoggedInUserFullAccessOrgs({
variables: {
excludeSharedOrgs: true,
cmtyEnabled: true,
},
});
};

const [signInUser] = useMutation(GOOGLE_LOGIN_MUTATION, {
Expand All @@ -36,7 +52,7 @@ const GoogleOAuthButton = ({ isSignup = false }) => {
const login = useGoogleLogin({
flow: "auth-code",
scope: "email",
redirect_uri: `${getBaseUrl()}/oauth/google/callback`,
redirect_uri: `${getBaseUrl()}/oauth/google/callback`,
onSuccess: (tokenResponse) => {
const { code } = tokenResponse;
signInUser({ variables: { code: code } });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,12 @@ const CollectCredentials = ({ moveForward }) => {
Sign up to Wonderverse
</Typography>
<Box sx={{ width: "80%" }}>
<GoogleOAuthButton isSignup />
<GoogleOAuthButton
params={{
token,
type,
}}
/>
</Box>
{!notSupportedChain && errorMessage ? <ErrorTypography>{errorMessage}</ErrorTypography> : ""}
{notSupportedChain && (
Expand Down
2 changes: 2 additions & 0 deletions wondrous-bot-admin/src/utils/constants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -547,3 +547,5 @@ export const QUALIFYING_ACTION_TYPES = {
};

export const LOCKED_PATHS = ["/store", "/store/items/create", "/store/items/:id", "/analytics"];

export const LOCAL_STORAGE_ORG_ID_KEY = "default-org-id";

0 comments on commit 61e9bce

Please sign in to comment.