@@ -35,6 +35,7 @@ export class AuthGuard implements CanActivate {
|
||||
|
||||
if (session) {
|
||||
const isOwner = !!session.user.isOwner
|
||||
|
||||
if (isOwner) {
|
||||
this.attachUserAndToken(
|
||||
request,
|
||||
|
||||
@@ -16,14 +16,28 @@ import {
|
||||
export const authConfig: ServerAuthConfig = {
|
||||
basePath: isDev ? '/auth' : `/api/v${API_VERSION}/auth`,
|
||||
secret: SECURITY.jwtSecret || machineIdSync(),
|
||||
|
||||
callbacks: {
|
||||
redirect({ url }) {
|
||||
return url
|
||||
},
|
||||
async jwt({ token, account }) {
|
||||
if (account) {
|
||||
token = Object.assign({}, token, {
|
||||
provider: account.provider,
|
||||
providerAccountId: account.providerAccountId,
|
||||
})
|
||||
}
|
||||
return token
|
||||
},
|
||||
},
|
||||
trustHost: true,
|
||||
providers: [],
|
||||
|
||||
session: {
|
||||
strategy: 'jwt',
|
||||
},
|
||||
|
||||
adapter: MongoDBAdapter(
|
||||
getDatabaseConnection().then((c) => c.getClient()),
|
||||
{
|
||||
|
||||
@@ -115,7 +115,9 @@ export class AuthController {
|
||||
return null
|
||||
}
|
||||
|
||||
const account = await this.authService.getOauthUserAccount(session.userId)
|
||||
const account = await this.authService.getOauthUserAccount(
|
||||
session.providerAccountId,
|
||||
)
|
||||
|
||||
return {
|
||||
...session.user,
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
import type { AdapterSession, AdapterUser } from '@mx-space/complied/auth'
|
||||
|
||||
export type SessionUser = AdapterSession & { user: AdapterUser }
|
||||
export type SessionUser = AdapterSession & {
|
||||
user: AdapterUser
|
||||
provider: string
|
||||
providerAccountId: string
|
||||
}
|
||||
|
||||
declare module '@mx-space/complied/auth' {
|
||||
export interface AdapterUser {
|
||||
|
||||
@@ -168,14 +168,35 @@ export class AuthService {
|
||||
...authConfig,
|
||||
callbacks: {
|
||||
...authConfig.callbacks,
|
||||
async session(...args) {
|
||||
resolve(args[0].session as SessionUser)
|
||||
session: async (params) => {
|
||||
const token = params.token
|
||||
|
||||
let user = params.user ?? params.token
|
||||
if (typeof token?.providerAccountId === 'string') {
|
||||
const existUser = (await this.getOauthUserAccount(
|
||||
token.providerAccountId,
|
||||
)) as any
|
||||
|
||||
if (existUser) {
|
||||
user = existUser
|
||||
}
|
||||
}
|
||||
|
||||
resolve({
|
||||
...params.session,
|
||||
...params.user,
|
||||
user,
|
||||
provider: token.provider,
|
||||
providerAccountId: token.providerAccountId,
|
||||
} as SessionUser)
|
||||
|
||||
const session =
|
||||
(await authConfig.callbacks?.session?.(...args)) ??
|
||||
args[0].session
|
||||
const user = args[0].user ?? args[0].token
|
||||
return { user, ...session } satisfies Session
|
||||
(await authConfig.callbacks?.session?.(params)) ?? params.session
|
||||
|
||||
return {
|
||||
user,
|
||||
...session,
|
||||
} satisfies Session
|
||||
},
|
||||
},
|
||||
}).then((session) => {
|
||||
@@ -209,21 +230,43 @@ export class AuthService {
|
||||
return 'OK'
|
||||
}
|
||||
|
||||
getOauthUserAccount(userId: string) {
|
||||
return this.databaseService.db
|
||||
async getOauthUserAccount(providerAccountId: string) {
|
||||
const account = await this.databaseService.db
|
||||
.collection(AUTH_JS_ACCOUNT_COLLECTION)
|
||||
.findOne(
|
||||
{
|
||||
userId: new Types.ObjectId(userId),
|
||||
providerAccountId,
|
||||
},
|
||||
{
|
||||
projection: {
|
||||
_id: 0,
|
||||
userId: 0,
|
||||
access_token: 0,
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
if (account?.userId) {
|
||||
const user = await this.databaseService.db
|
||||
.collection(AUTH_JS_USER_COLLECTION)
|
||||
.findOne(
|
||||
{
|
||||
_id: account.userId,
|
||||
},
|
||||
{
|
||||
projection: {
|
||||
_id: 0,
|
||||
email: 1,
|
||||
name: 1,
|
||||
image: 1,
|
||||
isOwner: 1,
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
Object.assign(account, user)
|
||||
}
|
||||
|
||||
return account
|
||||
}
|
||||
getOauthProviders() {
|
||||
return this.authConfig.providers.map((p) => p.name)
|
||||
|
||||
@@ -29,9 +29,8 @@ class HTTPClient<
|
||||
private _adaptor: T,
|
||||
private options: Omit<ClientOptions, 'controllers'> = {},
|
||||
) {
|
||||
this._endpoint = _endpoint
|
||||
.replace(/\/*$/, '')
|
||||
.replace('localhost', '127.0.0.1')
|
||||
this._endpoint = _endpoint.replace(/\/*$/, '')
|
||||
|
||||
this._proxy = this.buildRoute(this)()
|
||||
options.transformResponse ||= (data) => camelcaseKeys(data)
|
||||
options.getDataFromResponse ||= (res: any) => res.data
|
||||
|
||||
Reference in New Issue
Block a user