@better-auth/passkey 1.4.0-beta.16
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.md +17 -0
- package/README.md +54 -0
- package/dist/client.d.ts +199 -0
- package/dist/client.js +145 -0
- package/dist/index-B_gZWjC3.d.ts +740 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +558 -0
- package/package.json +73 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
Copyright (c) 2024 - present, Bereket Engida
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
|
5
|
+
and associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
6
|
+
including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
|
7
|
+
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
|
|
8
|
+
is furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all copies or
|
|
11
|
+
substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
|
|
14
|
+
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
15
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
16
|
+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
17
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# Better Auth Passkey Plugin
|
|
2
|
+
|
|
3
|
+
## Installation
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
# Using npm
|
|
7
|
+
npm install better-auth @better-auth/passkey
|
|
8
|
+
|
|
9
|
+
# Using yarn
|
|
10
|
+
yarn add better-auth @better-auth/passkey
|
|
11
|
+
|
|
12
|
+
# Using pnpm
|
|
13
|
+
pnpm add better-auth @better-auth/passkey
|
|
14
|
+
|
|
15
|
+
# Using bun
|
|
16
|
+
bun add better-auth @better-auth/passkey
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
### Server
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
import { betterAuth } from "better-auth";
|
|
25
|
+
import { passkey } from "@better-auth/passkey";
|
|
26
|
+
|
|
27
|
+
export const auth = betterAuth({
|
|
28
|
+
plugins: [
|
|
29
|
+
passkey({
|
|
30
|
+
rpID: "example.com",
|
|
31
|
+
rpName: "My App",
|
|
32
|
+
}),
|
|
33
|
+
],
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Client
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import { createAuthClient } from "better-auth/client";
|
|
41
|
+
import { passkeyClient } from "@better-auth/passkey/client";
|
|
42
|
+
|
|
43
|
+
export const authClient = createAuthClient({
|
|
44
|
+
plugins: [passkeyClient()],
|
|
45
|
+
});
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Documentation
|
|
49
|
+
|
|
50
|
+
For more information, visit the [Better Auth Passkey documentation](https://better-auth.com/docs/plugins/passkey).
|
|
51
|
+
|
|
52
|
+
## License
|
|
53
|
+
|
|
54
|
+
MIT
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { n as Passkey, t as passkey } from "./index-B_gZWjC3.js";
|
|
2
|
+
import * as nanostores0 from "nanostores";
|
|
3
|
+
import { atom } from "nanostores";
|
|
4
|
+
import * as better_auth0 from "better-auth";
|
|
5
|
+
import * as _better_fetch_fetch0 from "@better-fetch/fetch";
|
|
6
|
+
import { BetterFetch, BetterFetchOption } from "@better-fetch/fetch";
|
|
7
|
+
import { ClientStore } from "@better-auth/core";
|
|
8
|
+
import { Session, User } from "better-auth/types";
|
|
9
|
+
|
|
10
|
+
//#region src/client.d.ts
|
|
11
|
+
declare const getPasskeyActions: ($fetch: BetterFetch, {
|
|
12
|
+
$listPasskeys,
|
|
13
|
+
$store
|
|
14
|
+
}: {
|
|
15
|
+
$listPasskeys: ReturnType<typeof atom<any>>;
|
|
16
|
+
$store: ClientStore;
|
|
17
|
+
}) => {
|
|
18
|
+
signIn: {
|
|
19
|
+
/**
|
|
20
|
+
* Sign in with a registered passkey
|
|
21
|
+
*/
|
|
22
|
+
passkey: (opts?: {
|
|
23
|
+
autoFill?: boolean;
|
|
24
|
+
fetchOptions?: BetterFetchOption;
|
|
25
|
+
} | undefined, options?: BetterFetchOption | undefined) => Promise<{
|
|
26
|
+
data: null;
|
|
27
|
+
error: {
|
|
28
|
+
message?: string | undefined;
|
|
29
|
+
status: number;
|
|
30
|
+
statusText: string;
|
|
31
|
+
};
|
|
32
|
+
} | {
|
|
33
|
+
data: {
|
|
34
|
+
session: Session;
|
|
35
|
+
user: User;
|
|
36
|
+
};
|
|
37
|
+
error: null;
|
|
38
|
+
} | {
|
|
39
|
+
data: null;
|
|
40
|
+
error: {
|
|
41
|
+
code: string;
|
|
42
|
+
message: string;
|
|
43
|
+
status: number;
|
|
44
|
+
statusText: string;
|
|
45
|
+
};
|
|
46
|
+
}>;
|
|
47
|
+
};
|
|
48
|
+
passkey: {
|
|
49
|
+
/**
|
|
50
|
+
* Add a passkey to the user account
|
|
51
|
+
*/
|
|
52
|
+
addPasskey: (opts?: {
|
|
53
|
+
fetchOptions?: BetterFetchOption;
|
|
54
|
+
/**
|
|
55
|
+
* The name of the passkey. This is used to
|
|
56
|
+
* identify the passkey in the UI.
|
|
57
|
+
*/
|
|
58
|
+
name?: string;
|
|
59
|
+
/**
|
|
60
|
+
* The type of attachment for the passkey. Defaults to both
|
|
61
|
+
* platform and cross-platform allowed, with platform preferred.
|
|
62
|
+
*/
|
|
63
|
+
authenticatorAttachment?: "platform" | "cross-platform";
|
|
64
|
+
/**
|
|
65
|
+
* Try to silently create a passkey with the password manager that the user just signed
|
|
66
|
+
* in with.
|
|
67
|
+
* @default false
|
|
68
|
+
*/
|
|
69
|
+
useAutoRegister?: boolean;
|
|
70
|
+
} | undefined, fetchOpts?: BetterFetchOption | undefined) => Promise<{
|
|
71
|
+
data: null;
|
|
72
|
+
error: {
|
|
73
|
+
message?: string | undefined;
|
|
74
|
+
status: number;
|
|
75
|
+
statusText: string;
|
|
76
|
+
};
|
|
77
|
+
} | {
|
|
78
|
+
data: null;
|
|
79
|
+
error: {
|
|
80
|
+
code: string;
|
|
81
|
+
message: string;
|
|
82
|
+
status: number;
|
|
83
|
+
statusText: string;
|
|
84
|
+
};
|
|
85
|
+
} | undefined>;
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* Inferred Internal Types
|
|
89
|
+
*/
|
|
90
|
+
$Infer: {
|
|
91
|
+
Passkey: Passkey;
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
declare const passkeyClient: () => {
|
|
95
|
+
id: "passkey";
|
|
96
|
+
$InferServerPlugin: ReturnType<typeof passkey>;
|
|
97
|
+
getActions: ($fetch: BetterFetch, $store: ClientStore) => {
|
|
98
|
+
signIn: {
|
|
99
|
+
/**
|
|
100
|
+
* Sign in with a registered passkey
|
|
101
|
+
*/
|
|
102
|
+
passkey: (opts?: {
|
|
103
|
+
autoFill?: boolean;
|
|
104
|
+
fetchOptions?: BetterFetchOption;
|
|
105
|
+
} | undefined, options?: BetterFetchOption | undefined) => Promise<{
|
|
106
|
+
data: null;
|
|
107
|
+
error: {
|
|
108
|
+
message?: string | undefined;
|
|
109
|
+
status: number;
|
|
110
|
+
statusText: string;
|
|
111
|
+
};
|
|
112
|
+
} | {
|
|
113
|
+
data: {
|
|
114
|
+
session: Session;
|
|
115
|
+
user: User;
|
|
116
|
+
};
|
|
117
|
+
error: null;
|
|
118
|
+
} | {
|
|
119
|
+
data: null;
|
|
120
|
+
error: {
|
|
121
|
+
code: string;
|
|
122
|
+
message: string;
|
|
123
|
+
status: number;
|
|
124
|
+
statusText: string;
|
|
125
|
+
};
|
|
126
|
+
}>;
|
|
127
|
+
};
|
|
128
|
+
passkey: {
|
|
129
|
+
/**
|
|
130
|
+
* Add a passkey to the user account
|
|
131
|
+
*/
|
|
132
|
+
addPasskey: (opts?: {
|
|
133
|
+
fetchOptions?: BetterFetchOption;
|
|
134
|
+
/**
|
|
135
|
+
* The name of the passkey. This is used to
|
|
136
|
+
* identify the passkey in the UI.
|
|
137
|
+
*/
|
|
138
|
+
name?: string;
|
|
139
|
+
/**
|
|
140
|
+
* The type of attachment for the passkey. Defaults to both
|
|
141
|
+
* platform and cross-platform allowed, with platform preferred.
|
|
142
|
+
*/
|
|
143
|
+
authenticatorAttachment?: "platform" | "cross-platform";
|
|
144
|
+
/**
|
|
145
|
+
* Try to silently create a passkey with the password manager that the user just signed
|
|
146
|
+
* in with.
|
|
147
|
+
* @default false
|
|
148
|
+
*/
|
|
149
|
+
useAutoRegister?: boolean;
|
|
150
|
+
} | undefined, fetchOpts?: BetterFetchOption | undefined) => Promise<{
|
|
151
|
+
data: null;
|
|
152
|
+
error: {
|
|
153
|
+
message?: string | undefined;
|
|
154
|
+
status: number;
|
|
155
|
+
statusText: string;
|
|
156
|
+
};
|
|
157
|
+
} | {
|
|
158
|
+
data: null;
|
|
159
|
+
error: {
|
|
160
|
+
code: string;
|
|
161
|
+
message: string;
|
|
162
|
+
status: number;
|
|
163
|
+
statusText: string;
|
|
164
|
+
};
|
|
165
|
+
} | undefined>;
|
|
166
|
+
};
|
|
167
|
+
/**
|
|
168
|
+
* Inferred Internal Types
|
|
169
|
+
*/
|
|
170
|
+
$Infer: {
|
|
171
|
+
Passkey: Passkey;
|
|
172
|
+
};
|
|
173
|
+
};
|
|
174
|
+
getAtoms($fetch: BetterFetch): {
|
|
175
|
+
listPasskeys: nanostores0.PreinitializedWritableAtom<{
|
|
176
|
+
data: Passkey[] | null;
|
|
177
|
+
error: null | _better_fetch_fetch0.BetterFetchError;
|
|
178
|
+
isPending: boolean;
|
|
179
|
+
isRefetching: boolean;
|
|
180
|
+
refetch: (queryParams?: {
|
|
181
|
+
query?: better_auth0.SessionQueryParams;
|
|
182
|
+
} | undefined) => void;
|
|
183
|
+
}> & object;
|
|
184
|
+
$listPasskeys: nanostores0.PreinitializedWritableAtom<any> & object;
|
|
185
|
+
};
|
|
186
|
+
pathMethods: {
|
|
187
|
+
"/passkey/register": "POST";
|
|
188
|
+
"/passkey/authenticate": "POST";
|
|
189
|
+
};
|
|
190
|
+
atomListeners: ({
|
|
191
|
+
matcher(path: string): path is "/passkey/verify-registration" | "/passkey/delete-passkey" | "/passkey/update-passkey" | "/sign-out";
|
|
192
|
+
signal: "$listPasskeys";
|
|
193
|
+
} | {
|
|
194
|
+
matcher: (path: string) => path is "/passkey/verify-authentication";
|
|
195
|
+
signal: "$sessionSignal";
|
|
196
|
+
})[];
|
|
197
|
+
};
|
|
198
|
+
//#endregion
|
|
199
|
+
export { getPasskeyActions, passkeyClient };
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { WebAuthnError, startAuthentication, startRegistration } from "@simplewebauthn/browser";
|
|
2
|
+
import { useAuthQuery } from "better-auth/client";
|
|
3
|
+
import { atom } from "nanostores";
|
|
4
|
+
|
|
5
|
+
//#region src/client.ts
|
|
6
|
+
const getPasskeyActions = ($fetch, { $listPasskeys, $store }) => {
|
|
7
|
+
const signInPasskey = async (opts, options) => {
|
|
8
|
+
const response = await $fetch("/passkey/generate-authenticate-options", {
|
|
9
|
+
method: "POST",
|
|
10
|
+
throw: false
|
|
11
|
+
});
|
|
12
|
+
if (!response.data) return response;
|
|
13
|
+
try {
|
|
14
|
+
const verified = await $fetch("/passkey/verify-authentication", {
|
|
15
|
+
body: { response: await startAuthentication({
|
|
16
|
+
optionsJSON: response.data,
|
|
17
|
+
useBrowserAutofill: opts?.autoFill
|
|
18
|
+
}) },
|
|
19
|
+
...opts?.fetchOptions,
|
|
20
|
+
...options,
|
|
21
|
+
method: "POST",
|
|
22
|
+
throw: false
|
|
23
|
+
});
|
|
24
|
+
$listPasskeys.set(Math.random());
|
|
25
|
+
$store.notify("$sessionSignal");
|
|
26
|
+
return verified;
|
|
27
|
+
} catch (e) {
|
|
28
|
+
return {
|
|
29
|
+
data: null,
|
|
30
|
+
error: {
|
|
31
|
+
code: "AUTH_CANCELLED",
|
|
32
|
+
message: "auth cancelled",
|
|
33
|
+
status: 400,
|
|
34
|
+
statusText: "BAD_REQUEST"
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
const registerPasskey = async (opts, fetchOpts) => {
|
|
40
|
+
const options = await $fetch("/passkey/generate-register-options", {
|
|
41
|
+
method: "GET",
|
|
42
|
+
query: {
|
|
43
|
+
...opts?.authenticatorAttachment && { authenticatorAttachment: opts.authenticatorAttachment },
|
|
44
|
+
...opts?.name && { name: opts.name }
|
|
45
|
+
},
|
|
46
|
+
throw: false
|
|
47
|
+
});
|
|
48
|
+
if (!options.data) return options;
|
|
49
|
+
try {
|
|
50
|
+
const res = await startRegistration({
|
|
51
|
+
optionsJSON: options.data,
|
|
52
|
+
useAutoRegister: opts?.useAutoRegister
|
|
53
|
+
});
|
|
54
|
+
const verified = await $fetch("/passkey/verify-registration", {
|
|
55
|
+
...opts?.fetchOptions,
|
|
56
|
+
...fetchOpts,
|
|
57
|
+
body: {
|
|
58
|
+
response: res,
|
|
59
|
+
name: opts?.name
|
|
60
|
+
},
|
|
61
|
+
method: "POST",
|
|
62
|
+
throw: false
|
|
63
|
+
});
|
|
64
|
+
if (!verified.data) return verified;
|
|
65
|
+
$listPasskeys.set(Math.random());
|
|
66
|
+
} catch (e) {
|
|
67
|
+
if (e instanceof WebAuthnError) {
|
|
68
|
+
if (e.code === "ERROR_AUTHENTICATOR_PREVIOUSLY_REGISTERED") return {
|
|
69
|
+
data: null,
|
|
70
|
+
error: {
|
|
71
|
+
code: e.code,
|
|
72
|
+
message: "previously registered",
|
|
73
|
+
status: 400,
|
|
74
|
+
statusText: "BAD_REQUEST"
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
if (e.code === "ERROR_CEREMONY_ABORTED") return {
|
|
78
|
+
data: null,
|
|
79
|
+
error: {
|
|
80
|
+
code: e.code,
|
|
81
|
+
message: "registration cancelled",
|
|
82
|
+
status: 400,
|
|
83
|
+
statusText: "BAD_REQUEST"
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
return {
|
|
87
|
+
data: null,
|
|
88
|
+
error: {
|
|
89
|
+
code: e.code,
|
|
90
|
+
message: e.message,
|
|
91
|
+
status: 400,
|
|
92
|
+
statusText: "BAD_REQUEST"
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
data: null,
|
|
98
|
+
error: {
|
|
99
|
+
code: "UNKNOWN_ERROR",
|
|
100
|
+
message: e instanceof Error ? e.message : "unknown error",
|
|
101
|
+
status: 500,
|
|
102
|
+
statusText: "INTERNAL_SERVER_ERROR"
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
return {
|
|
108
|
+
signIn: { passkey: signInPasskey },
|
|
109
|
+
passkey: { addPasskey: registerPasskey },
|
|
110
|
+
$Infer: {}
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
const passkeyClient = () => {
|
|
114
|
+
const $listPasskeys = atom();
|
|
115
|
+
return {
|
|
116
|
+
id: "passkey",
|
|
117
|
+
$InferServerPlugin: {},
|
|
118
|
+
getActions: ($fetch, $store) => getPasskeyActions($fetch, {
|
|
119
|
+
$listPasskeys,
|
|
120
|
+
$store
|
|
121
|
+
}),
|
|
122
|
+
getAtoms($fetch) {
|
|
123
|
+
return {
|
|
124
|
+
listPasskeys: useAuthQuery($listPasskeys, "/passkey/list-user-passkeys", $fetch, { method: "GET" }),
|
|
125
|
+
$listPasskeys
|
|
126
|
+
};
|
|
127
|
+
},
|
|
128
|
+
pathMethods: {
|
|
129
|
+
"/passkey/register": "POST",
|
|
130
|
+
"/passkey/authenticate": "POST"
|
|
131
|
+
},
|
|
132
|
+
atomListeners: [{
|
|
133
|
+
matcher(path) {
|
|
134
|
+
return path === "/passkey/verify-registration" || path === "/passkey/delete-passkey" || path === "/passkey/update-passkey" || path === "/sign-out";
|
|
135
|
+
},
|
|
136
|
+
signal: "$listPasskeys"
|
|
137
|
+
}, {
|
|
138
|
+
matcher: (path) => path === "/passkey/verify-authentication",
|
|
139
|
+
signal: "$sessionSignal"
|
|
140
|
+
}]
|
|
141
|
+
};
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
//#endregion
|
|
145
|
+
export { getPasskeyActions, passkeyClient };
|