PyBlog — Authentication
Content
- Project Structure
- Firebase Setup
- Authentication
- Profile [WIP]
- Links [WIP]
- 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.
The above code will show an option to user for authentication type.
Now the flow is divided into three,
> SignUp
It will call _sign_up_user()
function,
_user_input.get_user_name()
is nothing but a call to PyInquirer to get a user input for the required details. For example
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.
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
Now if you see
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.
Now we have user_details
and platform_details
, let’s create the user on the Firebase.
The above will create a user with all the details required on Firebase Authentication
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
> 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
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,
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
And now if the user type y
then the user is logged out using the below logic,
And in _firebase.logout_user()
we simply do
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.