最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

next.js - Problem setting up Mikro-orm with nextauth adapter - Stack Overflow

programmeradmin2浏览0评论

I'm trying to setup mikro-orm with nextauth follwing this guide on the nextauth official documentation, but i got typescript errors saying this:

Property 'email' has no initializer and is not definitely assigned in the constructor.ts(2564)
'Session' refers to a value, but is being used as a type here. Did you mean 'typeof Session'?ts(2749)
'Account' refers to a value, but is being used as a type here. Did you mean 'typeof Account'?ts(2749)

the first error says that I need to give email either default value or assigned it on the constructor wish is something i do not want to do forcibly.

the second and third are the same, even tho when I did what I got suggested wish is adding typeof. I got new error:

Property 'sessions' in type 'User' is not assignable to the same property in base type 'User'.
  Type 'Collection<typeof Session, object>' is not assignable to type 'Collection<Session, object>'.
    Types of property 'loadCount' are incompatible.
 ...

I got a really long error that I cannot show here.

this is my current implementation for now with what I typed I did:

import config from "@/config/mikro-orm.config"

import {
  Cascade,
  Collection,
  Entity,
  OneToMany,
  PrimaryKey,
  Property,
  Unique,
} from "@mikro-orm/core"
import { defaultEntities } from "@auth/mikro-orm-adapter"
import { randomUUID } from "crypto"
 
const type { Account, Session } = defaultEntities
 
@Entity()
export class User implements defaultEntities.User {
  @PrimaryKey()
  id: string = randomUUID()
 
  @Property({ nullable: true })
  name?: string
 
  @Property({ nullable: true })
  @Unique()
  email: string
 
  @Property({ type: "Date", nullable: true })
  emailVerified: Date | null = null
 
  @Property({ nullable: true })
  image?: string
 
  @OneToMany({
    entity: () => Session,
    mappedBy: (session) => session.user,
    hidden: true,
    orphanRemoval: true,
    cascade: [Cascade.ALL],
  })
  sessions = new Collection<typeof Session>(this)
 
  @OneToMany({
    entity: () => Account,
    mappedBy: (account) => account.user,
    hidden: true,
    orphanRemoval: true,
    cascade: [Cascade.ALL],
  })
  accounts = new Collection<typeof Account>(this)
 
  @Enum({ hidden: true })
  role = "ADMIN"
}
 
export const { handlers, auth, signIn, signOut } = NextAuth({
  adapter: MikroOrmAdapter(config, { entities: { User } }),
})

this is my config file if anyone asks:

import { Options } from '@mikro-orm/core';
import { defaultEntities } from '@auth/mikro-orm-adapter';

const config: Options = {
    entities: [...Object.values(defaultEntities)],
};

export default config;
发布评论

评论列表(0)

  1. 暂无评论