Recently, I covered the steps necessary to add password reset email notification functionality to your BaaS enabled Delphi XE6 app using Kinvey. Today, I thought I would explain how to do this with Parse.
This demo shows you how to to derive from the existing ParseAPI class (TExtendParseApi = class(TParseApi)) for the Parse component and add a new method for executing Parse’s password reset.
Inside your Parse.com account, you can define email settings for the password reset email.
My application consists of 4 edit controls parented to 4 TListBox items:
- EditUsername
- EditPassword
- EditSessionToken
- EditEmail
TListBox has GroupingKind set to Grouped and StyleLookUp set to ‘transparentlistboxstyle’.
I added the ParseProvider1 component to my form and entered the AppID, MasterKey and RESTAPIKey that is displayed inside my Parse.com. In order to extend the functionality, I had 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.
Here is the code for my ExtendParseApiUnit
unit ExtendParseApiUnit; interface uses REST.Backend.Parseapi, System.JSON, REST.Client, REST.Types; type TExtendParseApi = class(TParseApi) private public procedure SignupUser(const AUserName, APassword, AEmail: string; out ALogin: TParseAPI.TLogin); overload; procedure PasswordReset(const AEmail: string); end; implementation // New version of signup user that has a password parameter procedure TExtendParseApi.SignupUser(const AUserName, APassword, AEmail: string; out ALogin: TParseAPI.TLogin); var LJSON: TJSONObject; begin LJSON := TJSONObject.Create; try LJSON.AddPair('email', AEmail); inherited SignupUser(AUserName, APassword, LJSON, ALogin); finally LJSON.Free; end; end; //curl -X POST \ // -H "X-Parse-Application-Id: cIj01OkQeJ8LUzFZjMnFyJQD6qx0OehYep0mMdak" \ // -H "X-Parse-REST-API-Key: yVVIeShrcZrdr3e4hMLodfnvLckWBZfTonCYlBsq" \ // -H "Content-Type: application/json" \ // -d '{"email":"coolguy@iloveapps.com"}' \ // https://api.parse.com/1/requestPasswordReset procedure TExtendParseApi.PasswordReset(const AEmail: string); var LJSON: TJSONObject; begin Request.ResetToDefaults; AddAuthParameters; Request.Method := TRESTRequestMethod.rmPOST; Request.Resource := 'requestPasswordReset'; LJSON := TJSONObject.Create; try LJSON.AddPair('email', AEmail); Request.AddBody(LJSON); Request.Execute; CheckForResponseError([201]); finally LJSON.Free; end; end; end.
Here is the code for my mobile client
unit ParseFormUnit; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, IPPeerClient, REST.OpenSSL, REST.Backend.ParseProvider, FMX.StdCtrls, FMX.Edit, ExtendParseApiUnit, FMX.ListBox, FMX.Layouts; type TParseForm = class(TForm) ButtonSignup: TButton; ButtonPasswordReset: TButton; ListBox1: TListBox; UserLabel: TListBoxItem; EditUserName: TEdit; PasswordLabel: TListBoxItem; EditPassword: TEdit; EmailLabel: TListBoxItem; EditEmail: TEdit; SessionLabel: TListBoxItem; EditSessionToken: TEdit; ToolBar1: TToolBar; Label5: TLabel; ParseProvider1: TParseProvider; procedure ButtonSignupClick(Sender: TObject); procedure ButtonPasswordResetClick(Sender: TObject); procedure FormCreate(Sender: TObject); private { Private declarations } FApi: TExtendParseAPI; procedure ClearFields; public { Public declarations } end; var ParseForm: TParseForm; implementation {$R *.fmx} uses REST.Backend.ParseApi, System.JSON; procedure TParseForm.ButtonPasswordResetClick(Sender: TObject); begin FApi.PasswordReset(EditEmail.Text); ShowMessage('Message sent to ' + EditEmail.Text); end; procedure TParseForm.ButtonSignupClick(Sender: TObject); var LLogin: TParseAPI.TLogin; begin FApi.SignupUser(EditUsername.Text, EditPassword.Text, EditEmail.Text, LLogin); FApi.Login(LLogin); // Make subsequent calls as this user EditSessionToken.Text := LLogin.SessionToken; ShowMessage('Welcome ' + LLogin.User.UserName); end; procedure TParseForm.FormCreate(Sender: TObject); begin FApi := TExtendParseAPI.Create(Self); // Assign appkey, etc. ParseProvider1.UpdateApi(FApi); end; procedure TParseForm.ClearFields; begin EditUserName.Text := ''; EditPassword.Text := ''; EditEmail.Text := ''; EditSessionToken.Text := ''; end; end.
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: