# Nonstop Swift SDK

## Users

### Login

`login` should be called whenever a user Registers or Signs In via OneID, after the call to create or update the user in ShowMS.

Thus the flow is:

OneID Success -> ShowMS Success -> Nonstop Success -> Control Returned to Client

In order to call `login` create a `UserInput` model:

```swift
let userInput = UserInput(firstName: "Bill", externalId: "showms-user-id", email: "bill@gmail.com", birthday: "1988-01-19", swid: "{4430f840-e59e-4902-98fc-86b25225f6ee}")
```

* **firstName** - Should be the users first name as returned by OneID
* **externalId** - Should be the ShowMS User ID
* **email** - Email address from OneID, this will be securely hashed and it used to find the user in the admin for customer service scenarios
* **birthday** - Birthday as returned from OneID in the format 'YYYY-MM-DD'
* **swid** - The user's SWID as returned by OneID

Note that `externalId` is a required field however, all other fields are optional. If values are available they should be supplied; if they are not available, there is no issue with omitting them.

And pass this into the login function:

```swift
Nonstop.sharedInstance.login(userInput: userInput).subscribe { event in
    switch event {
        case .success (let createdUser):
            // Nonstop user successfully logged in
        case .error (let error):
            // Error logging in Nonstop user
    }
}.disposed(by: disposables)
```

If the user should gain a reward by logging in then the SDK will play the appropriate animation before handing control back to the calling app.

Note: You can optionally pass an additional parameter:

```swift
Nonstop.sharedInstance.login(userInput: userInput, suppressAnimation: true).subscribe { event in
```

To suppress any Reward Animation as a result of login. The main use case for this is when already logged in existing users need to be logged into Nonstop in the background as part of the upgrade path.

On error the client should message the user and offer to retry.

The `createdUser` object returned with Success will have details of logged in user's:

* `id` - This is the externalId supplied in the login call
* `name` - The user's name as suppled in the login call
* `points` - The current number of points the user has
* `isNew` - A bool indicating whether the user has used Nonstop before (e.g. on another platform) or whether this is their first time using Nonstop. This can be used to trigger UI or messaging changes as appropriate.

But there should be no need to store this on the client app. While logged in the current user can be accessed via the SDK at all times via:

### Is Connected

You can ask whether there is a currently logged in user via:

```swift
Nonstop.sharedInstance.isConnected
```

This should be used for Session Management scenarios.

### Current User

You can access the currently logged in User via:

```swift
Nonstop.sharedInstance.currentUser
```

### Logout

`logout` should be called whenever a user logs out of OneID.

```swift
Nonstop.sharedInstance.logout().subscribe { event in
    switch event {
        case .success:
            // Nonstop user successfully logged out
        case .error (let error):
            // Error logging out
    }
}.disposed(by: disposables)
```

No action is required for either Success or Error, other than logging if required.
