Jump to content

Register users and add page same as username


Vineet Sawant

Recommended Posts

  • 7 months later...
  • 1 year later...

I am also getting started with this, a basic social network, where people can sign up, have their own profile page, post stuff, comment on other's posts, etc.

How far along are others who have tried this? What are the basic parts I should start with to get a minimal viable version online as soon as possible?

Link to comment
Share on other sites

@modifiedcontent

I am assuming you want to do this on the frontend of your website?

in post #3 you can find the basic code that you need to create a user and the profile page for that user from the API side.

In that code the profile page is created before the user page and the author uses to link the 2 pages together by the username which is unique and that should be ok.

I'd suggest to first create the user and then the profile page and make the profile page owned by the user.

I use this code to create user and profile page

        // sanitize input
        $uname = $sanitizer->pageName($registrationForm->get("uname")->value);
        $email = $sanitizer->email($registrationForm->get("regemail")->value);
        $fullname = $sanitizer->text($registrationForm->get("name")->value);
        $pass = $registrationForm->get("pass")->value;
        $timezone = $sanitizer->text($registrationForm->get("timezone")->value);

                // create new user
		$u = new User(); 
		$u->name = $uname; 
		$u->email = $email;
		$u->pass = $pass;
		$u->addRole("frontend"); // the specific role that you defined
		$u->save();

		// create profile page
		$user = $users->get($uname); // get the user
		$users->setCurrentUser($user); // set the user as current user so he will own the profilepage
		$uid = $user->id;

		$p = new Page(); // new page is created and owned by the currently set user

		// set the template and parent (required)
		$p->template = $templates->get("userprofile"); // your user profile template
		$p->parent = $pages->get("/profile"); // the parent page for the user profiles

		// populate the page's fields with sanitized data
		$p->title = $uname;
		$p->name = $uname . "-" . $uid; // just to be sure that the name is unique
		$p->fullname = $fullname; 
		$p->timezone = $timezone; 
		$p->save();

Now the profile page is owned by the user. To make it only editable by that user, in your profile page template you can implement something like this (assuming the user is logged in)

if($page->created_users_id != $user->id) {
    echo "You are not allowed to edit this resource";
} else {
    //  render the user profile edit form
}

Above solution is kind of outdated. Because since PW 2.5.14 you can create alternate user templates with Multiple templates or parents for users that contain your custom fields. You can use these as userprofile pages.

I'd recommend using these as they are easier to maintain with less code.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...