fix: social auth authentication workflow (#1264)

* fix: github login mutation

* dev: updated social auth workflow and handled multiple loads on user

* dev: mutaing user and updated analytics logout issue resolved

---------

Co-authored-by: Aaryan Khandelwal <aaryankhandu123@gmail.com>
This commit is contained in:
guru_sainath 2023-06-10 01:26:38 +05:30 committed by GitHub
parent 49f19c2c44
commit 66807bef0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 41 deletions

View File

@ -19,12 +19,14 @@ export const GithubLoginButton: FC<GithubLoginButtonProps> = (props) => {
} = useRouter(); } = useRouter();
// states // states
const [loginCallBackURL, setLoginCallBackURL] = useState(undefined); const [loginCallBackURL, setLoginCallBackURL] = useState(undefined);
const [gitCode, setGitCode] = useState<null | string>(null);
useEffect(() => { useEffect(() => {
if (code) { if (code && !gitCode) {
setGitCode(code.toString());
handleSignIn(code.toString()); handleSignIn(code.toString());
} }
}, [code, handleSignIn]); }, [code, gitCode, handleSignIn]);
useEffect(() => { useEffect(() => {
const origin = const origin =

View File

@ -24,6 +24,7 @@ const useUserAuth = (routeAuth: "sign-in" | "onboarding" | "admin" | null = "adm
mutate, mutate,
} = useSWR<ICurrentUserResponse>(CURRENT_USER, () => userService.currentUser(), { } = useSWR<ICurrentUserResponse>(CURRENT_USER, () => userService.currentUser(), {
refreshInterval: 0, refreshInterval: 0,
shouldRetryOnError: false,
}); });
useEffect(() => { useEffect(() => {

View File

@ -69,12 +69,13 @@ const Analytics = () => {
useEffect(() => { useEffect(() => {
if (!workspaceSlug) return; if (!workspaceSlug) return;
trackEventServices.trackAnalyticsEvent( if (user && workspaceSlug)
{ workspaceSlug: workspaceSlug?.toString() }, trackEventServices.trackAnalyticsEvent(
"WORKSPACE_SCOPE_AND_DEMAND_ANALYTICS", { workspaceSlug: workspaceSlug?.toString() },
user "WORKSPACE_SCOPE_AND_DEMAND_ANALYTICS",
); user
}, [workspaceSlug]); );
}, [user, workspaceSlug]);
return ( return (
<WorkspaceAuthorizationLayout <WorkspaceAuthorizationLayout

View File

@ -30,13 +30,13 @@ const HomePage: NextPage = () => {
const handleGoogleSignIn = async ({ clientId, credential }: any) => { const handleGoogleSignIn = async ({ clientId, credential }: any) => {
try { try {
if (clientId && credential) { if (clientId && credential) {
mutateUser( const socialAuthPayload = {
await authenticationService.socialAuth({ medium: "google",
medium: "google", credential,
credential, clientId,
clientId, };
}) const response = await authenticationService.socialAuth(socialAuthPayload);
); if (response && response?.user) mutateUser();
} else { } else {
throw Error("Cant find credentials"); throw Error("Cant find credentials");
} }
@ -53,13 +53,13 @@ const HomePage: NextPage = () => {
const handleGithubSignIn = async (credential: string) => { const handleGithubSignIn = async (credential: string) => {
try { try {
if (process.env.NEXT_PUBLIC_GITHUB_ID && credential) { if (process.env.NEXT_PUBLIC_GITHUB_ID && credential) {
mutateUser( const socialAuthPayload = {
await authenticationService.socialAuth({ medium: "github",
medium: "github", credential,
credential, clientId: process.env.NEXT_PUBLIC_GITHUB_ID,
clientId: process.env.NEXT_PUBLIC_GITHUB_ID, };
}) const response = await authenticationService.socialAuth(socialAuthPayload);
); if (response && response?.user) mutateUser();
} else { } else {
throw Error("Cant find credentials"); throw Error("Cant find credentials");
} }

View File

@ -1,7 +1,6 @@
import { useState } from "react"; import { useState } from "react";
import Image from "next/image"; import Router from "next/router";
import Router, { useRouter } from "next/router";
import { mutate } from "swr"; import { mutate } from "swr";
@ -38,8 +37,6 @@ const Onboarding: NextPage = () => {
const [workspace, setWorkspace] = useState(); const [workspace, setWorkspace] = useState();
const router = useRouter();
const { user, mutateUser } = useUserAuth("onboarding"); const { user, mutateUser } = useUserAuth("onboarding");
return ( return (
@ -89,21 +86,7 @@ const Onboarding: NextPage = () => {
userService userService
.updateUserOnBoard({ userRole }, user) .updateUserOnBoard({ userRole }, user)
.then(async () => { .then(async () => {
mutate<ICurrentUserResponse>( mutateUser();
CURRENT_USER,
(prevData) => {
if (!prevData) return prevData;
return {
...prevData,
user: {
...prevData.user,
is_onboarded: true,
},
};
},
false
);
const userWorkspaces = await workspaceService.userWorkspaces(); const userWorkspaces = await workspaceService.userWorkspaces();
const lastActiveWorkspace = const lastActiveWorkspace =