How to move CodeIgniter user passwords to Laravel

2019-08-06 22:13发布

问题:

I have a CI project and want to migrate its database to the Laravel's one.

The only problem is that they have different ways of hashing user passwords and so I cannot find a way to move them from one database to another.

I have already googled for the answer but nobody I found speaks about migrating passwords.

Thanks in advance.

回答1:

I have no experience with CodeIgniter and don't know how it does password hashing, but here's how I would approach the problem.

To make something clear: You can only "convert" the password to a Laravel hash if you have the actual password (in plain text). As you don't store the plain password you only have it at the moment the user logs in our enters the password somewhere.

Therefore you have to realize that this migration isn't done in a few hours. It will take some time for all your users to enter their passwords.

So what I'm getting at is you should add a field to your users table for the CodeIgniter password. Let's call it ci_password. (Or probably you just have to rename the old password column to this and create a new one for the Laravel password).

Now every time a user logs in, you first check if a Laravel password is stored in the database and attempt a log in. If there is no Laravel hash stored, check with the ci_password. (For this you will have to make CodeIgniters hashing work inside your Laravel application. Sorry can't help you with that)

If the ci_password is valid use the password input from the user and generate the Laravel hash (using Hash::make('secret')). Store the new hash in the database and delete (set to NULL) the ci_password.

This way the passwords will be migrated one by one and you have to do nothing. And maybe, on one lucky day, all old hashes will be migrated and you can remove this logic and the column in the database.