Otpless-js-sdk is a vanilla javascript SDK used for authenticating users using OTPless in client side application. Demo video
- Fetch WhatsApp intent and redirect users to WhatsApp.
- Get token from query params
- Get state from local storage
- Install the otpless-js-sdk from the npm repository.
npm i otpless-js-sdk
- Initialize the sdk.
appId
is required.enableErrorLogging
is an optional parameter. On setting it to true, the error logs are consoled to the stderr.
import otplessSdk from "otpless-js-sdk";
const sdkIntance = new otplessSdk(
{
appId: "YOUR_APP_ID",
enableErrorLogging: true
}
);
- create an Onclick function using
createGetIntentOnClick
. It takesredirectionURL
object property as a parameter. This is the url to which the client is redirected to once it gets a response in WhatsApp.
<button
onClick={sdkIntance.createGetIntentOnClick({
redirectionURL: "YOUR_REDIRECTION_URL",
})}
>
Login with WhatsApp
</button>
- After successfull redirection from WhatsApp, extract the token from query params using the static function
getTokenFromQueryParams
and the state from the local storage using the static functiongetState
. The token along with the state needs to be passed to your server which in turn should make call to OTPless server for user details.
const token=otplessSdk.getTokenFromQueryParams();
const state=otplessSdk.getState()
MIT
Made with ❤️ by OTPless