Merge branch 'main' into v1.2

This commit is contained in:
Bereket Engida
2025-02-14 15:17:41 +03:00
32 changed files with 468 additions and 98 deletions

View File

@@ -223,6 +223,24 @@ const { data, error } = await authClient.resetPassword({
});
```
### Update password
<Callout type="warn">
This only works on server-side, and the following code may change over time.
</Callout>
To set a password, you must hash it first:
```ts
const ctx = await auth.$context;
const hash = await ctx.password.hash("your-new-password");
```
Then, to set the password:
```ts
await ctx.internalAdapter.updatePassword("userId", hash) //(you can also use your orm directly)
```
### Configuration
**Password**

View File

@@ -73,7 +73,7 @@ The `getSession` function retrieves the current active session.
```ts client="client.ts"
import { authClient } from "@/lib/client"
const session = await authClient.getSession()
const { data: session } = await authClient.getSession()
```
To learn how to customize the session response check the [Customizing Session Response](#customizing-session-response) section.
@@ -85,7 +85,7 @@ The `useSession` action provides a reactive way to access the current session.
```ts client="client.ts"
import { authClient } from "@/lib/client"
const session = await authClient.useSession()
const { data: session } = authClient.useSession()
```
### List Sessions

View File

@@ -37,7 +37,7 @@ import { cors } from "hono/cors";
const app = new Hono();
app.use(
"/api/auth/**", // or replace with "*" to enable cors for all routes
"/api/auth/*", // or replace with "*" to enable cors for all routes
cors({
origin: "http://localhost:3001", // replace with your origin
allowHeaders: ["Content-Type", "Authorization"],
@@ -82,7 +82,7 @@ app.use("*", async (c, next) => {
return next();
});
app.on(["POST", "GET"], "/api/auth/**", (c) => {
app.on(["POST", "GET"], "/api/auth/*", (c) => {
return auth.handler(c.req.raw);
});

View File

@@ -7,7 +7,7 @@ Better Auth is a framework-agnostic authentication and authorization framework f
## Why Better Auth?
*Authentication in the TypeScript ecosystem has long been a half-solved problem. Other open-source libraries often require a lot of additional code for anything beyond basic authentication features.Rather than just pushing third-party services as the solution, I believe we can do better as a community—hence, Better Auth*
*Authentication in the TypeScript ecosystem has long been a half-solved problem. Other open-source libraries often require a lot of additional code for anything beyond basic authentication features. Rather than just pushing third-party services as the solution, I believe we can do better as a community—hence, Better Auth*
## Features

View File

@@ -53,7 +53,7 @@ The Email OTP plugin allows user to sign-in, verify their email, or reset their
First, send an OTP to the user's email address.
```ts title="example.ts"
await authClient.emailOtp.sendVerificationOtp({
const { data, error } = await authClient.emailOtp.sendVerificationOtp({
email: "user-email@email.com",
type: "sign-in" // or "email-verification", "forget-password"
})
@@ -64,7 +64,7 @@ await authClient.emailOtp.sendVerificationOtp({
Once the user provides the OTP, you can sign in the user using the `signIn.emailOTP()` method.
```ts title="example.ts"
const user = await authClient.signIn.emailOtp({
const { data, error } = await authClient.signIn.emailOtp({
email: "user-email@email.com",
otp: "123456"
})
@@ -77,7 +77,7 @@ If the user is not registered, they'll be automatically registered. If you want
To verify the user's email address, use the `verifyEmail()` method.
```ts title="example.ts"
const user = await authClient.emailOtp.verifyEmail({
const { data, error } = await authClient.emailOtp.verifyEmail({
email: "user-email@email.com",
otp: "123456"
})
@@ -88,7 +88,7 @@ const user = await authClient.emailOtp.verifyEmail({
To reset the user's password, use the `resetPassword()` method.
```ts title="example.ts"
await authClient.emailOtp.resetPassword({
const { data, error } = await authClient.emailOtp.resetPassword({
email: "user-email@email.com",
otp: "123456",
password: "password"
@@ -146,4 +146,4 @@ export const auth = betterAuth({
- `sendVerificationOnSignUp`: A boolean value that determines whether to send the OTP when a user signs up. Defaults to `false`.
- `disableSignUp`: A boolean value that determines whether to prevent automatic sign-up when the user is not registered. Defaults to `false`.
- `disableSignUp`: A boolean value that determines whether to prevent automatic sign-up when the user is not registered. Defaults to `false`.

View File

@@ -70,6 +70,17 @@ const response = await authClient.signIn.oauth2({
});
```
### Linking OAuth Accounts
To link an OAuth account to an existing user:
```ts title="link-account.ts"
const response = await authClient.oauth2.link({
providerId: "provider-id",
callbackURL: "/dashboard" // the path to redirect to after the account is linked
});
```
### Handle OAuth Callback
The plugin mounts a route to handle the OAuth callback `/oauth2/callback/:providerId`. This means by default `${baseURL}/api/auth/oauth2/callback/:providerId` will be used as the callback URL. Make sure your OAuth provider is configured to use this URL.

View File

@@ -3,9 +3,10 @@ title: Magic link
description: Magic link plugin
---
Magic link or email link is a way to authenticate users without a password. When a user enters their email, a link is sent to their email. When the user clicks on the link, they are authenticated.
Magic link or email link is a way to authenticate users without a password. When a user enters their email, a link is sent to their email. When the user clicks on the link, they are authenticated.
## Installation
<Steps>
<Step>
### Add the server Plugin
@@ -43,18 +44,19 @@ Magic link or email link is a way to authenticate users without a password. When
});
```
</Step>
</Steps>
## Usage
### Sign In with Magic Link
To sign in with a magic link, you need to call `signIn.magicLink` with the user's email address. The `sendMagicLink` function is called to send the magic link to the user's email.
To sign in with a magic link, you need to call `signIn.magicLink` with the user's email address. The `sendMagicLink` function is called to send the magic link to the user's email.
```ts title="magic-link.ts"
const { data, error } = await authClient.signIn.magicLink({
email: "user@email.com",
callbackURL: "/dashboard" //redirect after successful login (optional)
email: "user@email.com",
callbackURL: "/dashboard", //redirect after successful login (optional)
});
```
@@ -65,16 +67,16 @@ If the user has not signed up, unless `disableSignUp` is set to `true`, the user
When you send the URL generated by the `sendMagicLink` function to a user, clicking the link will authenticate them and redirect them to the `callbackURL` specified in the `signIn.magicLink` function. If an error occurs, the user will be redirected to the `callbackURL` with an error query parameter.
<Callout type="warn">
If no `callbackURL` is provided, the user will be redirected to the root URL.
If no `callbackURL` is provided, the user will be redirected to the root URL.
</Callout>
If you want to handle the verification manually, (e.g, if you send the user a different url), you can use the `verify` function.
```ts title="magic-link.ts"
const { data, error } = await authClient.magicLink.verify({
query: {
token
}
query: {
token,
},
});
```
@@ -91,3 +93,13 @@ and a `request` object as the second parameter.
**expiresIn**: specifies the time in seconds after which the magic link will expire. The default value is `300` seconds (5 minutes).
**disableSignUp**: If set to `true`, the user will not be able to sign up using the magic link. The default value is `false`.
**generateToken**: The `generateToken` function is called to generate a token which is used to uniquely identify the user. The default value is a random string. There is one parameter:
- `email`: The email address of the user.
<Callout type="warn">
When using `generateToken`, ensure that the returned string is hard to guess
because it is used to verify who someone actually is in a confidential way. By
default, we return a long and cryptographically secure string.
</Callout>

View File

@@ -544,6 +544,16 @@ auth.api.addMember({
})
```
### Leave Organization
To leave organization you can use `organization.leave` function. This function will remove the current user from the organization.
```ts title="auth-client.ts"
await authClient.organization.leave({
organizationId: "organization-id"
})
```
## Access Control
The organization plugin providers a very flexible access control system. You can control the access of the user based on the role they have in the organization. You can define your own set of permissions based on the role of the user.

View File

@@ -85,7 +85,6 @@ const authClient = createAuthClient({
passkeyClient(), // [!code highlight]
], // [!code highlight]
});
// ---cut---
const data = await authClient.passkey.addPasskey();
```
@@ -111,7 +110,6 @@ const authClient = createAuthClient({
passkeyClient(), // [!code highlight]
], // [!code highlight]
});
// ---cut---
const data = await authClient.signIn.passkey();
```