mirror of
https://github.com/hcengineering/platform.git
synced 2025-05-30 04:05:39 +00:00
Save github id (#4856)
Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com>
This commit is contained in:
parent
a4f7c23cb2
commit
c9be34aaa3
@ -41,16 +41,18 @@ export function registerGithub (
|
|||||||
redirectURL,
|
redirectURL,
|
||||||
passport.authenticate('github', { failureRedirect: concatLink(frontUrl, '/login'), session: true }),
|
passport.authenticate('github', { failureRedirect: concatLink(frontUrl, '/login'), session: true }),
|
||||||
async (ctx, next) => {
|
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(' ')
|
const [first, last] = ctx.state.user.displayName.split(' ')
|
||||||
if (email !== undefined) {
|
if (email !== undefined) {
|
||||||
if (ctx.query?.state != null) {
|
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) {
|
if (ctx.session != null) {
|
||||||
ctx.session.loginInfo = loginInfo
|
ctx.session.loginInfo = loginInfo
|
||||||
}
|
}
|
||||||
} else {
|
} 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) {
|
if (ctx.session != null) {
|
||||||
ctx.session.loginInfo = loginInfo
|
ctx.session.loginInfo = loginInfo
|
||||||
}
|
}
|
||||||
|
@ -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) })
|
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
|
* @public
|
||||||
*/
|
*/
|
||||||
@ -518,7 +522,8 @@ export async function createAcc (
|
|||||||
password: string | null,
|
password: string | null,
|
||||||
first: string,
|
first: string,
|
||||||
last: string,
|
last: string,
|
||||||
confirmed: boolean = false
|
confirmed: boolean = false,
|
||||||
|
extra?: Record<string, string>
|
||||||
): Promise<Account> {
|
): Promise<Account> {
|
||||||
const email = cleanEmail(_email)
|
const email = cleanEmail(_email)
|
||||||
const salt = randomBytes(32)
|
const salt = randomBytes(32)
|
||||||
@ -541,7 +546,8 @@ export async function createAcc (
|
|||||||
first,
|
first,
|
||||||
last,
|
last,
|
||||||
confirmed,
|
confirmed,
|
||||||
workspaces: []
|
workspaces: [],
|
||||||
|
...(extra ?? {})
|
||||||
})
|
})
|
||||||
|
|
||||||
const newAccount = await getAccount(db, email)
|
const newAccount = await getAccount(db, email)
|
||||||
@ -1499,12 +1505,16 @@ export async function joinWithProvider (
|
|||||||
_email: string,
|
_email: string,
|
||||||
first: string,
|
first: string,
|
||||||
last: string,
|
last: string,
|
||||||
inviteId: ObjectId
|
inviteId: ObjectId,
|
||||||
|
extra?: Record<string, string>
|
||||||
): Promise<WorkspaceLoginInfo | LoginInfo> {
|
): Promise<WorkspaceLoginInfo | LoginInfo> {
|
||||||
const email = cleanEmail(_email)
|
const email = cleanEmail(_email)
|
||||||
const invite = await getInvite(db, inviteId)
|
const invite = await getInvite(db, inviteId)
|
||||||
const workspace = await checkInvite(invite, email)
|
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) {
|
if (account !== null) {
|
||||||
// we should clean password if account is not confirmed
|
// we should clean password if account is not confirmed
|
||||||
if (account.confirmed === false) {
|
if (account.confirmed === false) {
|
||||||
@ -1530,7 +1540,7 @@ export async function joinWithProvider (
|
|||||||
return result
|
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 token = generateToken(email, getWorkspaceId('', productId), getExtra(newAccount))
|
||||||
const ws = await assignWorkspace(db, productId, email, workspace.name, false)
|
const ws = await assignWorkspace(db, productId, email, workspace.name, false)
|
||||||
const result = await selectWorkspace(db, productId, token, ws.workspaceUrl ?? ws.workspace, false)
|
const result = await selectWorkspace(db, productId, token, ws.workspaceUrl ?? ws.workspace, false)
|
||||||
@ -1545,10 +1555,14 @@ export async function loginWithProvider (
|
|||||||
productId: string,
|
productId: string,
|
||||||
_email: string,
|
_email: string,
|
||||||
first: string,
|
first: string,
|
||||||
last: string
|
last: string,
|
||||||
|
extra?: Record<string, string>
|
||||||
): Promise<LoginInfo> {
|
): Promise<LoginInfo> {
|
||||||
const email = cleanEmail(_email)
|
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) {
|
if (account !== null) {
|
||||||
// we should clean password if account is not confirmed
|
// we should clean password if account is not confirmed
|
||||||
if (account.confirmed === false) {
|
if (account.confirmed === false) {
|
||||||
@ -1562,7 +1576,7 @@ export async function loginWithProvider (
|
|||||||
return result
|
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 = {
|
const result = {
|
||||||
endpoint: getEndpoint(),
|
endpoint: getEndpoint(),
|
||||||
|
Loading…
Reference in New Issue
Block a user