BaaS Tutorial: Triggering Password Reset Emails using Kinvey
This tutorial outlines how to leverage the cloud based password reset email functionality that many BaaS providers offer. This tutorial uses Kinvey and assumes that you have signed up for an account on Kinvey.com.
The demo used in this tutorial shows you how to derive from the existing Kinvey API class for the Kinvey component and add a new method for executing Kinvey’s password reset.
Step 1: Set up your Email Template
Inside your Kinvey.com account, go to AddOns->Messaging->Email Templates. Next, select the ‘Password Reset‘ email template. There you can define Sender and Reply To information, the Subject line and the HTML for the body of your email. The App Name referenced in the email is the name of your application you defined inside your Kinvey.com account.
Step 2: Build the C++ Multi-Device Application
Your application consists of a toolbar with a title, several buttons and 4 edit controls parented to 4 TListBox items.TListBox has GroupingKind set to Grouped and StyleLookUp set to ‘transparentlistboxstyle’.
Step 3: Add your KinveyProvider component and account credentials
Add the KinveyProvider1 component to your form and enter the AppKey, AppSecret and MasterSecret that is displayed inside your Kinvey.com account under ‘AppName’->Manage Environments. In order to extend the functionality, you will need to add a new method for triggering a password reset email notification which you can see in the code samples below.
Shown: Mobile app running on Windows with Mobile Preview style.
Step 4: Extend the KinveyProvider API and write your application code
Here is the code for my ExtendKinveytemplate.h file:
//--------------------------------------------------------------------------- #ifndef ExtendApiTemplate_h #define ExtendApiTemplate_h #include <System.Classes.hpp> #include <memory> //--------------------------------------------------------------------------- template<typename T, typename L> class TExtendApi : public T { public: __fastcall TExtendApi(TComponent* AOwner): T(AOwner, NULL){} void __fastcall SignupUser(const String & AUserName, const String & APassword, const String & AEmail, L & ALogin); void __fastcall PasswordReset(const String &AEmail); }; template<typename T, typename L> void __fastcall TExtendApi<T,L>::SignupUser(const String & AUserName, const String & APassword, const String & AEmail, L & ALogin) { std::auto_ptr<TJSONObject> jsonEmail(new TJSONObject()); jsonEmail->AddPair("email", AEmail); T::SignupUser(AUserName, APassword, jsonEmail.get(), ALogin); } template<typename T, typename L> void __fastcall TExtendApi<T,L>::PasswordReset(const String &AEmail) { #ifdef _KINVEY_ Request->ResetToDefaults(); // Basic Auth ConnectionInfo = this->ConnectionInfo; ConnectionInfo.UserName = AEmail; T::AddAuthParameter(TAuthentication::UserName); // App credentials T::AddAuthParameter(TAuthentication::AppSecret); Request->Method = TRESTRequestMethod::rmPOST; Request->Resource = "rpc/{appkey}/{email}/user-password-reset-initiate"; Request->AddParameter("email", AEmail, TRESTRequestParameterKind::pkURLSEGMENT); #else Request->ResetToDefaults(); T::AddAuthParameters(); Request->Method = TRESTRequestMethod::rmPOST; Request->Resource = "requestPasswordReset"; std::auto_ptr<TJSONObject> jsonEmail(new TJSONObject()); jsonEmail->AddPair("email", AEmail); Request->AddBody(jsonEmail.get()); #endif Request->Execute(); int validCodes[] = {201}; T::CheckForResponseError(validCodes, 1); }
#endif
Here is the code for my mobile client:
//--------------------------------------------------------------------------- #include <fmx.h> #pragma hdrstop #define _KINVEY_ #include "KinveyFormUnit.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.fmx" TKinveyForm *KinveyForm; //--------------------------------------------------------------------------- __fastcall TKinveyForm::TKinveyForm(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TKinveyForm::FormCreate(TObject *Sender) { fApi = new TExtendApi<TKinveyApi, TKinveyApi::TLogin>(this); KinveyProvider1->UpdateApi(fApi); } //--------------------------------------------------------------------------- void __fastcall TKinveyForm::ButtonSignupClick(TObject *Sender) { TKinveyApi::TLogin lLogin; EditUserName->Text = ""; fApi->SignupUser(EditUserName->Text, EditPassword->Text, EditEmail->Text, lLogin); fApi->Login(lLogin); EditSessionToken->Text = lLogin.AuthToken; ShowMessage("Welcome" + lLogin.User.UserName); } //--------------------------------------------------------------------------- void __fastcall TKinveyForm::ButtonPasswordResetClick(TObject *Sender) { fApi->PasswordReset(EditEmail->Text); ShowMessage("Message sent to " + EditEmail->Text); } //---------------------------------------------------------------------------
Below you see a screenshot of the email that I received after clicking on the ‘Reset Password via Email’ button inside my app:
After clicking on the automatically generated reset URL, you see a web based form where you can reset your password: