mirror of
https://github.com/LukeHagar/better-auth.git
synced 2025-12-07 04:19:22 +00:00
docs: fix minor details
This commit is contained in:
@@ -86,6 +86,82 @@ export const auth = betterAuth({
|
||||
bearer(),
|
||||
admin(),
|
||||
],
|
||||
session: {
|
||||
additionalFields: {
|
||||
latitude: {
|
||||
type: "string",
|
||||
required: false,
|
||||
},
|
||||
longitude: {
|
||||
type: "string",
|
||||
required: false,
|
||||
},
|
||||
continent: {
|
||||
type: "string",
|
||||
required: false,
|
||||
},
|
||||
country: {
|
||||
type: "string",
|
||||
required: false,
|
||||
},
|
||||
region: {
|
||||
type: "string",
|
||||
required: false,
|
||||
},
|
||||
city: {
|
||||
type: "string",
|
||||
required: false,
|
||||
},
|
||||
timezone: {
|
||||
type: "string",
|
||||
required: false,
|
||||
},
|
||||
browserName: {
|
||||
type: "string",
|
||||
required: false,
|
||||
},
|
||||
browserVersion: {
|
||||
type: "string",
|
||||
required: false,
|
||||
},
|
||||
browserMajor: {
|
||||
type: "string",
|
||||
required: false,
|
||||
},
|
||||
engineName: {
|
||||
type: "string",
|
||||
required: false,
|
||||
},
|
||||
engineVersion: {
|
||||
type: "string",
|
||||
required: false,
|
||||
},
|
||||
osName: {
|
||||
type: "string",
|
||||
required: false,
|
||||
},
|
||||
osVersion: {
|
||||
type: "string",
|
||||
required: false,
|
||||
},
|
||||
deviceVendor: {
|
||||
type: "string",
|
||||
required: false,
|
||||
},
|
||||
deviceModel: {
|
||||
type: "string",
|
||||
required: false,
|
||||
},
|
||||
deviceType: {
|
||||
type: "string",
|
||||
required: false,
|
||||
},
|
||||
cpuArchitecture: {
|
||||
type: "string",
|
||||
required: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
socialProviders: {
|
||||
github: {
|
||||
clientId: process.env.GITHUB_CLIENT_ID || "",
|
||||
@@ -105,3 +181,5 @@ export const auth = betterAuth({
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
type A = typeof auth.$Infer.Session;
|
||||
|
||||
@@ -118,8 +118,7 @@ export async function GET(req: Request) {
|
||||
// this is used with the above example
|
||||
// const validParams = ogSchema.parse(ogData);
|
||||
const validParams = ogSchema.parse(urlParamsValues);
|
||||
console.log({ urlParamsValues });
|
||||
console.log("THE VALUD PARAMS: ", { validParams });
|
||||
|
||||
const { heading, type, mode } = validParams;
|
||||
const trueHeading =
|
||||
heading.length > 140 ? `${heading.substring(0, 140)}...` : heading;
|
||||
@@ -214,44 +213,18 @@ export async function GET(req: Request) {
|
||||
/>
|
||||
</svg>
|
||||
<svg
|
||||
width="110"
|
||||
height="110"
|
||||
viewBox="0 0 200 200"
|
||||
width="60"
|
||||
height="45"
|
||||
viewBox="0 0 60 45"
|
||||
fill="none"
|
||||
className="w-5 h-5"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
>
|
||||
<rect width="200" height="200" fill="#cacaca" />
|
||||
<line
|
||||
x1="21.5"
|
||||
y1="2.18557e-08"
|
||||
x2="21.5"
|
||||
y2="200"
|
||||
stroke="#bebebe"
|
||||
/>
|
||||
<line
|
||||
x1="173.5"
|
||||
y1="2.18557e-08"
|
||||
x2="173.5"
|
||||
y2="200"
|
||||
stroke="#bebebe"
|
||||
/>
|
||||
<line
|
||||
x1="200"
|
||||
y1="176.5"
|
||||
x2="-4.37114e-08"
|
||||
y2="176.5"
|
||||
stroke="#bebebe"
|
||||
/>
|
||||
<line
|
||||
x1="200"
|
||||
y1="24.5"
|
||||
x2="-4.37114e-08"
|
||||
y2="24.5"
|
||||
stroke="#bebebe"
|
||||
/>
|
||||
<path
|
||||
d="M64.4545 135V65.1818H88.8636C93.7273 65.1818 97.7386 66.0227 100.898 67.7045C104.057 69.3636 106.409 71.6023 107.955 74.4205C109.5 77.2159 110.273 80.3182 110.273 83.7273C110.273 86.7273 109.739 89.2045 108.67 91.1591C107.625 93.1136 106.239 94.6591 104.511 95.7955C102.807 96.9318 100.955 97.7727 98.9545 98.3182V99C101.091 99.1364 103.239 99.8864 105.398 101.25C107.557 102.614 109.364 104.568 110.818 107.114C112.273 109.659 113 112.773 113 116.455C113 119.955 112.205 123.102 110.614 125.898C109.023 128.693 106.511 130.909 103.08 132.545C99.6477 134.182 95.1818 135 89.6818 135H64.4545ZM72.9091 127.5H89.6818C95.2045 127.5 99.125 126.432 101.443 124.295C103.784 122.136 104.955 119.523 104.955 116.455C104.955 114.091 104.352 111.909 103.148 109.909C101.943 107.886 100.227 106.273 98 105.068C95.7727 103.841 93.1364 103.227 90.0909 103.227H72.9091V127.5ZM72.9091 95.8636H88.5909C91.1364 95.8636 93.4318 95.3636 95.4773 94.3636C97.5455 93.3636 99.1818 91.9545 100.386 90.1364C101.614 88.3182 102.227 86.1818 102.227 83.7273C102.227 80.6591 101.159 78.0568 99.0227 75.9205C96.8864 73.7614 93.5 72.6818 88.8636 72.6818H72.9091V95.8636ZM131.665 135.545C129.983 135.545 128.54 134.943 127.335 133.739C126.131 132.534 125.528 131.091 125.528 129.409C125.528 127.727 126.131 126.284 127.335 125.08C128.54 123.875 129.983 123.273 131.665 123.273C133.347 123.273 134.79 123.875 135.994 125.08C137.199 126.284 137.801 127.727 137.801 129.409C137.801 130.523 137.517 131.545 136.949 132.477C136.403 133.409 135.665 134.159 134.733 134.727C133.824 135.273 132.801 135.545 131.665 135.545Z"
|
||||
fill="#090602"
|
||||
fill-rule="evenodd"
|
||||
clip-rule="evenodd"
|
||||
d="M0 0H15V15H30V30H15V45H0V30V15V0ZM45 30V15H30V0H45H60V15V30V45H45H30V30H45Z"
|
||||
className="fill-black dark:fill-white"
|
||||
/>
|
||||
</svg>
|
||||
<div tw="flex flex-col flex-1 py-10">
|
||||
|
||||
@@ -27,8 +27,8 @@ import { betterAuth } from "better-auth"
|
||||
export const auth = betterAuth({
|
||||
//... other config options
|
||||
session: {
|
||||
expiresIn: 1000 * 60 * 60 * 24 * 7 // 7 days,
|
||||
updateAge: 1000 * 60 * 60 * 24 // 1 day (every 1 day the session expiration is updated)
|
||||
expiresIn: 60 * 60 * 24 * 7 // 7 days,
|
||||
updateAge: 60 * 60 * 24 // 1 day (every 1 day the session expiration is updated)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
@@ -84,7 +84,9 @@ import { inferAdditionalFields } from "better-auth/client/plugins";
|
||||
export const authClient = createAuthClient({
|
||||
plugins: [inferAdditionalFields({
|
||||
user: {
|
||||
role: "string"
|
||||
role: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
})],
|
||||
});
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
title: Svelte Kit Integration
|
||||
description: Learn how to integrate Better Auth with Svelte Kit
|
||||
---
|
||||
|
||||
Before you start, make sure you have a Better Auth instance configured. If you haven't done that yet, check out the [installation](/docs/installation).
|
||||
|
||||
### Mount the handler
|
||||
@@ -17,14 +18,6 @@ export async function handle({ event, resolve }) {
|
||||
}
|
||||
```
|
||||
|
||||
### Migrate the database
|
||||
Run the following command to create the necessary tables in your database:
|
||||
|
||||
```bash
|
||||
npx better-auth migrate
|
||||
```
|
||||
|
||||
|
||||
## Create a client
|
||||
|
||||
Create a client instance. You can name the file anything you want. Here we are creating `client.ts` file inside the `lib/` directory.
|
||||
@@ -32,7 +25,7 @@ Create a client instance. You can name the file anything you want. Here we are c
|
||||
```ts title="client.ts"
|
||||
import { createAuthClient } from "better-auth/svelte" // make sure to import from better-auth/svelte
|
||||
|
||||
export const client = createAuthClient({
|
||||
export const authClient = createAuthClient({
|
||||
//you can pass client configuration here
|
||||
})
|
||||
```
|
||||
@@ -43,18 +36,18 @@ Some of the actions are reactive. The client use [nano-store](https://github.com
|
||||
### Example usage
|
||||
```svelte
|
||||
<script lang="ts">
|
||||
import { client } from "$lib/client";
|
||||
const session = client.useSession();
|
||||
import { authClient } from "$lib/client";
|
||||
const session = authClient.useSession();
|
||||
</script>
|
||||
<div>
|
||||
{#if $session}
|
||||
{#if $session.data}
|
||||
<div>
|
||||
<p>
|
||||
{$session?.data?.user.name}
|
||||
</p>
|
||||
<button
|
||||
on:click={async () => {
|
||||
await client.signOut();
|
||||
await authClient.signOut();
|
||||
}}
|
||||
>
|
||||
Signout
|
||||
@@ -63,7 +56,7 @@ Some of the actions are reactive. The client use [nano-store](https://github.com
|
||||
{:else}
|
||||
<button
|
||||
on:click={async () => {
|
||||
await client.signIn.social({
|
||||
await authClient.signIn.social({
|
||||
provider: "github",
|
||||
});
|
||||
}}
|
||||
|
||||
@@ -7,7 +7,7 @@ describe("session", async () => {
|
||||
const { client, testUser, sessionSetter } = await getTestInstance();
|
||||
|
||||
it("should set cookies correctly on sign in", async () => {
|
||||
await client.signIn.email(
|
||||
const res = await client.signIn.email(
|
||||
{
|
||||
email: testUser.email,
|
||||
password: testUser.password,
|
||||
@@ -26,6 +26,9 @@ describe("session", async () => {
|
||||
},
|
||||
},
|
||||
);
|
||||
const expiresAt = new Date(res.data?.session?.expiresAt || "");
|
||||
const now = new Date();
|
||||
expect(expiresAt.getDate()).toBeGreaterThan(now.getDate() + 6);
|
||||
});
|
||||
|
||||
it("should return null when not authenticated", async () => {
|
||||
|
||||
@@ -49,6 +49,7 @@ function getPathAliases(cwd: string): Record<string, string> | null {
|
||||
}
|
||||
return result;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
throw new BetterAuthError("Error parsing tsconfig.json");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,44 +123,60 @@ export const createInternalAdapter = (
|
||||
* The cookie will be set to expire at the end of the session
|
||||
*/
|
||||
expiresAt: dontRememberMe
|
||||
? getDate(1000 * 60 * 60 * 24) // 1 day
|
||||
? getDate(60 * 60 * 24, "sec") // 1 day
|
||||
: getDate(sessionExpiration, "sec"),
|
||||
ipAddress: headers?.get("x-forwarded-for") || "",
|
||||
userAgent: headers?.get("user-agent") || "",
|
||||
};
|
||||
const session = await createWithHooks(data, "session");
|
||||
if (secondaryStorage && session) {
|
||||
const user = await adapter.findOne<User>({
|
||||
model: tables.user.tableName,
|
||||
where: [{ field: "id", value: userId }],
|
||||
});
|
||||
secondaryStorage.set(
|
||||
session.id,
|
||||
JSON.stringify(session),
|
||||
JSON.stringify({
|
||||
session,
|
||||
user,
|
||||
}),
|
||||
sessionExpiration,
|
||||
);
|
||||
}
|
||||
return session;
|
||||
},
|
||||
findSession: async (sessionId: string) => {
|
||||
let session: Session | null = null;
|
||||
if (secondaryStorage) {
|
||||
const sessionStringified = await secondaryStorage.get(sessionId);
|
||||
if (sessionStringified) {
|
||||
const s = JSON.parse(sessionStringified);
|
||||
session = {
|
||||
...s,
|
||||
expiresAt: new Date(s.expiresAt),
|
||||
return {
|
||||
session: {
|
||||
...s.session,
|
||||
expiresAt: new Date(s.session.expiresAt),
|
||||
},
|
||||
user: {
|
||||
...s.user,
|
||||
createdAt: new Date(s.user.createdAt),
|
||||
updatedAt: new Date(s.user.updatedAt),
|
||||
},
|
||||
} as {
|
||||
session: Session;
|
||||
user: User;
|
||||
};
|
||||
}
|
||||
} else {
|
||||
session = await adapter.findOne<Session>({
|
||||
model: tables.session.tableName,
|
||||
where: [
|
||||
{
|
||||
value: sessionId,
|
||||
field: "id",
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
const session = await adapter.findOne<Session>({
|
||||
model: tables.session.tableName,
|
||||
where: [
|
||||
{
|
||||
value: sessionId,
|
||||
field: "id",
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
if (!session) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
export const getDate = (span: number, unit: "sec" | "ms" = "ms") => {
|
||||
const date = new Date();
|
||||
return new Date(date.getTime() + (unit === "sec" ? span * 1000 : span));
|
||||
return new Date(Date.now() + (unit === "sec" ? span * 1000 : span));
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user