diff --git a/pods/authProviders/src/github.ts b/pods/authProviders/src/github.ts index 1266706c82..e431134cad 100644 --- a/pods/authProviders/src/github.ts +++ b/pods/authProviders/src/github.ts @@ -41,16 +41,18 @@ export function registerGithub ( redirectURL, passport.authenticate('github', { failureRedirect: concatLink(frontUrl, '/login'), session: true }), async (ctx, next) => { - const email = ctx.state.user.email ?? `github:${ctx.state.user.username}` + const email = ctx.state.user.emails?.[0]?.value ?? `github:${ctx.state.user.username}` const [first, last] = ctx.state.user.displayName.split(' ') if (email !== undefined) { if (ctx.query?.state != null) { - const loginInfo = await joinWithProvider(db, productId, email, first, last, ctx.query.state) + const loginInfo = await joinWithProvider(db, productId, email, first, last, ctx.query.state, { + githubId: ctx.state.user.id + }) if (ctx.session != null) { ctx.session.loginInfo = loginInfo } } else { - const loginInfo = await loginWithProvider(db, productId, email, first, last) + const loginInfo = await loginWithProvider(db, productId, email, first, last, { githubId: ctx.state.user.id }) if (ctx.session != null) { ctx.session.loginInfo = loginInfo } diff --git a/server/account/src/index.ts b/server/account/src/index.ts index 97c72d125f..160d43add0 100644 --- a/server/account/src/index.ts +++ b/server/account/src/index.ts @@ -164,6 +164,10 @@ export async function getAccount (db: Db, email: string): Promise({ email: cleanEmail(email) }) } +async function getAccountByQuery (db: Db, query: Record): Promise { + return await db.collection(ACCOUNT_COLLECTION).findOne(query) +} + /** * @public */ @@ -518,7 +522,8 @@ export async function createAcc ( password: string | null, first: string, last: string, - confirmed: boolean = false + confirmed: boolean = false, + extra?: Record ): Promise { const email = cleanEmail(_email) const salt = randomBytes(32) @@ -541,7 +546,8 @@ export async function createAcc ( first, last, confirmed, - workspaces: [] + workspaces: [], + ...(extra ?? {}) }) const newAccount = await getAccount(db, email) @@ -1499,12 +1505,16 @@ export async function joinWithProvider ( _email: string, first: string, last: string, - inviteId: ObjectId + inviteId: ObjectId, + extra?: Record ): Promise { const email = cleanEmail(_email) const invite = await getInvite(db, inviteId) const workspace = await checkInvite(invite, email) - const account = await getAccount(db, email) + let account = await getAccount(db, email) + if (account == null && extra !== undefined) { + account = await getAccountByQuery(db, extra) + } if (account !== null) { // we should clean password if account is not confirmed if (account.confirmed === false) { @@ -1530,7 +1540,7 @@ export async function joinWithProvider ( return result } - const newAccount = await createAcc(db, productId, email, null, first, last, true) + const newAccount = await createAcc(db, productId, email, null, first, last, true, extra) const token = generateToken(email, getWorkspaceId('', productId), getExtra(newAccount)) const ws = await assignWorkspace(db, productId, email, workspace.name, false) const result = await selectWorkspace(db, productId, token, ws.workspaceUrl ?? ws.workspace, false) @@ -1545,10 +1555,14 @@ export async function loginWithProvider ( productId: string, _email: string, first: string, - last: string + last: string, + extra?: Record ): Promise { const email = cleanEmail(_email) - const account = await getAccount(db, email) + let account = await getAccount(db, email) + if (account == null && extra !== undefined) { + account = await getAccountByQuery(db, extra) + } if (account !== null) { // we should clean password if account is not confirmed if (account.confirmed === false) { @@ -1562,7 +1576,7 @@ export async function loginWithProvider ( return result } - const newAccount = await createAcc(db, productId, email, null, first, last, true) + const newAccount = await createAcc(db, productId, email, null, first, last, true, extra) const result = { endpoint: getEndpoint(),