Authentication
Authentication in the @espressif/rainmaker-base-sdk handles user signup and login operations. This includes registration, password-based login, OTP-based login, and OAuth-based login. For more information about ESP RainMaker, see the Introduction to ESP RainMaker.
Get Auth Instance
ESPRMBase, configured in Setting up the SDK step, provides an instance of the ESPRMAuth class through getAuthInstance method, used to handle operations related to user authentication, including login, signup, forgot password, and more.
try {
const authInstance = ESPRMBase.getAuthInstance();
// Use authInstance for authentication operations
} catch (error) {
console.error("Error retrieving auth instance:", error);
}
User Signup
The user signup process involves sending a request to register a new user with a specified username and password, followed by confirming the user's account with a verification code.
Step 1: Sending Sign Up Code
To initiate the signup process, use the sendSignUpCode method:
/*
- username: The username for new user.
- password: The password for new user.
*/
try {
const response = await authInstance.sendSignUpCode(username, password);
console.log("Sign up code sent successfully:", response);
} catch (error) {
console.error("Error sending sign up code:", error);
}
Step 2: Confirming Sign Up
After sending the signup code, confirm the user's account using the confirmSignUp method:
/*
- username: The username used in sendSignUpCode method.
- verificationCode: The OTP sent to the user's username
*/
try {
const response = await authInstance.confirmSignUp(username, verificationCode);
console.log("User confirmed successfully:", response);
} catch (error) {
console.error("Error confirming user:", error);
}
User Sign in
Login Via Password
The user login process allows a registered user to authenticate by providing their username and password. Upon successful authentication, the user receives ESPRMUser instance for further interactions with the API.
/*
- username: The username of existing user.
- password: The password associated with that username.
*/
try {
const userInstance = await authInstance.login(username, password);
console.log("User logged in successfully:", userInstance);
} catch (error) {
console.error("Error logging in user:", error);
}