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();
// states
const [loginCallBackURL, setLoginCallBackURL] = useState(undefined);
const [gitCode, setGitCode] = useState<null | string>(null);
useEffect(() => {
if (code) {
if (code && !gitCode) {
setGitCode(code.toString());
handleSignIn(code.toString());
}
}, [code, handleSignIn]);
}, [code, gitCode, handleSignIn]);
useEffect(() => {
const origin =

View File

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

View File

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

View File

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

View File

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