跳到主要内容

Authentication

Sign up new users and sign in with password, OTP, or OAuth.

备注

Configure ESPRMBase first. authInstance comes from ESPRMBase.getAuthInstance(). Successful sign-in returns userInstance (ESPRMUser) for device and group APIs.

What This Module Does ?

Handles registration and login through ESPRMAuth: email/username signup with verification, password login, OTP login, and third-party OAuth.

Use Passwords for forgot-password and change-password after sign-in.

Expected outcome: An ESPRMUser instance with valid session tokens.

Common Workflows

Get auth instance

const authInstance = ESPRMBase.getAuthInstance();

Sign up

await authInstance.sendSignUpCode(username, password);
await authInstance.confirmSignUp(username, verificationCode);

User sign in

Password:

const userInstance = await authInstance.login(username, password);

OTP (two steps):

const sessionToken = await authInstance.requestLoginOTP(username);
const userInstance = await authInstance.loginWithOTP(
username,
verificationCode,
sessionToken
);

OAuth:

const userInstance = await authInstance.loginWithOauth("Google");

Valid provider identifiers: "Google" · "GitHub" · "SignInWithApple". Configure authUrl, clientId, redirectUrl, and oauthAdapter in Getting Started—see Advanced Concepts below.

Error Handling

try {
const userInstance = await authInstance.login(username, password);
} catch (error) {
console.error("Login failed:", error);
}

Common issues: unconfirmed signup, wrong OTP or session token, missing OAuth adapter or redirect configuration.

Advanced Concepts

OAuth setup

important

Before loginWithOauth, set in ESPRMBase.configure():

  • authUrl, clientId, redirectUrl
  • oauthAdapter (Adapters)

Platform redirect URIs: Android · iOS

Valid OAuth provider identifiers

Pass one of these string values to loginWithOauth():

"Google"          // ESPIdProvider.GOOGLE
"GitHub" // ESPIdProvider.GITHUB
"SignInWithApple" // ESPIdProvider.SIGN_IN_WITH_APPLE

Best Practices

  1. Persist sessions with a storage adapter so getLoggedInUser() works on relaunch
  2. Handle OTP session tokens from requestLoginOTP until loginWithOTP completes
  3. Do not embed OAuth secrets in client code—use build-time config for clientId
  4. Route to login when getLoggedInUser() returns null

On this page