《feday2025-从 next-auth 到 better-auth.pdf》由会员分享,可在线阅读,更多相关《feday2025-从 next-auth 到 better-auth.pdf(19页珍藏版)》请在三个皮匠报告上搜索。
1、从 next-auth 到 better-auth包(Alex Yang)关于我himself65himseif65Better Auth Founding EngineerNode.js Member,Ex-TC39 MemberJotai,Waku Core MaintainerX S from Next Authimport NextAuth from next?authimport GithubProvider from next?auth/providers/githubexport const authOptions=providers:GithubProvider(clientI
2、d:process.env.GITHUB_ID,clientSecret:process.env.GITHUB_SECRET,),export default NextAuth(authOptions)Next Auth解决了户会话(user session)简单便对接到next.js数据库关持多种OAuth(Google、GitHub、Twitter)Why not Next Auth?Next.js 强绑定,Auth.js 今鸽了看不懂的档繁琐的定制化没有类型Better Auth提供多种的登陆式export const auth=betterAuth(database:new Pool(
3、connectionString:DATABASE_URL,),emailAndPassword:enabled:true,);Better Auth定义Schema,with TypeScript supportimport betterAuth from better?auth;import Database from better?sqlite3;export const auth=betterAuth(database:new Database(database.db),user:additionalFields:role:type:string,input:false,);type
4、Session=typeof auth.$Infer.Session;npx better?auth/clilatest generatenpx better?auth/clilatest migrateBetter Auth插件系统import betterAuth from better?auth;import organization from better?auth/plugins;export const auth=betterAuth(plugins:organization(),);Better AuthFramework AgnosticBuilt-In Rate Limite
5、r档Context 7档Chat with AI档MCP PluginAI?https:/modelcontextprotocol.io/speci?cation/draft/basic/authorizationMCP pluginBetter Call(tiny web framework)create endpointbuilt-in middlewaresschema validationimport createEndpoint from better?callimport z from zodexport const createItem=createEndpoint(/item,
6、method:POST,body:z.object(id:z.string(),async(ctx)?return item:id:ctx.body.id )Better FetchUniversal Fetch ClientHooksSchema Validationimport z from zod;?or your preferred Standard Schema compliant libraryconst data:todos,error:todoError =a