https://github.com/flaviodelgrosso/fastify-better-auth
Fastify plugin to simplify the integration of the Better Auth library
https://github.com/flaviodelgrosso/fastify-better-auth
authentication better-auth fastify fastify-plugin nodejs
Last synced: about 1 year ago
JSON representation
Fastify plugin to simplify the integration of the Better Auth library
- Host: GitHub
- URL: https://github.com/flaviodelgrosso/fastify-better-auth
- Owner: flaviodelgrosso
- License: isc
- Created: 2025-02-05T18:46:07.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2025-03-31T03:10:46.000Z (about 1 year ago)
- Last Synced: 2025-03-31T04:22:32.641Z (about 1 year ago)
- Topics: authentication, better-auth, fastify, fastify-plugin, nodejs
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/fastify-better-auth
- Size: 514 KB
- Stars: 13
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Fastify Better Auth
[](https://www.npmjs.com/package/fastify-better-auth)
[](https://www.npmjs.com/package/fastify-better-auth)
[](https://github.com/flaviodelgrosso/fastify-better-auth/actions/workflows/ci.yml)
[](https://codecov.io/gh/flaviodelgrosso/fastify-better-auth)
Fastify Better Auth is a Fastify plugin that simplifies the integration of the [Better Auth](https://www.better-auth.com) library into your Fastify applications. This plugin allows you to easily register authentication routes using Better Auth, providing a seamless authentication experience.
## Install
```bash
npm install fastify-better-auth
```
### Usage
```javascript
import { betterAuth } from 'better-auth';
import { drizzleAdapter } from 'better-auth/adapters/drizzle';
import FastifyBetterAuth from 'fastify-better-auth';
import fp from 'fastify-plugin';
export const auth = betterAuth({
trustedOrigins: [env.auth.URL],
database: drizzleAdapter(db, {
provider: 'pg',
usePlural: true,
}),
emailAndPassword: {
enabled: true,
},
});
async function authPlugin(fastify) {
await fastify.register(FastifyBetterAuth, { auth });
}
export default fp(authPlugin, {
name: 'auth-plugin',
});
```
You can now access the auth instance from the Fastify instance. For example if you want get the current session you can do:
```javascript
const session = await fastify.auth.api.getSession({
headers: fromNodeHeaders(req.headers),
});
```
## License
This project is licensed under the ISC License - see the [LICENSE](LICENSE) file for details.