PyBlog — Authentication

Pradyot Prakash
4 min readApr 12, 2022

--

Photo by Micah Williams on Unsplash

Content

  1. Project Structure
  2. Firebase Setup
  3. Authentication
  4. Profile [WIP]
  5. Links [WIP]
  6. Final Remark [WIP]

So we need to know before if the user wants to create an account or want to login.

Let’s give an option in the console for the user to chose from.

Start authentication
User Authentication Option

The above code will show an option to user for authentication type.

Flow till now
Flow till now

Now the flow is divided into three,

> SignUp

It will call _sign_up_user() function,

Create user
SignUp User

_user_input.get_user_name() is nothing but a call to PyInquirer to get a user input for the required details. For example

Example for user input
Ask for user name

Let’s have a look at _firebase.upload_file(path=_photo_path), which uploads the picture to Firebase Storage from the path which is provided by user.

Upload image to Firebase Storage
Upload image to Firebase Storage

It returns the URL of the picture when the upload is successful.

What is get_platform_details()? — Sometimes when we are creating a user profile we might be needing some extra details from the system user is using, for marketing, security or any other reason. So it does the same thing

Get basic details of the system
Get Platform Details

Now if you see

Get the IpAddress using REST call
Get IP address

this will answer why we need urlib3 package. It makes a GET request to the Constants.URLs.IP_ADDRESS host and get the required details.

How to make a request?
Making A GET request

Now we have user_details and platform_details, let’s create the user on the Firebase.

Create user using auth
Create User

The above will create a user with all the details required on Firebase Authentication

Add new user details on Firestore
Save User Details to Firestore

After creating the user, details are also needed to be stored in Firestore for future user.

After doing all the above if you go to Firebase Authentication dashboard you will see a new user created there with email address and phone number and also the details which we fetched during creation will be store in Firestore to the user UID assigned to the user, like below

Authentication
How Firestore looks like?
Firestore

> Login

Login is straight forward, either we need an email address or phone number to login the user. Please add extra security for Login, this is just for demo purpose I have used only email and phone number

Login the user
Initiate Login
Update user details on Firestore
Update details on Firestore

After fetching the user details from Firebase Auth the new details has to be updated on Firestore as well, so the above code does it. Like the system might have change or also the last logged in time.

This is all what is required for authenticating the user. See the below image for a quick view of login,

Flow till now
Flow till now

After user is authenticated, one option which is required is log out the user and make the current session invalid.

So if you see in the above picture, if the user press the down arrow and press enter it will ask for a confirmation like below

A confirmation before logging out the user
Logout Confirmation

And now if the user type y then the user is logged out using the below logic,

Logout code for confirmation
Logout Logic

And in _firebase.logout_user() we simply do

Updating user details in Firebase class
Firebase logout

Since, auth doesn’t have a logout option in firebase_admin I used _current_user object to see if a user session is currently active or not.

You can see in the previous code that every time a session is created _current_user is updated with new details. And that’s what helps us getting the current user details from auth.

Now let’s see how we will show the current authenticated user other options like Profile Details, Create/Save Blogs, etc.

This article will be updated as I get new questions and experience. If you have any new question please provide it in the comments, will add it to the list.

Follow for getting the notification when new articles are published.

Follow me on Twitter and GitHub.

--

--