Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Google login not providing workspace details #79

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 57 additions & 54 deletions apps/webapp/src/Stores/useAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,71 +77,74 @@ 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) {
console.log(error)
}
}

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()
Expand Down