You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Create a `UserManager` class, that conforms to the `UserManagerType` protocol. Don't forget about the
34
+
35
+
```swift
36
+
importUserManagerType
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
+
typealiasUserType= 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
+
publicstaticvar currentUser: UserType? { getset }
52
+
53
+
/// Get or set the token string
54
+
publicstaticvar token: String? { getset }
55
+
56
+
/// Logout current user and set the object with the key `User` to nil and optionally set token to nil.
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
+
iflet 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 niltoken (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 forthat (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