fix: merge oauth and google support

Signed-off-by: Innei <i@innei.in>
This commit is contained in:
Innei
2024-09-03 17:10:48 +08:00
parent 2205f9871e
commit 23f6acb1f0
3 changed files with 48 additions and 3 deletions

View File

@@ -1,7 +1,11 @@
import type { NestMiddleware, OnModuleInit } from '@nestjs/common'
import type { IncomingMessage, ServerResponse } from 'node:http'
import { AuthConfig, authjs } from '@mx-space/complied/auth'
import {
AuthConfig,
authjs,
BuiltInProviderType,
} from '@mx-space/complied/auth'
import { Inject } from '@nestjs/common'
import { EventBusEvents } from '~/constants/event-bus.constant'
@@ -34,7 +38,7 @@ export class AuthMiddleware implements NestMiddleware, OnModuleInit {
const providers = [] as AuthConfig['providers']
oauth.providers.forEach((provider) => {
if (!provider.enabled) return
const type = provider.type
const type = provider.type as BuiltInProviderType
const mergedConfig = {
...oauth.public[type],
@@ -60,6 +64,30 @@ export class AuthMiddleware implements NestMiddleware, OnModuleInit {
},
})
providers.push(provider)
break
}
case 'google': {
if (!mergedConfig.clientId || !mergedConfig.clientSecret) return
const provider = authjs.providers.google({
clientId: mergedConfig.clientId,
clientSecret: mergedConfig.clientSecret,
profile(profile) {
return {
id: profile.sub,
email: profile.email,
name: profile.name,
handle: profile.email,
image: profile.picture,
isOwner: false,
}
},
})
providers.push(provider)
break
}
}
})

View File

@@ -227,6 +227,22 @@ export class ConfigsService {
const value = instanceValue as OAuthDto
const current = await this.get('oauth')
const currentProvidersMap = current.providers.reduce(
(acc, item) => {
acc[item.type] = item
return acc
},
{} as Record<string, any>,
)
value.providers.forEach((p) => {
if (!currentProvidersMap[p.type]) {
current.providers.push(p)
} else {
currentProvidersMap[p.type] = p
}
})
let nextAuthSecrets = value.secrets
if (value.secrets) {
nextAuthSecrets = merge(current.secrets, nextAuthSecrets)
@@ -237,7 +253,7 @@ export class ConfigsService {
nextAuthPublic = merge(current.public, nextAuthPublic)
}
const option = await this.patch(key as 'oauth', {
providers: value.providers,
providers: current.providers,
secrets: nextAuthSecrets,
public: nextAuthPublic,
})

View File

@@ -24,3 +24,4 @@ export * from '@auth/core'
export type * from '@auth/core/adapters'
export { MongoDBAdapter } from '@auth/mongodb-adapter'
export type { BuiltInProviderType } from '@auth/core/providers/index'