Dearmadman used larastarscnsocial to solve the problem of third-party account login integration in laravelsociete. after obtaining user information, what should he do? How can I bind multiple social accounts to the same account? In this article, we will discuss how to integrate login... Dearmadman uses Laravel social networks to solve the issue of third-party account login integration by using larastarscn/social networks. after obtaining user information, what should we do? How can I bind multiple social accounts to the same account? In this article, let's discuss the integration login issue.
Initially
At first, when we only need to integrate a single social login, we may add open_id or github_id similar attributes to the user model to quickly complete the task, so in the database, we need to add corresponding fields to the table. This is a quick and effective way to complete tasks.
However, what should we do if we need to integrate one or more types of social logins when there are more demands? Do we have to add fields in the table structure as willfully as possible?
Schema::table('users', function ($table) { $table->string('github_id'); $table->string('douban_id');});
This obviously violates the open and closed principle. if we do this, we can imagine that when multiple data tables are integrated with one login type, we need to make a correction to the data table structure, when you log on to the authorization callback for verification, you need to add an integration-driven matching process with the Field query.
What should I do?
Ideas
In this case, does the User table have too many capabilities and should it waste its energy to manage these social identities? How can we arrange SocialiteUser to specifically manage the relationship between users and social accounts? We need to design a scalable solution to manage social login with different drivers, so we can easily design this table structure:
- socialite_users - id - user_id - driver - open_id
What are the responsibilities of SocialiteUser? Obviously, it is mainly used to maintain the relationship between the social login identity and the user model. 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 authorized login process, the user agrees to authorize, and the third-party application will redirect to the callback route. in the callback route, the social network will actively request to obtain user information and map the user's social IDUser
The id attribute of the model.
Then, we can match the driver ID and the user's social ID in the callback route to check whether the bound user exists in the database. If a user exists, use the matched user to log on. if the user does not exist, generate a user and append the social account information to the user. Then use the newly generated account to log on.
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 case, if you need a new integration for social login, you can directly configure the driver without making any changes to other code.
PS: You are welcome to pay attention to the topic of Laravel in the short book and the contribution of articles related to Laravel. The author's knowledge and skills are limited. if you have a better design scheme, you are welcome to discuss and exchange, if any error occurs, please criticize and correct it. thank you for your consideration :)
Reply content:
In Laravel social networks, Dearmadman uses larastarscn/social networks to solve the problem of third-party account login integration. what happens after obtaining user information? How can I bind multiple social accounts to the same account? In this article, let's discuss the integration login issue.
Initially
At first, when we only need to integrate a single social login, we may add open_id or github_id similar attributes to the user model to quickly complete the task, so in the database, we need to add corresponding fields to the table. This is a quick and effective way to complete tasks.
However, what should we do if we need to integrate one or more types of social logins when there are more demands? Do we have to add fields in the table structure as willfully as possible?
Schema::table('users', function ($table) { $table->string('github_id'); $table->string('douban_id');});
This obviously violates the open and closed principle. if we do this, we can imagine that when multiple data tables are integrated with one login type, we need to make a correction to the data table structure, when you log on to the authorization callback for verification, you need to add an integration-driven matching process with the Field query.
What should I do?
Ideas
In this case, does the User table have too many capabilities and should it waste its energy to manage these social identities? How can we arrange SocialiteUser to specifically manage the relationship between users and social accounts? We need to design a scalable solution to manage social login with different drivers, so we can easily design this table structure:
- socialite_users - id - user_id - driver - open_id
What are the responsibilities of SocialiteUser? Obviously, it is mainly used to maintain the relationship between the social login identity and the user model. 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 authorized login process, the user agrees to authorize, and the third-party application will redirect to the callback route. in the callback route, the social network will actively request to obtain user information and map the user's social IDUser
The id attribute of the model.
Then, we can match the driver ID and the user's social ID in the callback route to check whether the bound user exists in the database. If a user exists, use the matched user to log on. if the user does not exist, generate a user and append the social account information to the user. Then use the newly generated account to log on.
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 case, if you need a new integration for social login, you can directly configure the driver without making any changes to other code.
PS: You are welcome to pay attention to the topic of Laravel in the short book and the contribution of articles related to Laravel. The author's knowledge and skills are limited. if you have a better design scheme, you are welcome to discuss and exchange, if any error occurs, please criticize and correct it. thank you for your consideration :)