better-auth-vercel-geolocation 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +72 -0
- package/dist/client.cjs +10 -0
- package/dist/client.d.cts +92 -0
- package/dist/client.d.cts.map +1 -0
- package/dist/client.d.ts +92 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +8 -0
- package/dist/index.cjs +71 -0
- package/dist/index.d.cts +88 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.ts +88 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +68 -0
- package/package.json +74 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 weepaho3
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# better-auth-vercel-geolocation
|
|
2
|
+
|
|
3
|
+
A [better-auth](https://www.better-auth.com/) plugin that enriches the auth session with Vercel edge geolocation data. On session create and refresh, it reads the [`x-vercel-ip-*` headers](https://vercel.com/docs/edge-network/headers#x-vercel-ip-city) via `@vercel/functions` server-side and writes the configured fields into the session record — no browser permission prompt, no extra API call.
|
|
4
|
+
|
|
5
|
+
> [!NOTE]
|
|
6
|
+
> Requires deployment on Vercel. In local development, geo fields will be `undefined`.
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- **Zero-config defaults** — stores city, country, countryRegion and flag out of the box
|
|
11
|
+
- **Configurable fields** — pick exactly which [geo fields](https://vercel.com/docs/edge-network/headers#x-vercel-ip-city) you need
|
|
12
|
+
- **Auto-refresh** — optionally updates geo data when the session refreshes
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install better-auth-vercel-geolocation
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Requires **better-auth** >= 1.4.18 as a peer dependency.
|
|
21
|
+
|
|
22
|
+
## Quick start
|
|
23
|
+
|
|
24
|
+
### 1. Add the plugin to your auth config
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import { betterAuth } from 'better-auth';
|
|
28
|
+
import { geolocation } from 'better-auth-plugin-vercel-geolocation';
|
|
29
|
+
|
|
30
|
+
export const auth = betterAuth({
|
|
31
|
+
plugins: [geolocation()],
|
|
32
|
+
});
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### 2. Migrate the database
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
npx @better-auth/cli migrate
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### 3. Add the client plugin
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
import { createAuthClient } from 'better-auth/react';
|
|
45
|
+
import { geolocationClient } from 'better-auth-plugin-vercel-geolocation/client';
|
|
46
|
+
|
|
47
|
+
export const authClient = createAuthClient({
|
|
48
|
+
plugins: [geolocationClient()],
|
|
49
|
+
});
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
The session now includes geolocation fields:
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
const { data: session } = authClient.useSession();
|
|
56
|
+
|
|
57
|
+
session.city; // "Hamburg"
|
|
58
|
+
session.country; // "HH"
|
|
59
|
+
session.countryRegion; // "BE"
|
|
60
|
+
session.flag; // "🇩🇪"
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Options
|
|
64
|
+
|
|
65
|
+
| Option | Type | Default | Description |
|
|
66
|
+
| ----------------- | ------------------------ | ---------------------------------------- | --------------------------------------------- |
|
|
67
|
+
| `fields` | `GeolocationFieldConfig` | `{ city, country, countryRegion, flag }` | Which fields to store on the session |
|
|
68
|
+
| `updateOnRefresh` | `boolean` | `true` | Update geo data when the session is refreshed |
|
|
69
|
+
|
|
70
|
+
## License
|
|
71
|
+
|
|
72
|
+
MIT
|
package/dist/client.cjs
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import * as better_auth from 'better-auth';
|
|
2
|
+
import { Geo } from '@vercel/functions';
|
|
3
|
+
|
|
4
|
+
type GeolocationField = keyof Geo;
|
|
5
|
+
type GeolocationFieldConfig = Partial<Record<GeolocationField, boolean>>;
|
|
6
|
+
declare const DEFAULT_FIELDS: {
|
|
7
|
+
readonly city: true;
|
|
8
|
+
readonly country: true;
|
|
9
|
+
readonly countryRegion: true;
|
|
10
|
+
readonly flag: true;
|
|
11
|
+
};
|
|
12
|
+
type DefaultGeolocationFields = typeof DEFAULT_FIELDS;
|
|
13
|
+
type GeoSchemaFields<T extends GeolocationFieldConfig> = {
|
|
14
|
+
[K in keyof T & GeolocationField as T[K] extends true ? K : never]: {
|
|
15
|
+
type: 'string';
|
|
16
|
+
required: false;
|
|
17
|
+
input: false;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
type InferGeoSession<T extends GeolocationFieldConfig> = {
|
|
21
|
+
[K in keyof T & GeolocationField as T[K] extends true ? K : never]?: string | null;
|
|
22
|
+
};
|
|
23
|
+
interface GeolocationPluginOptions<T extends GeolocationFieldConfig = GeolocationFieldConfig> {
|
|
24
|
+
/**
|
|
25
|
+
* Which geolocation fields to store on the session.
|
|
26
|
+
* @default { city: true, country: true, countryRegion: true, flag: true }
|
|
27
|
+
*/
|
|
28
|
+
fields?: T;
|
|
29
|
+
/**
|
|
30
|
+
* Whether to update session geolocation data when the session is refreshed.
|
|
31
|
+
* @default true
|
|
32
|
+
*/
|
|
33
|
+
updateOnRefresh?: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
declare const geolocation: <const T extends GeolocationFieldConfig = DefaultGeolocationFields>(options?: GeolocationPluginOptions<T>) => {
|
|
37
|
+
id: "vercel-geolocation";
|
|
38
|
+
schema: {
|
|
39
|
+
session: {
|
|
40
|
+
fields: GeoSchemaFields<T>;
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
$Infer: {
|
|
44
|
+
Session: {
|
|
45
|
+
session: InferGeoSession<T>;
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
init(): {
|
|
49
|
+
options: {
|
|
50
|
+
databaseHooks: {
|
|
51
|
+
session: {
|
|
52
|
+
create: {
|
|
53
|
+
before: (session: {
|
|
54
|
+
id: string;
|
|
55
|
+
createdAt: Date;
|
|
56
|
+
updatedAt: Date;
|
|
57
|
+
userId: string;
|
|
58
|
+
expiresAt: Date;
|
|
59
|
+
token: string;
|
|
60
|
+
ipAddress?: string | null | undefined;
|
|
61
|
+
userAgent?: string | null | undefined;
|
|
62
|
+
} & Record<string, unknown>, ctx: better_auth.GenericEndpointContext | null) => Promise<{
|
|
63
|
+
data: Record<string, unknown>;
|
|
64
|
+
} | undefined>;
|
|
65
|
+
};
|
|
66
|
+
update: {
|
|
67
|
+
before: (session: Partial<{
|
|
68
|
+
id: string;
|
|
69
|
+
createdAt: Date;
|
|
70
|
+
updatedAt: Date;
|
|
71
|
+
userId: string;
|
|
72
|
+
expiresAt: Date;
|
|
73
|
+
token: string;
|
|
74
|
+
ipAddress?: string | null | undefined;
|
|
75
|
+
userAgent?: string | null | undefined;
|
|
76
|
+
}> & Record<string, unknown>, ctx: better_auth.GenericEndpointContext | null) => Promise<{
|
|
77
|
+
data: Record<string, unknown>;
|
|
78
|
+
} | undefined>;
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
declare const geolocationClient: <const T extends GeolocationFieldConfig = DefaultGeolocationFields>() => {
|
|
87
|
+
id: "vercel-geolocation";
|
|
88
|
+
$InferServerPlugin: ReturnType<typeof geolocation<T>>;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export { geolocationClient };
|
|
92
|
+
//# sourceMappingURL=client.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.cts","sources":["../src/types.ts","../src/geolocation.ts","../src/client.ts"],"sourcesContent":["import type { Geo } from '@vercel/functions';\n\nexport type GeolocationField = keyof Geo;\n\nexport type GeolocationFieldConfig = Partial<Record<GeolocationField, boolean>>;\n\nexport const DEFAULT_FIELDS = {\n city: true,\n country: true,\n countryRegion: true,\n flag: true,\n} as const satisfies GeolocationFieldConfig;\n\nexport type DefaultGeolocationFields = typeof DEFAULT_FIELDS;\n\nexport type GeoSchemaFields<T extends GeolocationFieldConfig> = {\n [K in keyof T & GeolocationField as T[K] extends true ? K : never]: {\n type: 'string';\n required: false;\n input: false;\n };\n};\n\nexport type InferGeoSession<T extends GeolocationFieldConfig> = {\n [K in keyof T & GeolocationField as T[K] extends true\n ? K\n : never]?: string | null;\n};\n\nexport interface GeolocationPluginOptions<\n T extends GeolocationFieldConfig = GeolocationFieldConfig,\n> {\n /**\n * Which geolocation fields to store on the session.\n * @default { city: true, country: true, countryRegion: true, flag: true }\n */\n fields?: T;\n /**\n * Whether to update session geolocation data when the session is refreshed.\n * @default true\n */\n updateOnRefresh?: boolean;\n}\n","import type { BetterAuthPlugin } from 'better-auth';\n\nimport { geolocation as extractGeo } from '@vercel/functions';\n\nimport {\n DEFAULT_FIELDS,\n type DefaultGeolocationFields,\n type GeolocationField,\n type GeolocationFieldConfig,\n type GeolocationPluginOptions,\n type GeoSchemaFields,\n type InferGeoSession,\n} from './types';\n\nexport const geolocation = <\n const T extends GeolocationFieldConfig = DefaultGeolocationFields,\n>(\n options?: GeolocationPluginOptions<T>,\n) => {\n const updateOnRefresh = options?.updateOnRefresh ?? true;\n const fieldConfig = (options?.fields ?? DEFAULT_FIELDS) as T;\n\n const activeFields = (Object.keys(fieldConfig) as GeolocationField[]).filter(\n (field) => fieldConfig[field] === true,\n );\n\n const schemaFields = Object.fromEntries(\n activeFields.map((field) => [\n field,\n {\n type: 'string',\n required: false,\n input: false,\n },\n ]),\n ) as GeoSchemaFields<T>;\n\n function applyGeo(\n session: Record<string, unknown>,\n request: Request | undefined,\n ) {\n if (!request) return;\n\n const geo = extractGeo(request);\n for (const field of activeFields) {\n session[field] = geo[field];\n }\n return { data: session };\n }\n\n return {\n id: 'vercel-geolocation',\n schema: {\n session: { fields: schemaFields },\n },\n $Infer: {\n Session: {\n session: {} as InferGeoSession<T>,\n },\n },\n init() {\n return {\n options: {\n databaseHooks: {\n session: {\n create: {\n before: async (session, ctx) => {\n return applyGeo(session, ctx?.request);\n },\n },\n update: {\n before: async (session, ctx) => {\n if (!updateOnRefresh) return;\n return applyGeo(session, ctx?.request);\n },\n },\n },\n },\n },\n };\n },\n } satisfies BetterAuthPlugin;\n};\n","import type { BetterAuthClientPlugin } from 'better-auth/client';\n\nimport type { geolocation } from './geolocation';\nimport type {\n DefaultGeolocationFields,\n GeolocationFieldConfig,\n} from './types';\n\nexport const geolocationClient = <\n const T extends GeolocationFieldConfig = DefaultGeolocationFields,\n>() => {\n return {\n id: 'vercel-geolocation',\n $InferServerPlugin: {} as ReturnType<typeof geolocation<T>>,\n } satisfies BetterAuthClientPlugin;\n};\n"],"names":[],"mappings":";;;AACO,KAAA,gBAAA,SAAA,GAAA;AACA,KAAA,sBAAA,GAAA,OAAA,CAAA,MAAA,CAAA,gBAAA;AACA,cAAA,cAAA;AACP;AACA;AACA;AACA;AACA;AACO,KAAA,wBAAA,UAAA,cAAA;AACA,KAAA,eAAA,WAAA,sBAAA;AACP,oBAAA,gBAAA;AACA;AACA;AACA;AACA;AACA;AACO,KAAA,eAAA,WAAA,sBAAA;AACP,oBAAA,gBAAA;AACA;AACO,UAAA,wBAAA,WAAA,sBAAA,GAAA,sBAAA;AACP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AC9BO,cAAA,WAAA,mBAAA,sBAAA,GAAA,wBAAA,YAAA,wBAAA;AACP;AACA;AACA;AACA,oBAAA,eAAA;AACA;AACA;AACA;AACA;AACA,qBAAA,eAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,uCAAA,IAAA;AACA,uCAAA,IAAA;AACA;AACA,uCAAA,IAAA;AACA;AACA;AACA;AACA,4BAAA,MAAA,wBAA0D,WAAqB,CAAA,sBAAA,YAAA,OAAA;AAC/E,kCAAA,MAAA;AACA;AACA;AACA;AACA,0CAAA,OAAA;AACA;AACA,uCAAA,IAAA;AACA,uCAAA,IAAA;AACA;AACA,uCAAA,IAAA;AACA;AACA;AACA;AACA,6BAAA,MAAA,wBAA2D,WAAqB,CAAA,sBAAA,YAAA,OAAA;AAChF,kCAAA,MAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AC/CO,cAAA,iBAAA,mBAAA,sBAAA,GAAA,wBAAA;AACP;AACA,wBAAA,UAAA,QAAA,WAAA;AACA;;;;"}
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import * as better_auth from 'better-auth';
|
|
2
|
+
import { Geo } from '@vercel/functions';
|
|
3
|
+
|
|
4
|
+
type GeolocationField = keyof Geo;
|
|
5
|
+
type GeolocationFieldConfig = Partial<Record<GeolocationField, boolean>>;
|
|
6
|
+
declare const DEFAULT_FIELDS: {
|
|
7
|
+
readonly city: true;
|
|
8
|
+
readonly country: true;
|
|
9
|
+
readonly countryRegion: true;
|
|
10
|
+
readonly flag: true;
|
|
11
|
+
};
|
|
12
|
+
type DefaultGeolocationFields = typeof DEFAULT_FIELDS;
|
|
13
|
+
type GeoSchemaFields<T extends GeolocationFieldConfig> = {
|
|
14
|
+
[K in keyof T & GeolocationField as T[K] extends true ? K : never]: {
|
|
15
|
+
type: 'string';
|
|
16
|
+
required: false;
|
|
17
|
+
input: false;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
type InferGeoSession<T extends GeolocationFieldConfig> = {
|
|
21
|
+
[K in keyof T & GeolocationField as T[K] extends true ? K : never]?: string | null;
|
|
22
|
+
};
|
|
23
|
+
interface GeolocationPluginOptions<T extends GeolocationFieldConfig = GeolocationFieldConfig> {
|
|
24
|
+
/**
|
|
25
|
+
* Which geolocation fields to store on the session.
|
|
26
|
+
* @default { city: true, country: true, countryRegion: true, flag: true }
|
|
27
|
+
*/
|
|
28
|
+
fields?: T;
|
|
29
|
+
/**
|
|
30
|
+
* Whether to update session geolocation data when the session is refreshed.
|
|
31
|
+
* @default true
|
|
32
|
+
*/
|
|
33
|
+
updateOnRefresh?: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
declare const geolocation: <const T extends GeolocationFieldConfig = DefaultGeolocationFields>(options?: GeolocationPluginOptions<T>) => {
|
|
37
|
+
id: "vercel-geolocation";
|
|
38
|
+
schema: {
|
|
39
|
+
session: {
|
|
40
|
+
fields: GeoSchemaFields<T>;
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
$Infer: {
|
|
44
|
+
Session: {
|
|
45
|
+
session: InferGeoSession<T>;
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
init(): {
|
|
49
|
+
options: {
|
|
50
|
+
databaseHooks: {
|
|
51
|
+
session: {
|
|
52
|
+
create: {
|
|
53
|
+
before: (session: {
|
|
54
|
+
id: string;
|
|
55
|
+
createdAt: Date;
|
|
56
|
+
updatedAt: Date;
|
|
57
|
+
userId: string;
|
|
58
|
+
expiresAt: Date;
|
|
59
|
+
token: string;
|
|
60
|
+
ipAddress?: string | null | undefined;
|
|
61
|
+
userAgent?: string | null | undefined;
|
|
62
|
+
} & Record<string, unknown>, ctx: better_auth.GenericEndpointContext | null) => Promise<{
|
|
63
|
+
data: Record<string, unknown>;
|
|
64
|
+
} | undefined>;
|
|
65
|
+
};
|
|
66
|
+
update: {
|
|
67
|
+
before: (session: Partial<{
|
|
68
|
+
id: string;
|
|
69
|
+
createdAt: Date;
|
|
70
|
+
updatedAt: Date;
|
|
71
|
+
userId: string;
|
|
72
|
+
expiresAt: Date;
|
|
73
|
+
token: string;
|
|
74
|
+
ipAddress?: string | null | undefined;
|
|
75
|
+
userAgent?: string | null | undefined;
|
|
76
|
+
}> & Record<string, unknown>, ctx: better_auth.GenericEndpointContext | null) => Promise<{
|
|
77
|
+
data: Record<string, unknown>;
|
|
78
|
+
} | undefined>;
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
declare const geolocationClient: <const T extends GeolocationFieldConfig = DefaultGeolocationFields>() => {
|
|
87
|
+
id: "vercel-geolocation";
|
|
88
|
+
$InferServerPlugin: ReturnType<typeof geolocation<T>>;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export { geolocationClient };
|
|
92
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sources":["../src/types.ts","../src/geolocation.ts","../src/client.ts"],"sourcesContent":["import type { Geo } from '@vercel/functions';\n\nexport type GeolocationField = keyof Geo;\n\nexport type GeolocationFieldConfig = Partial<Record<GeolocationField, boolean>>;\n\nexport const DEFAULT_FIELDS = {\n city: true,\n country: true,\n countryRegion: true,\n flag: true,\n} as const satisfies GeolocationFieldConfig;\n\nexport type DefaultGeolocationFields = typeof DEFAULT_FIELDS;\n\nexport type GeoSchemaFields<T extends GeolocationFieldConfig> = {\n [K in keyof T & GeolocationField as T[K] extends true ? K : never]: {\n type: 'string';\n required: false;\n input: false;\n };\n};\n\nexport type InferGeoSession<T extends GeolocationFieldConfig> = {\n [K in keyof T & GeolocationField as T[K] extends true\n ? K\n : never]?: string | null;\n};\n\nexport interface GeolocationPluginOptions<\n T extends GeolocationFieldConfig = GeolocationFieldConfig,\n> {\n /**\n * Which geolocation fields to store on the session.\n * @default { city: true, country: true, countryRegion: true, flag: true }\n */\n fields?: T;\n /**\n * Whether to update session geolocation data when the session is refreshed.\n * @default true\n */\n updateOnRefresh?: boolean;\n}\n","import type { BetterAuthPlugin } from 'better-auth';\n\nimport { geolocation as extractGeo } from '@vercel/functions';\n\nimport {\n DEFAULT_FIELDS,\n type DefaultGeolocationFields,\n type GeolocationField,\n type GeolocationFieldConfig,\n type GeolocationPluginOptions,\n type GeoSchemaFields,\n type InferGeoSession,\n} from './types';\n\nexport const geolocation = <\n const T extends GeolocationFieldConfig = DefaultGeolocationFields,\n>(\n options?: GeolocationPluginOptions<T>,\n) => {\n const updateOnRefresh = options?.updateOnRefresh ?? true;\n const fieldConfig = (options?.fields ?? DEFAULT_FIELDS) as T;\n\n const activeFields = (Object.keys(fieldConfig) as GeolocationField[]).filter(\n (field) => fieldConfig[field] === true,\n );\n\n const schemaFields = Object.fromEntries(\n activeFields.map((field) => [\n field,\n {\n type: 'string',\n required: false,\n input: false,\n },\n ]),\n ) as GeoSchemaFields<T>;\n\n function applyGeo(\n session: Record<string, unknown>,\n request: Request | undefined,\n ) {\n if (!request) return;\n\n const geo = extractGeo(request);\n for (const field of activeFields) {\n session[field] = geo[field];\n }\n return { data: session };\n }\n\n return {\n id: 'vercel-geolocation',\n schema: {\n session: { fields: schemaFields },\n },\n $Infer: {\n Session: {\n session: {} as InferGeoSession<T>,\n },\n },\n init() {\n return {\n options: {\n databaseHooks: {\n session: {\n create: {\n before: async (session, ctx) => {\n return applyGeo(session, ctx?.request);\n },\n },\n update: {\n before: async (session, ctx) => {\n if (!updateOnRefresh) return;\n return applyGeo(session, ctx?.request);\n },\n },\n },\n },\n },\n };\n },\n } satisfies BetterAuthPlugin;\n};\n","import type { BetterAuthClientPlugin } from 'better-auth/client';\n\nimport type { geolocation } from './geolocation';\nimport type {\n DefaultGeolocationFields,\n GeolocationFieldConfig,\n} from './types';\n\nexport const geolocationClient = <\n const T extends GeolocationFieldConfig = DefaultGeolocationFields,\n>() => {\n return {\n id: 'vercel-geolocation',\n $InferServerPlugin: {} as ReturnType<typeof geolocation<T>>,\n } satisfies BetterAuthClientPlugin;\n};\n"],"names":[],"mappings":";;;AACO,KAAA,gBAAA,SAAA,GAAA;AACA,KAAA,sBAAA,GAAA,OAAA,CAAA,MAAA,CAAA,gBAAA;AACA,cAAA,cAAA;AACP;AACA;AACA;AACA;AACA;AACO,KAAA,wBAAA,UAAA,cAAA;AACA,KAAA,eAAA,WAAA,sBAAA;AACP,oBAAA,gBAAA;AACA;AACA;AACA;AACA;AACA;AACO,KAAA,eAAA,WAAA,sBAAA;AACP,oBAAA,gBAAA;AACA;AACO,UAAA,wBAAA,WAAA,sBAAA,GAAA,sBAAA;AACP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AC9BO,cAAA,WAAA,mBAAA,sBAAA,GAAA,wBAAA,YAAA,wBAAA;AACP;AACA;AACA;AACA,oBAAA,eAAA;AACA;AACA;AACA;AACA;AACA,qBAAA,eAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,uCAAA,IAAA;AACA,uCAAA,IAAA;AACA;AACA,uCAAA,IAAA;AACA;AACA;AACA;AACA,4BAAA,MAAA,wBAA0D,WAAqB,CAAA,sBAAA,YAAA,OAAA;AAC/E,kCAAA,MAAA;AACA;AACA;AACA;AACA,0CAAA,OAAA;AACA;AACA,uCAAA,IAAA;AACA,uCAAA,IAAA;AACA;AACA,uCAAA,IAAA;AACA;AACA;AACA;AACA,6BAAA,MAAA,wBAA2D,WAAqB,CAAA,sBAAA,YAAA,OAAA;AAChF,kCAAA,MAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AC/CO,cAAA,iBAAA,mBAAA,sBAAA,GAAA,wBAAA;AACP;AACA,wBAAA,UAAA,QAAA,WAAA;AACA;;;;"}
|
package/dist/client.js
ADDED
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
|
+
|
|
3
|
+
var functions = require('@vercel/functions');
|
|
4
|
+
|
|
5
|
+
const DEFAULT_FIELDS = {
|
|
6
|
+
city: true,
|
|
7
|
+
country: true,
|
|
8
|
+
countryRegion: true,
|
|
9
|
+
flag: true
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const geolocation = (options)=>{
|
|
13
|
+
const updateOnRefresh = options?.updateOnRefresh ?? true;
|
|
14
|
+
const fieldConfig = options?.fields ?? DEFAULT_FIELDS;
|
|
15
|
+
const activeFields = Object.keys(fieldConfig).filter((field)=>fieldConfig[field] === true);
|
|
16
|
+
const schemaFields = Object.fromEntries(activeFields.map((field)=>[
|
|
17
|
+
field,
|
|
18
|
+
{
|
|
19
|
+
type: 'string',
|
|
20
|
+
required: false,
|
|
21
|
+
input: false
|
|
22
|
+
}
|
|
23
|
+
]));
|
|
24
|
+
function applyGeo(session, request) {
|
|
25
|
+
if (!request) return;
|
|
26
|
+
const geo = functions.geolocation(request);
|
|
27
|
+
for (const field of activeFields){
|
|
28
|
+
session[field] = geo[field];
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
data: session
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
id: 'vercel-geolocation',
|
|
36
|
+
schema: {
|
|
37
|
+
session: {
|
|
38
|
+
fields: schemaFields
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
$Infer: {
|
|
42
|
+
Session: {
|
|
43
|
+
session: {}
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
init () {
|
|
47
|
+
return {
|
|
48
|
+
options: {
|
|
49
|
+
databaseHooks: {
|
|
50
|
+
session: {
|
|
51
|
+
create: {
|
|
52
|
+
before: async (session, ctx)=>{
|
|
53
|
+
return applyGeo(session, ctx?.request);
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
update: {
|
|
57
|
+
before: async (session, ctx)=>{
|
|
58
|
+
if (!updateOnRefresh) return;
|
|
59
|
+
return applyGeo(session, ctx?.request);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
exports.DEFAULT_FIELDS = DEFAULT_FIELDS;
|
|
71
|
+
exports.geolocation = geolocation;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import * as better_auth from 'better-auth';
|
|
2
|
+
import { Geo } from '@vercel/functions';
|
|
3
|
+
|
|
4
|
+
type GeolocationField = keyof Geo;
|
|
5
|
+
type GeolocationFieldConfig = Partial<Record<GeolocationField, boolean>>;
|
|
6
|
+
declare const DEFAULT_FIELDS: {
|
|
7
|
+
readonly city: true;
|
|
8
|
+
readonly country: true;
|
|
9
|
+
readonly countryRegion: true;
|
|
10
|
+
readonly flag: true;
|
|
11
|
+
};
|
|
12
|
+
type DefaultGeolocationFields = typeof DEFAULT_FIELDS;
|
|
13
|
+
type GeoSchemaFields<T extends GeolocationFieldConfig> = {
|
|
14
|
+
[K in keyof T & GeolocationField as T[K] extends true ? K : never]: {
|
|
15
|
+
type: 'string';
|
|
16
|
+
required: false;
|
|
17
|
+
input: false;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
type InferGeoSession<T extends GeolocationFieldConfig> = {
|
|
21
|
+
[K in keyof T & GeolocationField as T[K] extends true ? K : never]?: string | null;
|
|
22
|
+
};
|
|
23
|
+
interface GeolocationPluginOptions<T extends GeolocationFieldConfig = GeolocationFieldConfig> {
|
|
24
|
+
/**
|
|
25
|
+
* Which geolocation fields to store on the session.
|
|
26
|
+
* @default { city: true, country: true, countryRegion: true, flag: true }
|
|
27
|
+
*/
|
|
28
|
+
fields?: T;
|
|
29
|
+
/**
|
|
30
|
+
* Whether to update session geolocation data when the session is refreshed.
|
|
31
|
+
* @default true
|
|
32
|
+
*/
|
|
33
|
+
updateOnRefresh?: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
declare const geolocation: <const T extends GeolocationFieldConfig = DefaultGeolocationFields>(options?: GeolocationPluginOptions<T>) => {
|
|
37
|
+
id: "vercel-geolocation";
|
|
38
|
+
schema: {
|
|
39
|
+
session: {
|
|
40
|
+
fields: GeoSchemaFields<T>;
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
$Infer: {
|
|
44
|
+
Session: {
|
|
45
|
+
session: InferGeoSession<T>;
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
init(): {
|
|
49
|
+
options: {
|
|
50
|
+
databaseHooks: {
|
|
51
|
+
session: {
|
|
52
|
+
create: {
|
|
53
|
+
before: (session: {
|
|
54
|
+
id: string;
|
|
55
|
+
createdAt: Date;
|
|
56
|
+
updatedAt: Date;
|
|
57
|
+
userId: string;
|
|
58
|
+
expiresAt: Date;
|
|
59
|
+
token: string;
|
|
60
|
+
ipAddress?: string | null | undefined;
|
|
61
|
+
userAgent?: string | null | undefined;
|
|
62
|
+
} & Record<string, unknown>, ctx: better_auth.GenericEndpointContext | null) => Promise<{
|
|
63
|
+
data: Record<string, unknown>;
|
|
64
|
+
} | undefined>;
|
|
65
|
+
};
|
|
66
|
+
update: {
|
|
67
|
+
before: (session: Partial<{
|
|
68
|
+
id: string;
|
|
69
|
+
createdAt: Date;
|
|
70
|
+
updatedAt: Date;
|
|
71
|
+
userId: string;
|
|
72
|
+
expiresAt: Date;
|
|
73
|
+
token: string;
|
|
74
|
+
ipAddress?: string | null | undefined;
|
|
75
|
+
userAgent?: string | null | undefined;
|
|
76
|
+
}> & Record<string, unknown>, ctx: better_auth.GenericEndpointContext | null) => Promise<{
|
|
77
|
+
data: Record<string, unknown>;
|
|
78
|
+
} | undefined>;
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export { DEFAULT_FIELDS, geolocation };
|
|
87
|
+
export type { DefaultGeolocationFields, GeoSchemaFields, GeolocationField, GeolocationFieldConfig, GeolocationPluginOptions, InferGeoSession };
|
|
88
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","sources":["../src/types.ts","../src/geolocation.ts"],"sourcesContent":["import type { Geo } from '@vercel/functions';\n\nexport type GeolocationField = keyof Geo;\n\nexport type GeolocationFieldConfig = Partial<Record<GeolocationField, boolean>>;\n\nexport const DEFAULT_FIELDS = {\n city: true,\n country: true,\n countryRegion: true,\n flag: true,\n} as const satisfies GeolocationFieldConfig;\n\nexport type DefaultGeolocationFields = typeof DEFAULT_FIELDS;\n\nexport type GeoSchemaFields<T extends GeolocationFieldConfig> = {\n [K in keyof T & GeolocationField as T[K] extends true ? K : never]: {\n type: 'string';\n required: false;\n input: false;\n };\n};\n\nexport type InferGeoSession<T extends GeolocationFieldConfig> = {\n [K in keyof T & GeolocationField as T[K] extends true\n ? K\n : never]?: string | null;\n};\n\nexport interface GeolocationPluginOptions<\n T extends GeolocationFieldConfig = GeolocationFieldConfig,\n> {\n /**\n * Which geolocation fields to store on the session.\n * @default { city: true, country: true, countryRegion: true, flag: true }\n */\n fields?: T;\n /**\n * Whether to update session geolocation data when the session is refreshed.\n * @default true\n */\n updateOnRefresh?: boolean;\n}\n","import type { BetterAuthPlugin } from 'better-auth';\n\nimport { geolocation as extractGeo } from '@vercel/functions';\n\nimport {\n DEFAULT_FIELDS,\n type DefaultGeolocationFields,\n type GeolocationField,\n type GeolocationFieldConfig,\n type GeolocationPluginOptions,\n type GeoSchemaFields,\n type InferGeoSession,\n} from './types';\n\nexport const geolocation = <\n const T extends GeolocationFieldConfig = DefaultGeolocationFields,\n>(\n options?: GeolocationPluginOptions<T>,\n) => {\n const updateOnRefresh = options?.updateOnRefresh ?? true;\n const fieldConfig = (options?.fields ?? DEFAULT_FIELDS) as T;\n\n const activeFields = (Object.keys(fieldConfig) as GeolocationField[]).filter(\n (field) => fieldConfig[field] === true,\n );\n\n const schemaFields = Object.fromEntries(\n activeFields.map((field) => [\n field,\n {\n type: 'string',\n required: false,\n input: false,\n },\n ]),\n ) as GeoSchemaFields<T>;\n\n function applyGeo(\n session: Record<string, unknown>,\n request: Request | undefined,\n ) {\n if (!request) return;\n\n const geo = extractGeo(request);\n for (const field of activeFields) {\n session[field] = geo[field];\n }\n return { data: session };\n }\n\n return {\n id: 'vercel-geolocation',\n schema: {\n session: { fields: schemaFields },\n },\n $Infer: {\n Session: {\n session: {} as InferGeoSession<T>,\n },\n },\n init() {\n return {\n options: {\n databaseHooks: {\n session: {\n create: {\n before: async (session, ctx) => {\n return applyGeo(session, ctx?.request);\n },\n },\n update: {\n before: async (session, ctx) => {\n if (!updateOnRefresh) return;\n return applyGeo(session, ctx?.request);\n },\n },\n },\n },\n },\n };\n },\n } satisfies BetterAuthPlugin;\n};\n"],"names":[],"mappings":";;;AACO,KAAA,gBAAA,SAAA,GAAA;AACA,KAAA,sBAAA,GAAA,OAAA,CAAA,MAAA,CAAA,gBAAA;AACA,cAAA,cAAA;AACP;AACA;AACA;AACA;AACA;AACO,KAAA,wBAAA,UAAA,cAAA;AACA,KAAA,eAAA,WAAA,sBAAA;AACP,oBAAA,gBAAA;AACA;AACA;AACA;AACA;AACA;AACO,KAAA,eAAA,WAAA,sBAAA;AACP,oBAAA,gBAAA;AACA;AACO,UAAA,wBAAA,WAAA,sBAAA,GAAA,sBAAA;AACP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AC9BO,cAAA,WAAA,mBAAA,sBAAA,GAAA,wBAAA,YAAA,wBAAA;AACP;AACA;AACA;AACA,oBAAA,eAAA;AACA;AACA;AACA;AACA;AACA,qBAAA,eAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,uCAAA,IAAA;AACA,uCAAA,IAAA;AACA;AACA,uCAAA,IAAA;AACA;AACA;AACA;AACA,4BAAA,MAAA,wBAA0D,WAAqB,CAAA,sBAAA,YAAA,OAAA;AAC/E,kCAAA,MAAA;AACA;AACA;AACA;AACA,0CAAA,OAAA;AACA;AACA,uCAAA,IAAA;AACA,uCAAA,IAAA;AACA;AACA,uCAAA,IAAA;AACA;AACA;AACA;AACA,6BAAA,MAAA,wBAA2D,WAAqB,CAAA,sBAAA,YAAA,OAAA;AAChF,kCAAA,MAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;;"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import * as better_auth from 'better-auth';
|
|
2
|
+
import { Geo } from '@vercel/functions';
|
|
3
|
+
|
|
4
|
+
type GeolocationField = keyof Geo;
|
|
5
|
+
type GeolocationFieldConfig = Partial<Record<GeolocationField, boolean>>;
|
|
6
|
+
declare const DEFAULT_FIELDS: {
|
|
7
|
+
readonly city: true;
|
|
8
|
+
readonly country: true;
|
|
9
|
+
readonly countryRegion: true;
|
|
10
|
+
readonly flag: true;
|
|
11
|
+
};
|
|
12
|
+
type DefaultGeolocationFields = typeof DEFAULT_FIELDS;
|
|
13
|
+
type GeoSchemaFields<T extends GeolocationFieldConfig> = {
|
|
14
|
+
[K in keyof T & GeolocationField as T[K] extends true ? K : never]: {
|
|
15
|
+
type: 'string';
|
|
16
|
+
required: false;
|
|
17
|
+
input: false;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
type InferGeoSession<T extends GeolocationFieldConfig> = {
|
|
21
|
+
[K in keyof T & GeolocationField as T[K] extends true ? K : never]?: string | null;
|
|
22
|
+
};
|
|
23
|
+
interface GeolocationPluginOptions<T extends GeolocationFieldConfig = GeolocationFieldConfig> {
|
|
24
|
+
/**
|
|
25
|
+
* Which geolocation fields to store on the session.
|
|
26
|
+
* @default { city: true, country: true, countryRegion: true, flag: true }
|
|
27
|
+
*/
|
|
28
|
+
fields?: T;
|
|
29
|
+
/**
|
|
30
|
+
* Whether to update session geolocation data when the session is refreshed.
|
|
31
|
+
* @default true
|
|
32
|
+
*/
|
|
33
|
+
updateOnRefresh?: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
declare const geolocation: <const T extends GeolocationFieldConfig = DefaultGeolocationFields>(options?: GeolocationPluginOptions<T>) => {
|
|
37
|
+
id: "vercel-geolocation";
|
|
38
|
+
schema: {
|
|
39
|
+
session: {
|
|
40
|
+
fields: GeoSchemaFields<T>;
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
$Infer: {
|
|
44
|
+
Session: {
|
|
45
|
+
session: InferGeoSession<T>;
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
init(): {
|
|
49
|
+
options: {
|
|
50
|
+
databaseHooks: {
|
|
51
|
+
session: {
|
|
52
|
+
create: {
|
|
53
|
+
before: (session: {
|
|
54
|
+
id: string;
|
|
55
|
+
createdAt: Date;
|
|
56
|
+
updatedAt: Date;
|
|
57
|
+
userId: string;
|
|
58
|
+
expiresAt: Date;
|
|
59
|
+
token: string;
|
|
60
|
+
ipAddress?: string | null | undefined;
|
|
61
|
+
userAgent?: string | null | undefined;
|
|
62
|
+
} & Record<string, unknown>, ctx: better_auth.GenericEndpointContext | null) => Promise<{
|
|
63
|
+
data: Record<string, unknown>;
|
|
64
|
+
} | undefined>;
|
|
65
|
+
};
|
|
66
|
+
update: {
|
|
67
|
+
before: (session: Partial<{
|
|
68
|
+
id: string;
|
|
69
|
+
createdAt: Date;
|
|
70
|
+
updatedAt: Date;
|
|
71
|
+
userId: string;
|
|
72
|
+
expiresAt: Date;
|
|
73
|
+
token: string;
|
|
74
|
+
ipAddress?: string | null | undefined;
|
|
75
|
+
userAgent?: string | null | undefined;
|
|
76
|
+
}> & Record<string, unknown>, ctx: better_auth.GenericEndpointContext | null) => Promise<{
|
|
77
|
+
data: Record<string, unknown>;
|
|
78
|
+
} | undefined>;
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export { DEFAULT_FIELDS, geolocation };
|
|
87
|
+
export type { DefaultGeolocationFields, GeoSchemaFields, GeolocationField, GeolocationFieldConfig, GeolocationPluginOptions, InferGeoSession };
|
|
88
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sources":["../src/types.ts","../src/geolocation.ts"],"sourcesContent":["import type { Geo } from '@vercel/functions';\n\nexport type GeolocationField = keyof Geo;\n\nexport type GeolocationFieldConfig = Partial<Record<GeolocationField, boolean>>;\n\nexport const DEFAULT_FIELDS = {\n city: true,\n country: true,\n countryRegion: true,\n flag: true,\n} as const satisfies GeolocationFieldConfig;\n\nexport type DefaultGeolocationFields = typeof DEFAULT_FIELDS;\n\nexport type GeoSchemaFields<T extends GeolocationFieldConfig> = {\n [K in keyof T & GeolocationField as T[K] extends true ? K : never]: {\n type: 'string';\n required: false;\n input: false;\n };\n};\n\nexport type InferGeoSession<T extends GeolocationFieldConfig> = {\n [K in keyof T & GeolocationField as T[K] extends true\n ? K\n : never]?: string | null;\n};\n\nexport interface GeolocationPluginOptions<\n T extends GeolocationFieldConfig = GeolocationFieldConfig,\n> {\n /**\n * Which geolocation fields to store on the session.\n * @default { city: true, country: true, countryRegion: true, flag: true }\n */\n fields?: T;\n /**\n * Whether to update session geolocation data when the session is refreshed.\n * @default true\n */\n updateOnRefresh?: boolean;\n}\n","import type { BetterAuthPlugin } from 'better-auth';\n\nimport { geolocation as extractGeo } from '@vercel/functions';\n\nimport {\n DEFAULT_FIELDS,\n type DefaultGeolocationFields,\n type GeolocationField,\n type GeolocationFieldConfig,\n type GeolocationPluginOptions,\n type GeoSchemaFields,\n type InferGeoSession,\n} from './types';\n\nexport const geolocation = <\n const T extends GeolocationFieldConfig = DefaultGeolocationFields,\n>(\n options?: GeolocationPluginOptions<T>,\n) => {\n const updateOnRefresh = options?.updateOnRefresh ?? true;\n const fieldConfig = (options?.fields ?? DEFAULT_FIELDS) as T;\n\n const activeFields = (Object.keys(fieldConfig) as GeolocationField[]).filter(\n (field) => fieldConfig[field] === true,\n );\n\n const schemaFields = Object.fromEntries(\n activeFields.map((field) => [\n field,\n {\n type: 'string',\n required: false,\n input: false,\n },\n ]),\n ) as GeoSchemaFields<T>;\n\n function applyGeo(\n session: Record<string, unknown>,\n request: Request | undefined,\n ) {\n if (!request) return;\n\n const geo = extractGeo(request);\n for (const field of activeFields) {\n session[field] = geo[field];\n }\n return { data: session };\n }\n\n return {\n id: 'vercel-geolocation',\n schema: {\n session: { fields: schemaFields },\n },\n $Infer: {\n Session: {\n session: {} as InferGeoSession<T>,\n },\n },\n init() {\n return {\n options: {\n databaseHooks: {\n session: {\n create: {\n before: async (session, ctx) => {\n return applyGeo(session, ctx?.request);\n },\n },\n update: {\n before: async (session, ctx) => {\n if (!updateOnRefresh) return;\n return applyGeo(session, ctx?.request);\n },\n },\n },\n },\n },\n };\n },\n } satisfies BetterAuthPlugin;\n};\n"],"names":[],"mappings":";;;AACO,KAAA,gBAAA,SAAA,GAAA;AACA,KAAA,sBAAA,GAAA,OAAA,CAAA,MAAA,CAAA,gBAAA;AACA,cAAA,cAAA;AACP;AACA;AACA;AACA;AACA;AACO,KAAA,wBAAA,UAAA,cAAA;AACA,KAAA,eAAA,WAAA,sBAAA;AACP,oBAAA,gBAAA;AACA;AACA;AACA;AACA;AACA;AACO,KAAA,eAAA,WAAA,sBAAA;AACP,oBAAA,gBAAA;AACA;AACO,UAAA,wBAAA,WAAA,sBAAA,GAAA,sBAAA;AACP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AC9BO,cAAA,WAAA,mBAAA,sBAAA,GAAA,wBAAA,YAAA,wBAAA;AACP;AACA;AACA;AACA,oBAAA,eAAA;AACA;AACA;AACA;AACA;AACA,qBAAA,eAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,uCAAA,IAAA;AACA,uCAAA,IAAA;AACA;AACA,uCAAA,IAAA;AACA;AACA;AACA;AACA,4BAAA,MAAA,wBAA0D,WAAqB,CAAA,sBAAA,YAAA,OAAA;AAC/E,kCAAA,MAAA;AACA;AACA;AACA;AACA,0CAAA,OAAA;AACA;AACA,uCAAA,IAAA;AACA,uCAAA,IAAA;AACA;AACA,uCAAA,IAAA;AACA;AACA;AACA;AACA,6BAAA,MAAA,wBAA2D,WAAqB,CAAA,sBAAA,YAAA,OAAA;AAChF,kCAAA,MAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;;"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { geolocation as geolocation$1 } from '@vercel/functions';
|
|
2
|
+
|
|
3
|
+
const DEFAULT_FIELDS = {
|
|
4
|
+
city: true,
|
|
5
|
+
country: true,
|
|
6
|
+
countryRegion: true,
|
|
7
|
+
flag: true
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const geolocation = (options)=>{
|
|
11
|
+
const updateOnRefresh = options?.updateOnRefresh ?? true;
|
|
12
|
+
const fieldConfig = options?.fields ?? DEFAULT_FIELDS;
|
|
13
|
+
const activeFields = Object.keys(fieldConfig).filter((field)=>fieldConfig[field] === true);
|
|
14
|
+
const schemaFields = Object.fromEntries(activeFields.map((field)=>[
|
|
15
|
+
field,
|
|
16
|
+
{
|
|
17
|
+
type: 'string',
|
|
18
|
+
required: false,
|
|
19
|
+
input: false
|
|
20
|
+
}
|
|
21
|
+
]));
|
|
22
|
+
function applyGeo(session, request) {
|
|
23
|
+
if (!request) return;
|
|
24
|
+
const geo = geolocation$1(request);
|
|
25
|
+
for (const field of activeFields){
|
|
26
|
+
session[field] = geo[field];
|
|
27
|
+
}
|
|
28
|
+
return {
|
|
29
|
+
data: session
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
id: 'vercel-geolocation',
|
|
34
|
+
schema: {
|
|
35
|
+
session: {
|
|
36
|
+
fields: schemaFields
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
$Infer: {
|
|
40
|
+
Session: {
|
|
41
|
+
session: {}
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
init () {
|
|
45
|
+
return {
|
|
46
|
+
options: {
|
|
47
|
+
databaseHooks: {
|
|
48
|
+
session: {
|
|
49
|
+
create: {
|
|
50
|
+
before: async (session, ctx)=>{
|
|
51
|
+
return applyGeo(session, ctx?.request);
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
update: {
|
|
55
|
+
before: async (session, ctx)=>{
|
|
56
|
+
if (!updateOnRefresh) return;
|
|
57
|
+
return applyGeo(session, ctx?.request);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export { DEFAULT_FIELDS, geolocation };
|
package/package.json
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "better-auth-vercel-geolocation",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A better-auth plugin that enriches sessions with Vercel edge geolocation data.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"homepage": "https://github.com/weepaho3/better-auth-vercel-geolocation",
|
|
7
|
+
"bugs": {
|
|
8
|
+
"url": "https://github.com/weepaho3/better-auth-vercel-geolocation/issues"
|
|
9
|
+
},
|
|
10
|
+
"author": {
|
|
11
|
+
"name": "weepaho3",
|
|
12
|
+
"url": "https://github.com/weepaho3"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/weepaho3/better-auth-vercel-geolocation.git"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"better-auth",
|
|
20
|
+
"better-auth-plugin",
|
|
21
|
+
"vercel",
|
|
22
|
+
"geolocation",
|
|
23
|
+
"session",
|
|
24
|
+
"nextjs"
|
|
25
|
+
],
|
|
26
|
+
"files": [
|
|
27
|
+
"dist",
|
|
28
|
+
"README.md"
|
|
29
|
+
],
|
|
30
|
+
"type": "module",
|
|
31
|
+
"main": "./dist/index.cjs",
|
|
32
|
+
"module": "./dist/index.js",
|
|
33
|
+
"types": "./dist/index.d.ts",
|
|
34
|
+
"exports": {
|
|
35
|
+
".": {
|
|
36
|
+
"import": {
|
|
37
|
+
"types": "./dist/index.d.ts",
|
|
38
|
+
"default": "./dist/index.js"
|
|
39
|
+
},
|
|
40
|
+
"require": {
|
|
41
|
+
"types": "./dist/index.d.cts",
|
|
42
|
+
"default": "./dist/index.cjs"
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"./client": {
|
|
46
|
+
"import": {
|
|
47
|
+
"types": "./dist/client.d.ts",
|
|
48
|
+
"default": "./dist/client.js"
|
|
49
|
+
},
|
|
50
|
+
"require": {
|
|
51
|
+
"types": "./dist/client.d.cts",
|
|
52
|
+
"default": "./dist/client.cjs"
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
"publishConfig": {
|
|
57
|
+
"access": "public"
|
|
58
|
+
},
|
|
59
|
+
"scripts": {
|
|
60
|
+
"build": "bunchee",
|
|
61
|
+
"clean": "rm -rf dist",
|
|
62
|
+
"type-check": "tsc --noEmit"
|
|
63
|
+
},
|
|
64
|
+
"dependencies": {
|
|
65
|
+
"@vercel/functions": "^3.4.3"
|
|
66
|
+
},
|
|
67
|
+
"devDependencies": {
|
|
68
|
+
"bunchee": "^6.9.3",
|
|
69
|
+
"typescript": "^5.7.2"
|
|
70
|
+
},
|
|
71
|
+
"peerDependencies": {
|
|
72
|
+
"better-auth": "^1.4.18"
|
|
73
|
+
}
|
|
74
|
+
}
|