简体   繁体   中英

Laravel 5 SQLSTATE[23000] users_email_unique

I am implementing a social authentication on my website with laravel 5.

I successfully logged in a couple of users but now for some very strange reasons it doesn't work anymore..

When I try to log in a new user I have this error coming up:

QueryException in Connection.php line 624:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'users_email_unique' (SQL: insert into `users` (`name`, `profile_picture`, `facebook_id`) values (Hwan, https://graph.facebook.com/v2.4/1701160536770162/picture1701160536770162type=normal, ?))

But this user has never been registered in the DB before !!

I tried with other users, all the same...

But if I remove an existing FB user from the DB and try again, it works !!

Here is my controller:

class AccountController extends Controller {
/**
 * Redirect the app to the social provider
 *
 * @return SNS token and user data
 */
public function redirectToProvider($provider) {
    return Socialize::with($provider)->redirect();
}
/**
 * Callback handler
 *
 * @return redirect to index
 */
public function handleProviderCallback($provider) {
    $user   = Socialize::with($provider)->user();

    // get the sns user data
    $id         = $user->getId();
    $name   = $user->getName();
    $avatar = $user->getAvatar();

    // get the user provider id form the DB
    $users = DB::table('users')->where($provider.'_id', $id)->get();

    // check if the record exists
    if(empty($users)){
        DB::table('users')->insert(
            ['name' => $name,'profile_picture' => $avatar,$provider.'_id' => $id]
        );
        $users = DB::table('users')->where($provider.'_id', $id)->get();
    }

    foreach ($users as $user)
    {
        $userID = $user->id;
    }
    Auth::loginUsingId($userID);
    {
        return redirect()->to('home');
    }
}

}

And my routes:

Route::get('connect/{provider}', 'AccountController@redirectToProvider');
Route::get('account/{provider}', 'AccountController@handleProviderCallback');

And my user schema:

 Schema::create('users', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password', 60);
        $table->string('profile_picture');
        $table->text('facebook_id');
        $table->rememberToken();
        $table->timestamps();
    });

Any help is greatly appreciated

Thank you

You have a unique constraint on your email field but you do not appear to be inserting an email address. After one user is inserted without an email address, no other users can be signed up without an email address.

You will not be able to have two empty strings in the email column of your database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM