useSignIn()
The useSignIn() hook exposes the underlying authentication primitives utilized by the pre-built <SignInForm /> component.
This hook facilitates the development of custom authentication interfaces, enabling precise control over input handling, state transitions, and error management without relying on predefined UI components.
Hook Import
import { useSignIn } from "@wacht/tanstack-router" ;
Hook Interface
The hook returns an object detailing the current status of the sign-in attempt, the active authentication strategy, and the methods required to progress the state machine.
Indicates whether the SDK is actively processing a network request or managing internal state transitions.
Show SignIn Interface Methods
createStrategy
(strategy: SignInStrategy) => SignInFunction
Show Initialize Authentication Strategy
The authentication strategy identifier (e.g., 'email', 'oauth', 'passkey').
A typed execution function for the specified strategy.
prepareVerification
(params: VerificationParams) => Promise<ApiResult<PrepareVerificationResponse>>
Show Initialize Verification Flow
params
VerificationParams
required
Configuration object defining the verification strategy (e.g., 'email_otp', 'phone_otp', 'magic_link') and any relevant redirect URIs.
returns
Promise<ApiResult<PrepareVerificationResponse>>
The resolution of the verification dispatch request.
completeVerification
(verificationCode: string) => Promise<Session>
Show Finalize Verification
The verification code or One-Time Password submitted by the client.
Resolves with a fully authenticated session construct upon successful validation.
completeProfile
(data: ProfileCompletionData) => Promise<Session>
Show Submit Profile Completion Data
data
ProfileCompletionData
required
The mandatory profile attributes required to finalize the authentication process.
Resolves with an authenticated session upon successful submission.
identify
(identifier: string) => Promise<IdentifyResult>
The email address or unique identifier of the target user subject.
Provides the supported authentication vectors configured for the subject (sso, social, password).
initEnterpriseSso
(connectionId: string, redirectUri?: string) => Promise<{ sso_url: string; session: Session }>
Show Initialize SAML/OIDC SSO
The unique identifier of the target Enterprise SSO configuration.
An optional callback URI to process the authentication payload post-redirect.
returns
Promise<{ sso_url: string; session: Session }>
Resolves with either an Identity Provider URL for redirection, or a hydrated session object if instantaneously resolved.
The active context state of the current sign-in execution.
Terminates and clears the current sign-in attempt context from memory.
setSignInAttempt
(attempt: SigninAttempt | null) => void
Direct mutation method to override the existing sign-in attempt state representation.
Implementation Guidelines
Developing a Custom Credential Interface
The following implementation demonstrates the construction of a custom credential submission interface utilizing the signIn object:
import { useSignIn } from "@wacht/tanstack-router" ;
import { useState } from "react" ;
import { useNavigate } from "@tanstack/react-router" ;
export function CustomSignInForm () {
const { loading , signIn } = useSignIn ();
const navigate = useNavigate ();
const [ emailAddress , setEmailAddress ] = useState ( "" );
const [ password , setPassword ] = useState ( "" );
const [ error , setError ] = useState ( "" );
const handleSubmit = async ( e : React . FormEvent ) => {
e . preventDefault ();
if ( loading ) return ;
setError ( "" );
try {
const emailStrategy = signIn . createStrategy ( "email" );
await emailStrategy ({
email: emailAddress ,
password ,
});
// The Wacht API transparently sets an HTTP-only session cookie upon success.
navigate ({ to: "/dashboard" });
} catch ( err : any ) {
setError ( err . message || "Authentication negotiation failed." );
}
};
return (
< form onSubmit = { handleSubmit } className = "flex flex-col gap-4" >
{ error && < div className = "text-red-700 font-medium text-sm border border-red-200 p-2 rounded bg-red-50" > { error } </ div > }
< input
type = "email"
value = { emailAddress }
onChange = { ( e ) => setEmailAddress ( e . target . value ) }
className = "border p-2 rounded"
/>
< input
type = "password"
value = { password }
onChange = { ( e ) => setPassword ( e . target . value ) }
className = "border p-2 rounded"
/>
< button type = "submit" disabled = { loading } className = "bg-blue-600 text-white p-2 rounded" >
{ loading ? "Authenticating..." : "Sign In" }
</ button >
</ form >
);
}
Security Considerations
Implementing custom authentication interfaces via useSignIn() bypasses the protections embedded within the pre-built <SignInForm /> component. Consequently, the consuming application is directly responsible for rendering granular API error messages (e.g., invalid_password), orchestrating multi-factor authentication steps, and appropriately handling brute-force lockouts.