Mobile (FE) and BE social authentication using React-Native, Django and PostgreSQL
This is a general example for authentication a of mobile app (FE), using third party (Facebook, Google...) and a BE service (Djange-Rest-Framework). Will use google as an example, process is the same for all third party authenticatrion.
The highlevel flow is (App should already be registered - have app id):
- Mobile App - user login to third party
- Third party supplies a token
- Pass token to BE
- BE validates the token with third party, retrieves requested data.
- BE supplies a new token for session.
- App, user, uses token for communicating with backend API.
- Python >= 3.6
- virtual env:
- python.exe -m venv /venv/path
- source /venv/path/Scripts/activate
- Install Django, Django-Rest-Famework
- pip install django
- pip install django-rest-framework
- Install PostgreSQL
- Install react-native
The examples are based on the following links:
https://www.django-rest-framework.org/tutorial/4-authentication-and-permissions/
https://www.django-rest-framework.org/api-guide/authentication/
The basic step for almost all apps is to create a user.
Luckily Django rest framework (drf) already supports such requirement.
- Create a PostgreSQL DB. In my opinion, easiest way is to use pgadmin, lets call it 'authentication_example', also create a user for that db, lets call it 'auth_user_admin' with psw: '123456'
- Create a simple django app - I am using Eclipse with Django SDK, but you can use django command list as well: https://www.django-rest-framework.org/tutorial/quickstart/
- Install python pgsql: pip install psycopg2
- Configure django app to worj with postgreSQL, changed the default sql section to pgsql:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'authentication_example',
'USER': 'auth_user_admin',
'PASSWORD': '123456',
'HOST': 'localhost',
'PORT': '',
}
} - Update DB:
- python manage.py makemigrations
- python manage.py migrate