JavaScript SDK Guide

Pure JavaScript/TypeScript SDK without React dependencies. Use this for Vue, Svelte, Angular, or any non-React project.

Installation

npm install get401

Initialize the client

import { Get401Client } from 'get401'
 
const client = new Get401Client('your-app-id', true) // appId, debug

Login Flow

Email and password are sent together in a single request.

const loginResponse = await client.login('user@example.com', 'password123')
 
// If MFA is enabled, verify OTP
if (loginResponse.next === 'otp') {
  const otpResponse = await client.challengeOTP('123456')
}

Registration Flow

// 1. Register
const registerResponse = await client.register('user@example.com', 'password123')
 
// 2. Verify email if required
if (registerResponse.setup === 'email') {
  await client.challengeEmailSetup('email-verification-code')
}
 
// 3. Setup OTP / MFA
const otpSetup = await client.getOTPSetup()
// otpSetup.otpQrCode - show this QR code to the user
// otpSetup.secret    - manual entry key
 
await client.verifyOTPSetup('otp-code')

Session Management

// Get current user
const user = await client.getUser()
 
// Logout
await client.logout()
 
// Refresh token manually (auto-refreshed by default)
await client.refreshToken()

API Reference

Get401Client

const client = new Get401Client(appId: string, debug?: boolean)
Method Returns Description
login(email, password) LoginResponse Login - both fields sent in one request
challengeOTP(code) ChallengeResponse Submit OTP code
register(email, password) RegisterResponse Register new user
challengeEmailSetup(code) - Verify email during registration
getOTPSetup() OTPSetupResponse Get QR code and secret for MFA setup
verifyOTPSetup(code) - Confirm MFA setup
getUser() User Get current user
logout() - Logout
refreshToken() - Refresh authentication token
isAuthenticated() boolean Check auth status
getAppId() string Get the app ID

Types

interface User {
  id: string
  email: string
  name?: string
  picture?: string
}
 
interface LoginResponse {
  next: 'email' | 'otp'
}
 
interface RegisterResponse {
  setup: 'email' | 'otp'
}
 
interface OTPSetupResponse {
  secret: string
  otpUrl: string
  otpQrCode: string  // base64 QR code image
}
 
interface ChallengeResponse {
  next?: 'email' | 'otp' | null
}