Introduction
Welcome to the documentation for ParticleAuthService! This is a comprehensive guide that will walk you through the features and functionality of the ParticleAuthService library. ParticleAuthService provides authentication services for Particle devices and applications, allowing you to securely authenticate users within your Particle-powered projects.
Installation
To get started with ParticleAuthService, follow these steps:
- Ensure you have the latest version of the Particle CLI installed on your machine.
- Open a terminal window and run the following command to install the ParticleAuthService library:
particle library add ParticleAuthService
Usage
Here’s an overview of how you can use ParticleAuthService in your Particle projects:
1. Import the Library
Start by importing the ParticleAuthService library into your project:
#include "ParticleAuthService.h"
2. Instantiate ParticleAuthService
Create an instance of the ParticleAuthService class:
ParticleAuthService authService;
3. Authenticate Users
Use the authenticate()
method to authenticate your users:
authService.authenticate(username, password);
Methods
authenticate(username, password)
This method is used to authenticate a user with their username and password. It returns a boolean value indicating whether the authentication was successful or not.
Parameters:
username
(string) – The username of the user.password
(string) – The password of the user.
Returns:
A boolean value indicating whether the authentication was successful or not.
getAccessToken()
This method returns the access token associated with the currently authenticated user. If no user is authenticated, it returns an empty string.
Returns:
The access token associated with the currently authenticated user.
getUserInfo()
This method returns a JSON object containing information about the currently authenticated user. If no user is authenticated, it returns an empty JSON object.
Returns:
A JSON object containing information about the currently authenticated user.
Examples
Example 1: Authenticating a User
Here’s an example that shows how to authenticate a user using ParticleAuthService:
if (authService.authenticate("user@example.com", "password123")) {
// User authentication successful
} else {
// User authentication failed
}
Example 2: Retrieving User Information
Here’s an example that demonstrates how to retrieve information about the currently authenticated user:
String accessToken = authService.getAccessToken();
if (!accessToken.isEmpty()) {
// User is authenticated, retrieve user info
JSONValue userInfo = authService.getUserInfo();
// Process user info here
} else {
// User is not authenticated
}
Conclusion
Congratulations! You now have a solid understanding of ParticleAuthService and how to use it to authenticate users within your Particle projects. Use this powerful library to enhance security and control access to your Particle devices and applications. If you have any further questions or need additional support, please reach out to our ParticleAuthService community.