A
A
Andrew2015-10-15 19:51:14
PHP
Andrew, 2015-10-15 19:51:14

How to store user authorization data through a social network in the database?

Hello, a mobile application and a server part for it are being written, it is planned to enable the user to link his account through several social networks. networks right away, the question is, how is it best and most correct to store it in the database? What options do I see:
1) create several additional fields for the user in the database for each social network
2) create several additional tables for each social network and bind by user id.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin, 2015-10-15
@ntzch

We use the following table schema:

Table "public.social_account"
   Column    |              Type              | Modifiers
-------------+--------------------------------+-----------
 user_id     | integer                        | not null
 network     | character varying(255)         | not null
 external_id | character varying(255)         | not null
 created_at  | timestamp(0) without time zone | not null
 updated_at  | timestamp(0) without time zone | not null
 id          | uuid                           | not null
Indexes:
    "social_account_pkey" PRIMARY KEY, btree (id)
    "unique_social_account" UNIQUE, btree (network, external_id)
    "idx_f24d8339a76ed395" btree (user_id)
Foreign-key constraints:
    "fk_f24d8339a76ed395" FOREIGN KEY (user_id) REFERENCES app_user(id) ON DELETE CASCADE

That is, for each binding, a record is stored that contains the user_id, the name of the social network (google/vk/facebook/etc) and the id of this social network (all have an arbitrary format).
Thus, when a user authenticates through one of these social networks, the user_id is first looked up in the external_id + network binding. If user_id is found - authenticate the current user as this user_id. If not, we receive an email from the social network and use it to look for the user in the user table. If found, then create an entry in the social_account and authenticate the user. If nothing was found (first visit), then create a user and create an entry in social_account.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question