Flutter google sign in authenticate django social auth for google

Viewed 1358

I am creating a flutter android app which uses google sign in. Once logged in, I recieve accesstoken and idtoken. I want to use this token to authenticate my backend which uses django social auth and

  1. Login and return the authoken, if the user has already signed up, or
  2. Register the user , login and return the user id and authtoken.

Is this possible ? If so please suggest any documents online or please explain how should I approach this.

2 Answers

Over the years of doing this again and again, I found the solution below works well for me. It creates clear understanding of who is doing what.

Basically, you need:

  1. Django Rest framework-backed token authentication for normal API requests. Mostly your app works on this. Link: https://www.django-rest-framework.org/api-guide/authentication/#tokenauthentication
  2. Google or Facebook or any other login to issue an auth token in 1. Thus effectively FB/ Google shortcuts the process of typing in username and password.

This is achieved via the flow below:

  1. New user comes in and signs in via FB/ Google
  2. You get Fb/Google token and send it to your backend
  3. You verify the validity of the token. Re-obtain user name and email from G/FB from the backend. Use these details to create a user account in your backend. DO NOT USE email provided from front-end for account creation (assuming email is your primary unique user identifier)

NOTE: Don't forget to check if account already exists. If it does, this is a returning user/ login and not a new user. In this case, validate and return valid Django Rest Token

  1. Once 3 is complete, issue a Django REST framework Token in response to the request made in 3.
  2. After 4, you have a token in your app. Use this token for normal requests.

Happy coding! Happy to answer follow-up questions.

it is possible,first you have to create your api using django Rest Framework,the link below can help you to create your backend and set a token for every user:

https://dev.to/amartyadev/flutter-app-authentication-with-django-backend-1-21cp

then you have to add social authentication to your backend,you can write it yourself or using link below to use library :

https://github.com/RealmTeam/django-rest-framework-social-oauth2

after this approach you have to create your flutter app,the below link is a useful resource to connect your backend and your flutter app :

https://www.asapdevelopers.com/flutter-login-app-with-python-backend/

Related