Quantcast
Channel: Sarina DuPont, Product Manager RAD Studio
Viewing all articles
Browse latest Browse all 132

BaaS User Accounts and Password Management

$
0
0

Over the last couple of months, I have been covering various BaaS (Backend-as-a-Service) features introduced in RAD Studio XE6.
Today, I thought I would expand on the User Account Creation and Authentication topic I previously covered by showing you how to allow the user to change their password within your BaaS enabled application.

In this app, I am using Kinvey as my BaaS Provider but the UI and code are the same if you are using Parse.
The app consists of 3 Edit controls: One for Username, one for Password and one for the Session ID.
There are also 4 TButtons on the form: One for Sign Up, one for Login, one for Logout, and one for Change Password.
I parented the input controls to TListbox items and added ListBoxItemGroupHeaders to divide up the UI.

Screenshot: iOS UI at design time; Windows app at runtime with Mobile Preview style

Below is the Object Pascal code for my application:

unit Unit7;

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.ServiceTypes, REST.Backend.MetaTypes, System.JSON,
  REST.Backend.KinveyServices, FMX.Edit, FMX.StdCtrls, REST.Backend.Providers,
  REST.Backend.ServiceComponents, REST.Backend.KinveyProvider, FMX.ListBox,
  FMX.Layouts;

type
  TForm7 = class(TForm)
    KinveyProvider1: TKinveyProvider;
    BackendUsers1: TBackendUsers;
    ButtonChangePassword: TButton;
    ButtonLogin: TButton;
    ButtonLogout: TButton;
    ButtonSignup: TButton;
    EditPassword: TEdit;
    EditSessionID: TEdit;
    EditUserName: TEdit;
    ListBox1: TListBox;
    ListBoxItem1: TListBoxItem;
    ListBoxItem2: TListBoxItem;
    ListBoxItem3: TListBoxItem;
    ListBoxGroupHeader1: TListBoxGroupHeader;
    ListBoxGroupHeader2: TListBoxGroupHeader;
    ToolBar1: TToolBar;
    Label1: TLabel;
    procedure ButtonSignupClick(Sender: TObject);
    procedure ButtonLoginClick(Sender: TObject);
    procedure ButtonLogoutClick(Sender: TObject);
    procedure ButtonChangePasswordClick(Sender: TObject);
  private
    { Private declarations }
    FLogin: TBackendEntityValue;
    procedure Login;
    procedure Signup;
  public
    { Public declarations }
  end;

var
  Form7: TForm7;

implementation

{$R *.fmx}

procedure TForm7.ButtonChangePasswordClick(Sender: TObject);
var
  LPassword: TJSONObject;
  LUpdatedAt: TBackendEntityValue;
begin
  LPassword := TJSONObject.Create;
  try
    LPassword.AddPair('password', EditPassword.Text);
    BackendUsers1.Users.UpdateUser(FLogin, LPassword, LUpdatedAt);
    Login;
  finally
    LPassword.Free;
  end;
end;

procedure TForm7.ButtonLoginClick(Sender: TObject);
begin
  Login;
end;

procedure TForm7.ButtonLogoutClick(Sender: TObject);
begin
  BackendUsers1.Users.Logout;
  EditSessionID.Text := '';
  EditUsername.Text := '';
  EditPassword.Text := '';
end;

procedure TForm7.Signup;
begin
  BackendUsers1.Users.SignupUser(EditUserName.Text, EditPassword.Text, nil, FLogin);
  BackendUsers1.Users.Login(FLogin);
  EditSessionID.Text := FLogin.AuthToken;
end;

procedure TForm7.Login;
begin
  BackendUsers1.Users.LoginUser(EditUserName.Text, EditPassword.Text, FLogin);
  BackendUsers1.Users.Login(FLogin);
  EditSessionID.Text := FLogin.AuthToken;
end;

procedure TForm7.ButtonSignupClick(Sender: TObject);
begin
  Signup;
end;

end.

Viewing all articles
Browse latest Browse all 132

Trending Articles