@bounc.ing/next 0.2.1 → 0.3.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.
@@ -0,0 +1,8 @@
1
+ import React from 'react';
2
+ interface PasskeyButtonProps {
3
+ mode: 'register' | 'login';
4
+ className?: string;
5
+ children?: React.ReactNode;
6
+ }
7
+ export declare function PasskeyButton({ mode, className, children }: PasskeyButtonProps): import("react/jsx-runtime").JSX.Element;
8
+ export {};
@@ -0,0 +1,19 @@
1
+ 'use client';
2
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import { useRegisterPasskey, useLoginWithPasskey } from './use-passkey.js';
4
+ export function PasskeyButton({ mode, className, children }) {
5
+ if (mode === 'register') {
6
+ return _jsx(RegisterPasskeyButton, { className: className, children: children });
7
+ }
8
+ return _jsx(LoginPasskeyButton, { className: className, children: children });
9
+ }
10
+ function RegisterPasskeyButton({ className, children }) {
11
+ const { register, isRegistering, error, registered } = useRegisterPasskey();
12
+ if (registered)
13
+ return _jsx("span", { className: className, children: "Passkey added \u2713" });
14
+ return (_jsxs(_Fragment, { children: [_jsx("button", { type: "button", className: className, onClick: () => register(), disabled: isRegistering, children: children ?? (isRegistering ? 'Adding...' : 'Add a passkey') }), error && _jsx("p", { style: { color: 'red', fontSize: '0.85em' }, children: error.message })] }));
15
+ }
16
+ function LoginPasskeyButton({ className, children }) {
17
+ const { login, isLoggingIn, error } = useLoginWithPasskey();
18
+ return (_jsxs(_Fragment, { children: [_jsx("button", { type: "button", className: className, onClick: () => login(), disabled: isLoggingIn, children: children ?? (isLoggingIn ? 'Signing in...' : 'Sign in with a passkey') }), error && _jsx("p", { style: { color: 'red', fontSize: '0.85em' }, children: error.message })] }));
19
+ }
@@ -2,16 +2,21 @@
2
2
  import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
3
3
  import { useState, useEffect } from 'react';
4
4
  import { useBouncingContext } from './provider.js';
5
+ import { PasskeyButton } from './passkey.js';
5
6
  export function SignIn({ className }) {
6
7
  const { domain } = useBouncingContext();
7
8
  const [providers, setProviders] = useState([]);
9
+ const [passkeysEnabled, setPasskeysEnabled] = useState(false);
8
10
  useEffect(() => {
9
11
  fetch(`https://${domain}/auth/providers`)
10
12
  .then((res) => res.json())
11
- .then((data) => setProviders(data.providers))
13
+ .then((data) => {
14
+ setProviders(data.providers);
15
+ setPasskeysEnabled(!!data.passkeys);
16
+ })
12
17
  .catch(() => { });
13
18
  }, [domain]);
14
- if (providers.length === 0)
19
+ if (providers.length === 0 && !passkeysEnabled)
15
20
  return null;
16
- return (_jsx("div", { className: className, children: providers.map((provider) => (_jsxs("a", { href: `https://${domain}/auth/oauth/${provider}`, style: { display: 'inline-block', margin: '0.25rem' }, children: ["Sign in with ", provider.charAt(0).toUpperCase() + provider.slice(1)] }, provider))) }));
21
+ return (_jsxs("div", { className: className, children: [providers.map((provider) => (_jsxs("a", { href: `https://${domain}/auth/oauth/${provider}`, style: { display: 'inline-block', margin: '0.25rem' }, children: ["Sign in with ", provider.charAt(0).toUpperCase() + provider.slice(1)] }, provider))), passkeysEnabled && (_jsx("div", { style: { margin: '0.25rem' }, children: _jsx(PasskeyButton, { mode: "login" }) }))] }));
17
22
  }
@@ -0,0 +1,14 @@
1
+ interface RegisterResult {
2
+ register: (deviceName?: string) => Promise<void>;
3
+ isRegistering: boolean;
4
+ error: Error | null;
5
+ registered: boolean;
6
+ }
7
+ export declare function useRegisterPasskey(): RegisterResult;
8
+ interface LoginResult {
9
+ login: () => Promise<void>;
10
+ isLoggingIn: boolean;
11
+ error: Error | null;
12
+ }
13
+ export declare function useLoginWithPasskey(): LoginResult;
14
+ export {};
@@ -0,0 +1,76 @@
1
+ 'use client';
2
+ import { useState, useCallback } from 'react';
3
+ import { startRegistration, startAuthentication } from '@simplewebauthn/browser';
4
+ import { useBouncingContext } from './provider.js';
5
+ export function useRegisterPasskey() {
6
+ const { domain } = useBouncingContext();
7
+ const [isRegistering, setIsRegistering] = useState(false);
8
+ const [error, setError] = useState(null);
9
+ const [registered, setRegistered] = useState(false);
10
+ const register = useCallback(async (deviceName) => {
11
+ setIsRegistering(true);
12
+ setError(null);
13
+ try {
14
+ const beginRes = await fetch(`https://${domain}/auth/webauthn/register/begin`, {
15
+ method: 'POST',
16
+ credentials: 'include',
17
+ });
18
+ if (!beginRes.ok)
19
+ throw new Error(`begin failed: ${beginRes.status}`);
20
+ const options = await beginRes.json();
21
+ const response = await startRegistration({ optionsJSON: options });
22
+ const finishRes = await fetch(`https://${domain}/auth/webauthn/register/finish`, {
23
+ method: 'POST',
24
+ credentials: 'include',
25
+ headers: { 'Content-Type': 'application/json' },
26
+ body: JSON.stringify({ response, deviceName }),
27
+ });
28
+ if (!finishRes.ok)
29
+ throw new Error(`finish failed: ${finishRes.status}`);
30
+ setRegistered(true);
31
+ }
32
+ catch (e) {
33
+ setError(e instanceof Error ? e : new Error(String(e)));
34
+ }
35
+ finally {
36
+ setIsRegistering(false);
37
+ }
38
+ }, [domain]);
39
+ return { register, isRegistering, error, registered };
40
+ }
41
+ export function useLoginWithPasskey() {
42
+ const { domain } = useBouncingContext();
43
+ const [isLoggingIn, setIsLoggingIn] = useState(false);
44
+ const [error, setError] = useState(null);
45
+ const login = useCallback(async () => {
46
+ setIsLoggingIn(true);
47
+ setError(null);
48
+ try {
49
+ const beginRes = await fetch(`https://${domain}/auth/webauthn/login/begin`, {
50
+ method: 'POST',
51
+ credentials: 'include',
52
+ });
53
+ if (!beginRes.ok)
54
+ throw new Error(`begin failed: ${beginRes.status}`);
55
+ const options = await beginRes.json();
56
+ const response = await startAuthentication({ optionsJSON: options });
57
+ const finishRes = await fetch(`https://${domain}/auth/webauthn/login/finish`, {
58
+ method: 'POST',
59
+ credentials: 'include',
60
+ headers: { 'Content-Type': 'application/json' },
61
+ body: JSON.stringify({ response }),
62
+ });
63
+ if (!finishRes.ok)
64
+ throw new Error(`finish failed: ${finishRes.status}`);
65
+ // Reload to pick up new auth cookies
66
+ window.location.reload();
67
+ }
68
+ catch (e) {
69
+ setError(e instanceof Error ? e : new Error(String(e)));
70
+ }
71
+ finally {
72
+ setIsLoggingIn(false);
73
+ }
74
+ }, [domain]);
75
+ return { login, isLoggingIn, error };
76
+ }
package/dist/client.d.ts CHANGED
@@ -2,3 +2,5 @@ export { BouncingProvider } from './client/provider.js';
2
2
  export { useUser } from './client/use-user.js';
3
3
  export { SignIn } from './client/sign-in.js';
4
4
  export { UserButton } from './client/user-button.js';
5
+ export { PasskeyButton } from './client/passkey.js';
6
+ export { useRegisterPasskey, useLoginWithPasskey } from './client/use-passkey.js';
package/dist/client.js CHANGED
@@ -2,3 +2,5 @@ export { BouncingProvider } from './client/provider.js';
2
2
  export { useUser } from './client/use-user.js';
3
3
  export { SignIn } from './client/sign-in.js';
4
4
  export { UserButton } from './client/user-button.js';
5
+ export { PasskeyButton } from './client/passkey.js';
6
+ export { useRegisterPasskey, useLoginWithPasskey } from './client/use-passkey.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bounc.ing/next",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "description": "Bouncing auth SDK for Next.js — auth for your app in 3 minutes",
5
5
  "type": "module",
6
6
  "exports": {
@@ -28,16 +28,19 @@
28
28
  "react-dom": ">=19.0.0"
29
29
  },
30
30
  "dependencies": {
31
+ "@simplewebauthn/browser": "^13.3.0",
31
32
  "jose": "^6.0.0"
32
33
  },
33
34
  "devDependencies": {
34
35
  "@types/react": "^19.0.0",
35
- "react": "^19.0.0",
36
36
  "next": "^15.0.0",
37
+ "react": "^19.0.0",
37
38
  "typescript": "^6.0.0",
38
39
  "vitest": "^4.1.0"
39
40
  },
40
- "files": ["dist"],
41
+ "files": [
42
+ "dist"
43
+ ],
41
44
  "license": "Apache-2.0",
42
45
  "repository": {
43
46
  "type": "git",