Who are you?
Who, who, who, who?
Who are you?

Part 3 of refactoring my CLI App. Authorisation and Sessions!

Eliot Howes
Eliot’s Journey
Published in
3 min readSep 16, 2019

--

In this post I will be exploring how I refactored my code to authorise a user and keep track of them through a ‘session’. Unfortunately using the Command Line there isn’t really a way to do this in a fancy way…when I finally get round to recreating this app in Ruby on Rails I will demonstrate how this can be done much more effectively.

So for now…

As always let’s start by looking at the original code. As is the norm with a CLI App I had a run method which usually calls each method in term which are required for your app to run. As you can see from the code here I was only calling two methods, the second of which login_type then caused a chain of methods to call one another until eventually the user logged out…I don’t need to tell you now that this isn’t good OO programming.

To keep this short I will just focus on refactoring the user side of the app. First things first I have defined a Session Class . All our methods that belong to a user are now being defined in CliUserMethods Class and when it inherits < Session Class it has the following variable and methods:

When a new instance of CliUserMethods is created we now see that it has Session Class's @current_user variable. This means we can assign a user object to the @current_user variable to keep track of them during a session and then set @current_user to nil when they log out.

Now our methods in CliUserMethods are as follows to allow @current_user to be set:

Now to authorise a user throughout our app we can call the method logged_in? and whilst @current_user contains a User object , the method will return true and users will be allowed to continue doing what they please.

A nice short post today which demonstrates how you can authorise a user at login and then track them through a session as well as log them out at the end of a session.

I think I have probably reached a point now where to show further learning through this CLI app I will need to develop it into a Rails app. Over the next few weeks I will try and finish refactoring the code, create a video walkthrough and draw a line under my CLI app.

Hopefully the next time I write a blog it will be highlighting the interesting points around how this CLI app was transformed into an all singing, all dancing Ruby on Rails app.

Until next time…

--

--

Eliot Howes
Eliot’s Journey

Personal and Technical blog about all things software development. Full Stack Software Developer - London.