Intro
Hello everyone. Topic that I’d like to discuss today is quite simple. What is it about and why do I need to talk about it? Usually when user gets registered / logged in there is no strict way to describe this state. Obviously when the app authorized the user we can retrieve bunch of information about it like: APIClient.sharedClient().token != nil
or UserStore.sharedStore().currentUser != nil
, or performing a DB query where User.isCurrent == true
or all this at the same time. Literally there are lot of runtime attributes indicating about logged in user but nothing that combines it into a single object. Basically User Session is that missing abstraction that gives us a way to definitely describe current user state: logged in or not. I belive most of readers faced with logout case, whe you have stop all running services, probably remove user’s disk data, etc. I saw many places to handle logout cleanup but none of them feels comfortable: App Delegates, UIViewController that handles logout and so on. User Session is something that:
- an object that definitely describes current login state
- gives us a correct place to both start and stop services
- we’ll talk today
Implementation
User Session abstraction neither new topic or a rocket science. It is used in most of the backend services, however it didn’t receive the attention it deserves in iOS-community (IMO). For core implementation we need 3 classes:
- User session itself
- User session controller to open, close and restore sessions as well as keep strong reference to current session
- User session prototype to encapsulate any login data (user, token, etc) before new session is opened
User Session Prototype
Lets start from user session and its prototype. Assume that both sign in / up request returns the following response:
1 2 3 4 5 6 7 8 9 |
|
In order to be passed around this JSON it needs to be encapsulated into an object, not a plain Dictionary
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
UserSessionPrototype
contains just 2 properties:
identifier
of the user, which will be used to buildUserSession.identifier
. This is quite useful for preserving of session’s data between logout / login actionsuserInfo
: simple Dictionary to keep all user data for session bootstrap
Prototype not necessarily should be so simple. It may combine any login data that is required for correct session bootstrap such as tokens, session trial limit and so on.
User Session
What about UserSession
? Obviously it should support init
with prototype, restoration. What else? It needs to be able to bootstrap initial state and place to handle start & stop of some services (timeline background update, etc).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
Now lets talk about session restoration: in order to perform correct init you’re free to check anything. In my real-world case I checked whether keychain has a valid token for passed identifier.
What else we need to add to the session? Probably sort of a flag to definitely describe session’s current state. It can be effectively used for further development like auth token refresh, logout and so on:
- Undefined - session hasn’t been opened yet
- Opened - default session state
- Closed - session has been closed
- Invalid - user session bootstap failed or auth token has been invalidated
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
User Session Controller
Now it is time to add controller to keep an eye on active session. As was mentioned above main responsibilities are:
- Open a new session as a result of sign up / in
- Close session
- Restore session
- Keep a strong reference to the opened session
1 2 3 4 5 6 7 8 9 10 11 |
|
Let’s start from opening of a new session. In this example I’m not going to cover full flow of sign up / in since it is quite straightforward. For now lets assume, that we have APIClient
, that talks to the backend. Result of login operation is a UserSessionPrototype
that we implemented earlier or NSError
in case of network error:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
It is generally preferable to return an object that can cancel async operation.
Void
as a return type of any async operation should be ommited, but to keep our example simple I’m ignoring this rule
Session closing is a oneline function:
1 2 3 |
|
Now it is time for restoration! What do we need for it? Obviously to preserve opened user session’s identifier between apllication launches. Best place to update persistent identifier value is userSession
’s didSet. For storage lets go with NSUserDefaults:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
Also we need to add initialization of ‘UserSessionController’ into the root of the app:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Extras
Ok it seems that we’re done with basic implementation. Is there anything useful we can add out of the box? User’s data management convenience! By a simple path utility added to the session we can easily distinguish any user’s files physically, so you no longer have to care about possible collisions.
Briefly our Documents
and ‘Caches’ path will be dependand on UserSession.identifier
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Adding this to the UserSession
:
1 2 3 |
|
Now when you’re going to setup CoreData stack there is a great utility for “path consulting”. For sure, final desicion of where to store any user’s data is up to the implementation but existence of such path utility pushes developer to do it right
;)
Summary
UserSession
is a great %your_app_name% citizen:
- amazing starting point to stop using sharedManagers and take control over of your services!
- gives you entry point to perform any neccessary logout cleanup
- helps to store your data more organized
- can indicates about invalidated auth tokens by updating
state
value - abstraction that I’d like to know about few years earlier
Check out sample source code on the github!
Special thanks for help to Alex Denisov and Paul Taykalo!