Save github id (#4856)

Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com>
This commit is contained in:
Denis Bykhov 2024-03-02 12:20:33 +06:00 committed by GitHub
parent a4f7c23cb2
commit c9be34aaa3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 11 deletions

View File

@ -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
}

View File

@ -164,6 +164,10 @@ export async function getAccount (db: Db, email: string): Promise<Account | null
return await db.collection(ACCOUNT_COLLECTION).findOne<Account>({ email: cleanEmail(email) })
}
async function getAccountByQuery (db: Db, query: Record<string, string>): Promise<Account | null> {
return await db.collection(ACCOUNT_COLLECTION).findOne<Account>(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<string, string>
): Promise<Account> {
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<string, string>
): Promise<WorkspaceLoginInfo | LoginInfo> {
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<string, string>
): Promise<LoginInfo> {
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(),