User Authentication - modio/modio-sdk-legacy GitHub Wiki
We authenticate users via a simple email verification flow - which is used to generate OAuth 2 access tokens. A user can manage the tokens they have been issued via their OAuth 2 token page.
Email authentication flow
Authentication occurs by asking for their email and then for the 5-digit security code sent to their email. To do so, first call emailRequest to send the email and then emailExchange to exchange the security code for an access token that will be handled internally by the SDK so you won't need to deal with it.
emailRequest
void Instance::emailRequest(const std::string& email, const std::function<void(const modio::Response&, const std::string&)>& callback)
API endpoint used: Email Request
Requests a 5-digit security code to be sent via email. emailExchange should be called after to complete the authentication process.
Function parameters
Name | Type | Description |
---|---|---|
const std::string& |
User's email. | |
callback | const std::function<void(const modio::Response&, const std::string&)>& |
Function called once the process finished. |
Callback parameters
Name | Type | Description |
---|---|---|
response | const modio::Response& |
modio::Response object that contains the mod.io response status. |
Example
modio_instance.emailRequest("[email protected]", [&](const modio::Response& response)
{
if (response.code == 200)
{
//Authentication code successfully requested
}
});
emailExchange
void Instance::emailExchange(const std::string& security_code, const std::function<void(const modio::Response&)>& callback)
API endpoint used: Email Exchange
Exchanges the 5-digit code for an authentication token. The token is handled internally by the SDK.
Function parameters
Name | Type | Description |
---|---|---|
security_code | const std::string& |
5-digit code sent to users via email when emailRequest was called. |
callback | const std::function<void(const modio::Response&)>& |
Function called once the process finished. |
Callback parameters
Name | Type | Description |
---|---|---|
response | const modio::Response& |
modio::Response object that contains the mod.io response status. |
Example
modio_instance.emailExchange("50AD4", [&](const modio::Response& response)
{
if (response.code == 200)
{
//Email exchanged successfully
}
});
authenticateViaToken
void Instance::authenticateViaToken(const std::string &access_token);
Authenticates the user by providing a valid mod.io token directly.
Function parameters
Name | Type | Description |
---|---|---|
access_token | const std::string& |
mod.io user authentication token. |
Example
modio_instance.authenticateViaToken("eykger...lkw421");
isLoggedIn
Returns true if the user is logged in, otherwise returns false.
bool Instance::isLoggedIn()
getCurrentUser
Returns a modio::User object corresponding to the current user logged in.
modio::User Instance::getCurrentUser()
Example
if(modio_instance.isLoggedIn())
{
std::cout << "Logged in as " << modio_instance.getCurrentUser().username << std::endl;
}else
{
std::cout << "You are not logged in." << std::endl;
}
logout
Logs out the user from mod.io.
void Instance::logout()