mirror of
https://github.com/LukeHagar/better-auth.git
synced 2025-12-10 04:19:32 +00:00
60 lines
1.7 KiB
Plaintext
60 lines
1.7 KiB
Plaintext
---
|
|
title: Core Concepts
|
|
description: core concepts of better-auth
|
|
---
|
|
|
|
## Auth Server
|
|
|
|
```ts
|
|
import { betterAuth } from "better-auth"
|
|
|
|
export const auth = betterAuth({
|
|
//...
|
|
})
|
|
const api = auth.api
|
|
const handler = auth.handler
|
|
```
|
|
|
|
The auth instance you create with betterAuth comes with two key properties:
|
|
|
|
**handler**: A web standard handler that you mount on your server to handle API requests. (see installation [here](/docs/installation#mount-handler))
|
|
|
|
**api**: A collection of methods you can call directly on the server to interact with the auth server, such as `getSession` to retrieve the current session.
|
|
|
|
**Example: Getting the current session on the server**
|
|
|
|
```ts
|
|
/**
|
|
* Consider this as a random route endpoint that we want to protect
|
|
*/
|
|
export const POST = (request: Request)=> {
|
|
const session = await auth.api.getSession({
|
|
headers: request.headers // get session requires the headers to be passed
|
|
})
|
|
}
|
|
```
|
|
## Client
|
|
|
|
The client side of the library lets you interact with the auth server and includes built-in state management for specific methods, such as useSession.
|
|
|
|
You can import the client and use it to call these methods directly or export each method individually from the client.
|
|
|
|
If you add new plugins, they may also introduce their own methods. For instance, using the twoFactor plugin will add methods like twoFactor.enable. Check out the example below to see how to use the client:
|
|
|
|
```tsx
|
|
const client = createAuthClient()
|
|
|
|
export const { signIn, signUp, signOut, useSession } = client
|
|
|
|
export function SignUp(){
|
|
async function handleSubmit(data){
|
|
await signUp.email({
|
|
name: data.name,
|
|
email: data.email,
|
|
password: data.password,
|
|
})
|
|
}
|
|
//...your component
|
|
}
|
|
```
|