fix: auth jwt

Signed-off-by: Innei <i@innei.in>
This commit is contained in:
Innei
2024-09-04 13:16:46 +08:00
parent 0327ab7c14
commit bba36c2d1a
6 changed files with 78 additions and 15 deletions

View File

@@ -35,6 +35,7 @@ export class AuthGuard implements CanActivate {
if (session) {
const isOwner = !!session.user.isOwner
if (isOwner) {
this.attachUserAndToken(
request,

View File

@@ -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()),
{

View File

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

View File

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

View File

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

View File

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