Authenticating User on Django backend of flutter app
I was recently working on an app in flutter which uses google sign in and it was really a pain to get my backend to authenticate the users signing into the app using their Google accounts. So today, I am gonna show you how to do that. First of all, you have to get google signin working on your app. For that, there are many online tutorials which you can follow. I’ll just post my auth.dart code here which handles the authentication.
The above code is for the authentication class which handles all the authentication. The gSignin function is for signing in using gmail account and gLogout is for logging out. The verifyToken function is for token verification on your backend. The URL in verify token is the backend URL which you are trying to connect to. The backend will be sent the token and it will verify whether the token is correct or not and accordingly respond to any request. So this is how it works, whenever the app wants anything from the backend, it has to send the token and then the backend will verify the token and respond positively if the token is correctly verified.
This is my code for token verification in my Django backend :
In the above code, ky.json is the private key you get from the service accounts tab in firebase console. This file is used to talk to firebase remotely using your account.
One mistake I wouldn’t want anyone to repeat is that, when I was trying to get it to work, I was just printing the token in Flutter console and copy pasting it in python and trying to verify the pasted token. For some reasons, it doesn’t work that way. If you know why feel free to leave a comment.
Originally published at kode-map.blogspot.com.