Skip to content
This repository was archived by the owner on Mar 10, 2022. It is now read-only.

Commit a761fbe

Browse files
committed
Added usage instructions to README
1 parent 49db08c commit a761fbe

1 file changed

Lines changed: 81 additions & 2 deletions

File tree

README.md

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
[![Carthage Compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
55
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/nodes-ios/user-manager-type/blob/master/LICENSE)
66

7+
`UserManagerType` is a protocol that makes it easier to implement a user manager system for your iOS app.
8+
9+
710
## 📝 Requirements
811

912
* iOS 8.0+
10-
* Swift 2.0+
13+
* Swift 3.0+
1114

1215
## 📦 Installation
1316

@@ -22,4 +25,80 @@ github "nodes-ios/user-manager-type" ~> 2.0
2225
> `github "nodes-ios/user-manager-type" == 1.2.0`
2326
>
2427
> **Swift 2.2**
25-
> `github "nodes-ios/user-manager-type" == 1.1.2`
28+
> `github "nodes-ios/user-manager-type" == 1.1.2`
29+
30+
31+
## 💻 Usage
32+
33+
Create a `UserManager` class, that conforms to the `UserManagerType` protocol. Don't forget about the
34+
35+
```swift
36+
import UserManagerType
37+
```
38+
39+
Specify the `UserType` typealias to your User class. In the case of this example, the class/struct that we use for a user is called `User`.
40+
41+
```swift
42+
typealias UserType = User
43+
```
44+
45+
And that's it. You now have a fully working UserManager, which can store the current user, the current user's token, log out the current user or check if the current user is logged in.
46+
47+
Have a look at the `UserManagerType`'s interface:
48+
49+
```swift
50+
/// Get or set the current user object
51+
public static var currentUser: UserType? { get set }
52+
53+
/// Get or set the token string
54+
public static var token: String? { get set }
55+
56+
/// Logout current user and set the object with the key `User` to nil and optionally set token to nil.
57+
public static func logoutCurrentUser(clearToken clear: Bool)
58+
59+
/// Check if someone is logged in
60+
public static var isLoggedIn: Bool { get }
61+
```
62+
## 🔎 Examples
63+
You probably need to **send an auth token in all your API request headers**. You can get the token like this:
64+
65+
``` swift
66+
let auth = UserManager.token ?? ""
67+
```
68+
69+
Anywhere in the code where you need to **access the currently logged in user**, you can do this:
70+
71+
```swift
72+
if let currentUser = UserManager.currentUser {
73+
// do your stuff here
74+
}
75+
```
76+
77+
After calling your API's login method, if it succeeded, make sure you **set the token on the `UserManager`**. Setting it only on the `UserManager.currentUser` won't work.
78+
79+
```swift
80+
ConnectionManager.login(username: username, password: password, completion: { (response) in
81+
switch response.result {
82+
case .success(let user):
83+
UserManager.currentUser = user
84+
UserManager.token = user.token
85+
// do whatever else needs to be done here
86+
87+
case .failure(let error):
88+
// handle the error
89+
})
90+
```
91+
92+
There are some REST APIs that always return the token on the user object, in all the calls. For those, setting the token separately on the `UserManager` might seem redundant. But other REST APIs only send the token in the response to the login method. If there's a `/me` API call that returns the current user, but without his token, then in the `UserManager`, the `currentUser` would have an empty or nil token (dependin on how you deserialize it). We chose to have the token stored directly on the `UserManager` to accomodate both situations.
93+
94+
To **log out the current user**, you can do this:
95+
```swift
96+
UserManager.logoutCurrentUser(clearToken: true)
97+
```
98+
This will clear the current user and the token. You will still need to handle the UI for that (i.e. take the user back to the login flow)
99+
100+
## 👥 Credits
101+
Made with ❤️ at [Nodes](http://nodesagency.com).
102+
103+
## 📄 License
104+
**UserManagerType** is available under the MIT license. See the [LICENSE](https://github.com/nodes-ios/user-manager-type/blob/master/LICENSE) file for more info.

0 commit comments

Comments
 (0)