https://github.com/frectonz/fayda
Better Auth plugin for Fayda.
https://github.com/frectonz/fayda
better-auth fayda
Last synced: 5 months ago
JSON representation
Better Auth plugin for Fayda.
- Host: GitHub
- URL: https://github.com/frectonz/fayda
- Owner: frectonz
- License: mit
- Created: 2025-08-02T17:50:51.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-08-02T19:42:25.000Z (11 months ago)
- Last Synced: 2025-08-02T19:43:30.448Z (11 months ago)
- Topics: better-auth, fayda
- Language: TypeScript
- Homepage:
- Size: 50.8 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Better Auth Fayda Plugin
Better Auth plugin for Fayda.
## Installation
You can install the plugin using `pnpm`, `npm`, or `yarn`.
```
pnpm add fayda
```
## Auth Configuration
To use the plugin, you need to add it to your `better-auth` plugin list. Ensure you set the `accountLinking` options to link the user's Fayda identity with their local account.
The `clientId` and `privateKey` should be loaded from your environment variables.
```ts
import { fayda } from 'fayda';
import { betterAuth } from 'better-auth';
export const auth = betterAuth({
// ...
plugins: [
// ...
await fayda({
clientId: env.CLIENT_ID,
privateKey: env.PRIVATE_KEY,
})
],
account: {
accountLinking: {
enabled: true,
trustedProviders: ['fayda']
}
}
});
```
### Redirect Configuration
**IMPORTANT**: The `clientId` you receive from Fayda must have its `redirectURL` configured to `/api/auth/oauth2/callback/fayda` where `` is the address of your application like `https://example.com`.
If you are using a development `clientId` and need to use a different `callbackUrl` (e.g., `http://localhost:3000/callback`), you will need to set up a redirector. This redirector will accept the request and forward it to the correct better-auth callback handler.
Below is a `SvelteKit` example of how to implement this redirect. In this example, if your `callbackURL` is `http://localhost:3000/callback`, you would place this code in `src/routes/callback/+page.server.ts`.
```ts
import { redirect } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
export const load: PageServerLoad = async (req) => {
const originalUrl = new URL(req.url, `http://localhost:3000`);
const target = new URL('http://localhost:3000/api/auth/oauth2/callback/fayda');
target.search = originalUrl.search;
redirect(302, target);
};
```
## Auth Configuration
Next, you need to add the generic OAuth client plugin to your client-side authentication configuration. This will enable the client to initiate the OAuth flow with the Fayda provider.
```ts
import { createAuthClient } from 'better-auth/svelte';
import { genericOAuthClient } from 'better-auth/client/plugins';
export const authClient = createAuthClient({
baseURL: 'http://localhost:3000',
plugins: [genericOAuthClient()]
});
```
Once the client is configured, you can use the `signIn.oauth2` method to start the authentication flow.
```ts
authClient.signIn.oauth2({
providerId: 'fayda',
});
```