-
Create an App in OTPless dashboard and copy the
APP ID
-
Add OTPLESS Script as a function
Add the contents of initOtpless.js file in
src/utils/initOtpless.js
file in your project and paste theAPP ID
here.initOtpless.js file exports the following functions
initOTPless, to initialize the OTPless SDK,
Authenticate, to authenticate the user.
verifyOTP, to verify the OTP.
-
Load the script in Login/Signup component and add callback function
Add following code in Login/Signup component.
retrive data using otplessUser object
useEffect(() => initOTPless(handleUserData), []); const handleUserData = (otplessUser) => { alert(JSON.stringify(otplessUser)); };
-
Create your UI
Design UI to collect user input and trigger authentication method of your choice.
const [phone, setPhone] = useState(null) const [otp, setOtp] = useState(null) return <> <div id="mobile-section"> <input id='mobile-input' placeholder='Enter mobile number' onChange={(e) => setPhone(e.target.value)} /> <button onClick={() => Authenticate({ channel: 'PHONE', phone })}>Request OTP</button> </div> <div id="otp-section"> <input id='otp-input' placeholder='Enter OTP' onChange={(e) => setOtp(e.target.value)} minLength={6} maxLength={6} /> <button onClick={() => verifyOTP({ channel: activeSection, otp, phone })}>Verify OTP</button> </div> <button onClick={() => Authenticate({channel: 'OAUTH', channelType:'WHATSAPP' })}>Authenticate with WhatsApp</button> <button onClick={() => Authenticate({channel: 'OAUTH', channelType:'GOOGLE'})}>Authenticate with Gmail</button> </>
view source (NOTE: This integration has a different ui)
This demo implementation adds extra modularity, scalability and reusability to the OTPless Headless sdk
Prerequisites: NodeJS
-
Install Packages
npm install
-
Run the demo
npm run dev
-
Open localhost:5173 in your browser
-
Switch branches to check out available options to integrate OTPless in your project