Authentication
Sign up new users and sign in with password, OTP, or OAuth.
备注
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
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
- Persist sessions with a storage adapter so
getLoggedInUser()works on relaunch - Handle OTP session tokens from
requestLoginOTPuntilloginWithOTPcompletes - Do not embed OAuth secrets in client code—use build-time config for
clientId - Route to login when
getLoggedInUser()returnsnull