Dearmadman in the Laravel socialite to explain the use of larastarscn/socialite to solve the third-party account login integration problem, then after the acquisition of user data? How to bind the same account with multiple social accounts? In this article, let's explore the point of integrated login.
Initially
At first, when we only needed to integrate a single social login, we might simply add open_id or github_id similar attributes to the user model in order to quickly complete the task, and in the database we would need to include the corresponding fields in the table. This is a quick and efficient way to accomplish a task.
But what if more needs come and we need to integrate one or more social logins? Do we have to be headstrong? Add the appropriate fields to the table structure?
Schema::table('users', function ($table) { $table->string('github_id'); $table->string('douban_id');});
This is obviously against the principle of open closure, if we do so, then we can imagine that when each multiple integration of a login, we need to make a correction to the data table structure, and in the login authorization callback verification, but also to add an integrated driver and field query matching operations.
So what should I do?
Idea
In this case, does the User table take on too much power and should it waste its energy to manage these social identities? So why don't we schedule socialiteuser to specifically manage the relationship between users and social accounts? We need to design a scalable scheme to manage social logins for different drives, so we can easily design this table structure:
- socialite_users - id - user_id - driver - open_id
What responsibilities should Socialiteuser have? Obviously, it is primarily used to maintain the relationship between social login identities and user models. Then it should have the following capabilities:
The following is a simple code demonstration:
where ([' Driver ' = $driver, ' open_id ' = ' $openid])->first (); Return $finder? $finder->user: $finder; }/** * Get related user model. * * @return/app/user| | NULL */Public function User () {return $this->belongsto (' App\user '); }/** * Save a new record. * * @param $userId integer * @param $driver String * @param $id String * @return/app/socialiteuser */Public Function Saveone ($userId, $driver, $id) {return $this->create ([' user_id ' =] $userId, ' driver ' = $driver, ' open_id ' + $id]); }}
Use
In the authorization login process, the user agrees to authorize, the third-party app redirects to the callback route, the callback route socialite proactively requests the user profile, and maps the user's social ID ID to User
the model's ID attribute.
Then we can match the driver identity and the user's corresponding social ID ID in the callback route to the user who already has the binding in the query library. If it exists, use the matching user login directly, and if it does not exist, generate a user and attach social account information to the user. Then sign in with the newly generated account.
redirect(); } public function handleProviderCallback($driver) { $user = Socialite::driver($driver)->user(); $model = new User(); $socialiteUser = new SocialiteUser(); $finder = $socialiteUser->getUser($driver, $user->id); if (! $finder) { $finder = $model->generateUserInstance(); $finder->save(); $socialiteUser->saveOne($finder->id, $driver, $user->id); } Auth::login($finder); return view('home'); }}
In this sense, if the need for a new social login integration, then there is no need to make other code changes, directly configure the driver can be.
PS: Welcome to focus on Jane Laravel special topics, but also welcome the contribution of Laravel related articles:), the author's knowledge and skill level is limited, if you have a better design program welcome discussion exchange, if there is wrong place also please criticize, thank you:)
Reply content:
Dearmadman in the Laravel socialite to explain the use of larastarscn/socialite to solve the third-party account login integration problem, then after the acquisition of user data? How to bind the same account with multiple social accounts? In this article, let's explore the point of integrated login.
Initially
At first, when we only needed to integrate a single social login, we might simply add open_id or github_id similar attributes to the user model in order to quickly complete the task, and in the database we would need to include the corresponding fields in the table. This is a quick and efficient way to accomplish a task.
But what if more needs come and we need to integrate one or more social logins? Do we have to be headstrong? Add the appropriate fields to the table structure?
Schema::table('users', function ($table) { $table->string('github_id'); $table->string('douban_id');});
This is obviously against the principle of open closure, if we do so, then we can imagine that when each multiple integration of a login, we need to make a correction to the data table structure, and in the login authorization callback verification, but also to add an integrated driver and field query matching operations.
So what should I do?
Idea
In this case, does the User table take on too much power and should it waste its energy to manage these social identities? So why don't we schedule socialiteuser to specifically manage the relationship between users and social accounts? We need to design a scalable scheme to manage social logins for different drives, so we can easily design this table structure:
- socialite_users - id - user_id - driver - open_id
What responsibilities should Socialiteuser have? Obviously, it is primarily used to maintain the relationship between social login identities and user models. Then it should have the following capabilities:
The following is a simple code demonstration:
where ([' Driver ' = $driver, ' open_id ' = ' $openid])->first (); Return $finder? $finder->user: $finder; }/** * Get related user model. * * @return/app/user| | NULL */Public function User () {return $this->belongsto (' App\user '); }/** * Save a new record. * * @param $userId integer * @param $driver String * @param $id String * @return/app/socialiteuser */Public Function Saveone ($userId, $driver, $id) {return $this->create ([' user_id ' =] $userId, ' driver ' = $driver, ' open_id ' + $id]); }}
Use
In the authorization login process, the user agrees to authorize, the third-party app redirects to the callback route, the callback route socialite proactively requests the user profile, and maps the user's social ID ID to User
the model's ID attribute.
Then we can match the driver identity and the user's corresponding social ID ID in the callback route to the user who already has the binding in the query library. If it exists, use the matching user login directly, and if it does not exist, generate a user and attach social account information to the user. Then sign in with the newly generated account.
redirect(); } public function handleProviderCallback($driver) { $user = Socialite::driver($driver)->user(); $model = new User(); $socialiteUser = new SocialiteUser(); $finder = $socialiteUser->getUser($driver, $user->id); if (! $finder) { $finder = $model->generateUserInstance(); $finder->save(); $socialiteUser->saveOne($finder->id, $driver, $user->id); } Auth::login($finder); return view('home'); }}
In this sense, if the need for a new social login integration, then there is no need to make other code changes, directly configure the driver can be.
PS: Welcome to focus on Jane Laravel special topics, but also welcome the contribution of Laravel related articles:), the author's knowledge and skill level is limited, if you have a better design program welcome discussion exchange, if there is wrong place also please criticize, thank you:)