From b055ec5b51d51598a2e28f44e6152b9afa7171ab Mon Sep 17 00:00:00 2001 From: Rishivikram N Date: Tue, 7 Jun 2022 12:31:43 +0530 Subject: [PATCH] [Fix] Google login missing workspace details --- apps/webapp/src/Stores/useAuth.ts | 111 +++++++++++++++--------------- 1 file changed, 57 insertions(+), 54 deletions(-) diff --git a/apps/webapp/src/Stores/useAuth.ts b/apps/webapp/src/Stores/useAuth.ts index 8b4d35a2e..ceb6b5cf9 100644 --- a/apps/webapp/src/Stores/useAuth.ts +++ b/apps/webapp/src/Stores/useAuth.ts @@ -77,64 +77,16 @@ export const useAuthentication = () => { const result: any = await googleSignIn(code, clientId, redirectURI) if (getWorkspace && result.userCred !== undefined) { - await client - .get(apiURLs.getUserRecords) - .then((d: any) => { + await client.get(apiURLs.getUserRecords).then(async (d: any) => { + if (!d.data.group) { + await registerUserForGoogle(result) + } else { const userDetails = { email: result.userCred.email, userId: result.userCred.userId } const workspaceDetails = { id: d.data.group, name: 'WORKSPACE_NAME' } setAuthenticated(userDetails, workspaceDetails) - }) - .catch(async (e) => { - setSensitiveData({ email: result.userCred.email, name: result.userCred.username, password: '', roles: [] }) - - const uCred: UserCred = { - email: result.userCred.email, - userId: result.userCred.userId, - expiry: result.userCred.exp, - token: result.userCred.token, - url: result.userCred.iss - } - const newWorkspaceName = `WD_${nanoid()}` - - await client - .post( - apiURLs.registerUser, - { - type: 'RegisterUserRequest', - user: { - id: uCred.userId, - name: uCred.email, - email: uCred.email - }, - workspaceName: newWorkspaceName - }, - { - headers: { - 'mex-workspace-id': '' - } - } - ) - .then(async (d: any) => { - const userDetails = { email: uCred.email, userId: uCred.userId } - const { registrationInfo, ilinks, nodes, snippets } = d.data - const workspaceDetails = { id: registrationInfo.id, name: registrationInfo.name } - - setILinks(ilinks) - initSnippets(snippets) - - const contents = {} - nodes.forEach((node) => { - contents[node.id] = { ...node, type: 'Node' } - }) - initContents(contents) - setAuthenticated(userDetails, workspaceDetails) - try { - await refreshToken() - } catch (error) {} // eslint-disable-line - }) - .catch(console.error) - }) + } + }) } return result } catch (error) { @@ -142,6 +94,57 @@ export const useAuthentication = () => { } } + async function registerUserForGoogle(result: any) { + setSensitiveData({ email: result.userCred.email, name: result.userCred.username, password: '', roles: [] }) + + const uCred: UserCred = { + email: result.userCred.email, + userId: result.userCred.userId, + expiry: result.userCred.expiry, + token: result.userCred.token, + url: result.userCred.url + } + const newWorkspaceName = `WD_${nanoid()}` + + await client + .post( + apiURLs.registerUser, + { + type: 'RegisterUserRequest', + user: { + id: uCred.userId, + name: uCred.email, + email: uCred.email + }, + workspaceName: newWorkspaceName + }, + { + headers: { + 'mex-workspace-id': '' + } + } + ) + .then(async (d: any) => { + const userDetails = { email: uCred.email, userId: uCred.userId } + const { registrationInfo, ilinks, nodes, snippets } = d.data + const workspaceDetails = { id: registrationInfo.id, name: registrationInfo.name } + + setILinks(ilinks) + initSnippets(snippets) + + const contents = {} + nodes.forEach((node) => { + contents[node.id] = { ...node, type: 'Node' } + }) + initContents(contents) + setAuthenticated(userDetails, workspaceDetails) + try { + await refreshToken() + } catch (error) {} // eslint-disable-line + }) + .catch(console.error) + } + const logout = async () => { await signOut() setUnAuthenticated()