Welcome Guest, Not a member yet? Register   Sign In
Shield: Set random user ID
#1

(This post was last modified: 10-02-2023, 07:38 AM by AndrewDFrazier. Edit Reason: Additional information )

I'm working on rebuilding a project that was built on CI3 and used Community Auth for user registration functions.  A key part of this user creation was that random IDs were used rather than sequential ones.  I'd like to reproduce that using Shield.
I've generated an unused ID and set $user->id to use it within RegisterController:

Code:
        // Workaround for email only registration/login
        if ($user->username === null) {
            $user->username = null;
        }

$user->id = $this->get_unused_id();

        try {
$users->save($user);
        } catch (ValidationException $e) {
            return redirect()->back()->withInput()->with('errors', $users->errors());
        }

However, this causes insertion to fail because of a foreign key constraint failure:
Code:
Cannot add or update a child row: a foreign key constraint fails (`auth_identities`, CONSTRAINT `auth_identities_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE)

Is there a better way to generate and use random IDs?

I should add that if I remove my line setting $user->ID then the user creation works as normal.
Reply
#2

Quote:save()
This is a wrapper around the insert() and update() methods that handle inserting or updating the record automatically, based on whether it finds an array key matching the primary key value:
https://codeigniter4.github.io/CodeIgnit....html#save
Reply
#3

Create your own User Provider and customize.
See https://github.com/codeigniter4/shield/b...rovider.md
Reply




Theme © iAndrew 2016 - Forum software by © MyBB