Secure Your Spot Now – Be First in Line for a License!Register Here !!

Module 7: Advanced App Features

Module 7: Advanced App Features

Lesson 18: User Authentication and Profiles

Objective: Implement user authentication, allowing users to log in and manage their profiles securely.

User Authentication:

User authentication ensures that only registered users can access their albums and photos. Here's a function to authenticate users:

function AuthenticateUser(Head: PUser; Name, Password: String): PUser;
begin
  while Head <> nil do
  begin
    if (Head^.Name = Name) and (Head^.Password = Password) then
    begin
      AuthenticateUser := Head;
      Exit;
    end;
    Head := Head^.Next;
  end;
  AuthenticateUser := nil;
end;

Explanation:

  • AuthenticateUser: checks if the provided name and password match a registered user.
  • while Head <> nil do: iterates through the user list.
  • if (Head^.Name = Name) and (Head^.Password = Password) then: verifies the user's credentials.
  • AuthenticateUser := nil; returns nil if no match is found.

Managing User Profiles:

Users should be able to update their profiles, such as changing their password. Here’s a procedure to change a user’s password:

procedure ChangePassword(User: PUser; NewPassword: String);
begin
  User^.Password := NewPassword;
end;

Explanation:

  • ChangePassword: takes the user and the new password as parameters.
  • User^.Password := NewPassword; updates the user's password.
Exercises:
  • Implement the user authentication feature. Test it by registering multiple users and logging in with different credentials.
  • Create a menu option that allows logged-in users to change their password. Test it by updating a user's password and attempting to log in with the new password.

Lesson 19: Photo Sharing and Privacy Settings

Objective: Add functionality for sharing photos with specific users and managing privacy settings.

Sharing Photos:

To share photos, we’ll need to allow users to link their albums to other users. Here’s a procedure to share an album:

procedure ShareAlbum(User: PUser; Album: PAlbum; SharedUser: PUser);
var
  SharedAlbum: PAlbum;
begin
  New(SharedAlbum);
  SharedAlbum^.Name := Album^.Name;
  SharedAlbum^.Photos := Album^.Photos;
  SharedAlbum^.Next := SharedUser^.Albums;
  SharedUser^.Albums := SharedAlbum;
end;

Explanation:

  • ShareAlbum: takes the current user, the album to be shared, and the user with whom it’s shared as parameters.
  • New(SharedAlbum); allocates memory for the shared album.
  • SharedAlbum^.Name := Album^.Name; copies the album name.
  • SharedAlbum^.Photos := Album^.Photos; links the photos to the shared album.
  • SharedAlbum^.Next := SharedUser^.Albums; links the shared album to the other user’s albums.
  • SharedUser^.Albums := SharedAlbum; updates the other user’s album list to include the shared album.

Privacy Settings:

Users should be able to set privacy settings on their albums. We’ll add a field to the album structure to store privacy settings:

type
  PAlbum = ^TAlbum;
  TAlbum = record
    Name: String;
    Photos: PPhoto;
    Privacy: String;  // 'Public' or 'Private'
    Next: PAlbum;
  end;

Explanation:

  • Privacy: indicates whether the album is public or private.
  • 'Public': means the album can be shared and viewed by others.
  • 'Private': means the album is only accessible by the owner.
Exercises:
  • Implement the photo-sharing feature. Test it by creating albums, sharing them with other users, and ensuring they can view the shared content.
  • Add privacy settings to albums. Test the functionality by setting different privacy levels and verifying access control.

Lesson 20: Data Persistence

Objective: Implement data persistence so that user data, albums, and photos are stored securely and remain available between sessions.

Saving Data to Files:

We’ll need to save user data and albums to files to ensure they persist between sessions. Here’s an example of how to save user data:

procedure SaveUserData(User: PUser; FileName: String);
var
  f: TextFile;
begin
  AssignFile(f, FileName);
  Rewrite(f);
  while User <> nil do
  begin
    Writeln(f, User^.Name);
    Writeln(f, User^.Password);
    User := User^.Next;
  end;
  CloseFile(f);
end;

Explanation:

  • AssignFile(f, FileName); links the file variable to a physical file.
  • Rewrite(f); opens the file for writing, clearing existing content.
  • Writeln(f, User^.Name); writes the user's name to the file.
  • Writeln(f, User^.Password); writes the user's password to the file.
  • User := User^.Next; moves to the next user in the list.
  • CloseFile(f); closes the file after writing is complete.

Loading Data from Files:

We’ll also need to load the data back into the program when it starts:

procedure LoadUserData(var Head: PUser; FileName: String);
var
  f: TextFile;
  Name, Password: String;
  NewUser: PUser;
begin
  AssignFile(f, FileName);
  Reset(f);
  while not Eof(f) do
  begin
    Readln(f, Name);
    Readln(f, Password);
    New(NewUser);
    NewUser^.Name := Name;
    NewUser^.Password := Password;
    NewUser^.Albums := nil;
    NewUser^.Next := Head;
    Head := NewUser;
  end;
  CloseFile(f);
end;

Explanation:

  • Reset(f); opens the file for reading.
  • Readln(f, Name); reads the user's name from the file.
  • Readln(f, Password); reads the user's password from the file.
  • New(NewUser); allocates memory for the new user.
  • NewUser^.Name := Name; assigns the name to the user.
  • NewUser^.Password := Password; assigns the password to the user.
  • NewUser^.Next := Head; links the new user to the head of the user list.
  • Head := NewUser; updates the head of the user list.
Exercises:
  • Implement the save and load functions for user data. Test them by saving users to a file, restarting the program, and loading the data back.
  • Extend the save and load functions to include albums and photos, ensuring that all data persists correctly between sessions.
Next Steps:

In the next module, we’ll focus on testing and debugging the app to ensure all features work correctly and the app is stable.

Secure Your Spot Now – Be First in Line for a License!