Skip to content

Commit

Permalink
Require challengeOptions to be passed explicitly rather than spread (
Browse files Browse the repository at this point in the history
  • Loading branch information
hwhmeikle authored Oct 15, 2024
1 parent 38cb799 commit 15f3e96
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 24 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
🚧 Work in progress: Not intended for production use. 🚧
# Authsignal React SDK

React components for [Authsignal](https://authsignal.com).

[Documentation](https://docs.authsignal.com/sdks/client/react)

## Installation
Add `@authsignal/react` to your `package.json` dependencies.
Expand Down Expand Up @@ -50,7 +54,7 @@ export function Checkout() {

if (data.challengeOptions) {
startChallenge({
...challengeOptions,
challengeOptions: data.challengeOptions,
onChallengeSuccess: ({ token }) => {
// Challenge was successful
},
Expand Down Expand Up @@ -81,7 +85,6 @@ export function Checkout() {
const { startChallengeAsync } = useAuthsignal();

const handlePayment = async () => {
setIsLoading(true);

const response = await fetch('/api/payment', {
method: 'POST',
Expand All @@ -95,7 +98,7 @@ export function Checkout() {
if (data.challengeOptions) {
try {
const { token } = await startChallengeAsync({
...challengeOptions,
challengeOptions: data.challengeOptions,
});

// Challenge was successful
Expand All @@ -109,8 +112,6 @@ export function Checkout() {
}
}
}

setIsLoading(false);
};

return (
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@authsignal/react",
"version": "0.0.14",
"version": "0.0.15",
"description": "",
"keywords": [
"authsignal",
Expand Down Expand Up @@ -33,6 +33,7 @@
"@radix-ui/react-dialog": "^1.1.1",
"@radix-ui/react-icons": "^1.3.0",
"@radix-ui/react-label": "^2.1.0",
"clsx": "^2.1.1",
"input-otp": "^1.2.4",
"react-hook-form": "^7.53.0",
"tailwind-merge": "^2.5.2",
Expand Down
11 changes: 7 additions & 4 deletions src/components/challenge/challenge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ import {
import { createTheme } from "../../lib/create-theme";

export function Challenge({
defaultVerificationMethod,
onChallengeSuccess,
onCancel,
token,
user,
verificationMethods,
challengeOptions: {
token,
user,
verificationMethods,
defaultVerificationMethod,
},

onTokenExpired,
}: ChallengeProps) {
const [open, setOpen] = React.useState(false);
Expand Down
6 changes: 4 additions & 2 deletions src/components/challenge/use-challenge-context.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { Authsignal } from "@authsignal/browser";
import React from "react";
import { ChallengeProps, TVerificationMethod } from "../../types";
import { ChallengeOptions, TVerificationMethod } from "../../types";

type ChallengeState = {
isDesktop: boolean;
verificationMethod?: TVerificationMethod;
verificationMethods?: ChallengeOptions["verificationMethods"];
user?: ChallengeOptions["user"];
setVerificationMethod: React.Dispatch<
React.SetStateAction<TVerificationMethod | undefined>
>;
handleChallengeSuccess: (params: { token: string }) => void;
authsignal: Authsignal;
} & Pick<ChallengeProps, "user" | "verificationMethods">;
};

export const ChallengeContext = React.createContext<ChallengeState | undefined>(
undefined,
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import "./main.css";

export { Authsignal } from "./authsignal";
export { useAuthsignal, ChallengeError } from "./use-authsignal";
export { Appearance, AuthsignalProps } from "./types";
export { Appearance, AuthsignalProps, ChallengeOptions } from "./types";
17 changes: 9 additions & 8 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,8 @@ export const VerificationMethod = {
export type TVerificationMethod =
(typeof VerificationMethod)[keyof typeof VerificationMethod];

export type ChallengeProps = {
export type ChallengeOptions = {
token: string;
onChallengeSuccess?: (params: { token: string }) => void;
onCancel?: () => void;
onTokenExpired?: () => void;
defaultVerificationMethod?: TVerificationMethod;
verificationMethods?: TVerificationMethod[];
user?: {
Expand All @@ -50,9 +47,13 @@ export type ChallengeProps = {
};
};

export type ChallengeProps = {
challengeOptions: ChallengeOptions;
onChallengeSuccess?: (params: { token: string }) => void;
onCancel?: () => void;
onTokenExpired?: () => void;
};

export type StartChallengeOptions = ChallengeProps;

export type StartChallengeAsyncOptions = Omit<
ChallengeProps,
"onChallengeSuccess" | "onTokenExpired" | "onCancel"
>;
export type StartChallengeAsyncOptions = { challengeOptions: ChallengeOptions };
27 changes: 25 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,11 @@ cliui@^8.0.1:
strip-ansi "^6.0.1"
wrap-ansi "^7.0.0"

clsx@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/clsx/-/clsx-2.1.1.tgz#eed397c9fd8bd882bfb18deab7102049a2f32999"
integrity sha512-eYm0QWBtUrBWZWG0d386OGAw16Z995PiOVo2B7bjWSbHedGl5e0ZWaq65kOGgUSNesEIDkB9ISbTg/JK9dhCZA==

color-convert@^1.9.0:
version "1.9.3"
resolved "https://registry.npmjs.org/color-convert/-/color-convert-1.9.3.tgz"
Expand Down Expand Up @@ -3446,7 +3451,16 @@ string-hash@^1.1.1:
resolved "https://registry.yarnpkg.com/string-hash/-/string-hash-1.1.3.tgz#e8aafc0ac1855b4666929ed7dd1275df5d6c811b"
integrity sha512-kJUvRUFK49aub+a7T1nNE66EJbZBMnBgoC1UbCZ5n6bsZKBRga4KgBRTMn/pFkeCZSYtNeSyMxPDM0AXWELk2A==

"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
"string-width-cjs@npm:string-width@^4.2.0":
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
dependencies:
emoji-regex "^8.0.0"
is-fullwidth-code-point "^3.0.0"
strip-ansi "^6.0.1"

string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
Expand Down Expand Up @@ -3884,7 +3898,16 @@ word-wrap@^1.2.5:
resolved "https://registry.npmjs.org/word-wrap/-/word-wrap-1.2.5.tgz"
integrity sha512-BN22B5eaMMI9UMtjrGd5g5eCYPpCPDUy0FJXbYsaT5zYxjFOckS53SQDE3pWkVoWpHXVb3BrYcEN4Twa55B5cA==

"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
dependencies:
ansi-styles "^4.0.0"
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
Expand Down

0 comments on commit 15f3e96

Please sign in to comment.