How to let users login with google account in your Django website

created at May 27, 2021, 2:54 p.m. views: 0

To allow users login with google account in our django can be easily done by using django-allauth package.

this package can be installed by:

pip install django-allauth

Configurations

configurations have illustrated this very clear in the official docs, just do as this page does:

https://django-allauth.readthedocs.io/en/latest/installation.html

create a Credential in google APIs

now we need to go to google developer console,

in the left sidebar, find APIs & Services:

click on the Credentials option and we would comt to the Credential page:

at the top of this page, we could click to create a Credential:

three forms to be filled:

  1. Name: stdworkflow (This name is only used to identify the client in the console and will not be shown to end users.)
  2. Authorized JavaScript origins: https://stdworkflow.com (replace with your domain)
  3. Authorized redirect URIshttps://stdworkflow.com/accounts/google/login/callback/ (replace with your domain)

save, done. A Client ID and Client secret will be generated for us automatically.

write Client ID and Client secret into our database

method1: Interact with GUI

In our database, django-allauth created several tables for us with prefix socialaccount_, we need to modify two of them:

socialaccount_socialapp

socialaccount_socialapp_sites

socialaccount_socialapp

idprovidernameclient_idsecretkey
 GoogleGoogleYOUR_CLIENT_IDYOUR_CLENT_SECRET 
  • id: leave it empty, this would be generated by our database (this id will be used in socialaccount_socialapp_sites)
  • provider: Google
  • name: Google
  • client_id: the client_id google generated for us in APIs & Services
  • secret:the client_secret google generated for us in APIs & Services
  • key: we leave it empty

socialaccount_socialapp_sites

idsocialapp_idsite_id
 1 (if it is the fisrt one)1
  • id:leave it empty, this would be generated by our database
  • socialapp_id : the pk/id number generated in socialaccount_socialapp table
  • site_id: 1 (usually)

now all the configurations are done, you access your login page (generated by django-allauth), there would be a google option for login with google account. And if you have configured your Email sender in your project, you will be able to login (sign up) successfully.

method2: Command line

Bellow is an example of how to insert records into socialaccount_socialapp and socialaccount_socialapp_sites tables:

[user@localhost]$ mysql -u root -p
Enter password:

mysql> use DATABASENAME; # repalce "DATABASENAME" with your database name
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> insert into socialaccount_socialapp values (1, 'Google', 'Google', 'YOUR_CLIENT_ID', 'YOUR_CLENT_SECRET', '');
Query OK, 1 row affected (0.01 sec)

mysql>
mysql> insert into socialaccount_socialapp_sites values (1, 1, 1);
Query OK, 1 row affected (0.01 sec)

mysql> exit
Bye
Please log in to leave a comment.