Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Document Combine support for iOS #2198

Merged
merged 4 commits into from
Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Our `docs/lib/auth/menu.json` should reference the newly-created page at `docs/l
}
```

Our `docs/overauth.md` can look like this for now:
Our `docs/lib/auth/overview.md` can look like this for now:

```md
---
Expand Down
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,7 @@
"kinesisvideo",
"KMS-generated",
"Kotlin",
"Kylo",
"Lambda",
"LambdaExecutionRole",
"lambdafunction",
Expand Down
Binary file added docs/images/combine-xcode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 31 additions & 2 deletions docs/lib/auth/fragments/ios/device_features/10_rememberDevice.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
<amplify-block-switcher>

<amplify-block name="Listener (iOS 11+)">

```swift
_ = Amplify.Auth.rememberDevice() { result in
switch result {
func rememberDevice() {
_ = Amplify.Auth.rememberDevice() { result in
switch result {
case .success:
print("Remember device succeeded")
case .failure(let error):
print("Remember device failed with error \(error)")
}
}
}
```

</amplify-block>

<amplify-block name="Combine (iOS 13+)">

```swift
func rememberDevice() -> AnyCancellable {
Amplify.Auth.rememberDevice()
.resultPublisher
.sink {
if case let .failure(authError) = $0 {
print("Remember device failed with error \(authError)")
}
}
receiveValue: {
print("Remember device succeeded")
}
}
```

</amplify-block>

</amplify-block-switcher>
43 changes: 36 additions & 7 deletions docs/lib/auth/fragments/ios/device_features/20_forgetDevice.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
<amplify-block-switcher>

<amplify-block name="Listener (iOS 11+)">

```swift
_ = Amplify.Auth.forgetDevice() { result in
switch result {
case .success:
print("Forget device succeeded")
case .failure(let error):
print("Forget device failed with error \(error)")
func forgetDevice() {
_ = Amplify.Auth.forgetDevice() { result in
switch result {
case .success:
print("Forget device succeeded")
case .failure(let error):
print("Forget device failed with error \(error)")
}
}
}
```
```

</amplify-block>

<amplify-block name="Combine (iOS 13+)">

```swift
func forgetDevice() -> AnyCancellable {
Amplify.Auth.forgetDevice()
.resultPublisher
.sink {
if case let .failure(authError) = $0 {
print("Forget device failed with error \(authError)")
}
}
receiveValue: {
print("Forget device succeeded")
}
}
```

</amplify-block>

</amplify-block-switcher>
45 changes: 38 additions & 7 deletions docs/lib/auth/fragments/ios/device_features/30_fetchDevice.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,43 @@
<amplify-block-switcher>

<amplify-block name="Listener (iOS 11+)">

```swift
_ = Amplify.Auth.fetchDevices() { result in
switch result {
case .success(let fetchDeviceResult):
for device in fetchDeviceResult {
print(device.id)
func fetchDevices() {
_ = Amplify.Auth.fetchDevices() { result in
switch result {
case .success(let fetchDeviceResult):
for device in fetchDeviceResult {
print(device.id)
}
case .failure(let error):
print("Fetch devices failed with error \(error)")
}
case .failure(let error):
print("Fetch devices failed with error \(error)")
}
}
```

</amplify-block>

<amplify-block name="Combine (iOS 13+)">

```swift
func fetchDevices() -> AnyCancellable {
Amplify.Auth.fetchDevices()
.resultPublisher
.sink {
if case let .failure(authError) = $0 {
print("Fetch devices failed with error \(authError)")
}
}
receiveValue: { fetchDeviceResult in
for device in fetchDeviceResult {
print(device.id)
}
}
}
```

</amplify-block>

</amplify-block-switcher>
28 changes: 27 additions & 1 deletion docs/lib/auth/fragments/ios/getting_started/40_fetchSession.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<amplify-block-switcher>

<amplify-block name="Listener (iOS 11+)">

```swift
func fetchCurrentAuthSession() {
_ = Amplify.Auth.fetchAuthSession { result in
Expand All @@ -9,4 +13,26 @@ func fetchCurrentAuthSession() {
}
}
}
```
```

</amplify-block>

<amplify-block name="Combine (iOS 13+)">

```swift
func fetchCurrentAuthSession() -> AnyCancellable {
Amplify.Auth.fetchAuthSession().resultPublisher
.sink {
if case let .failure(authError) = $0 {
print("Fetch session failed with error \(authError)")
}
}
receiveValue: { session in
print("Is user signed in - \(session.isSignedIn)")
}
}
```

</amplify-block>

</amplify-block-switcher>
41 changes: 40 additions & 1 deletion docs/lib/auth/fragments/ios/hubEvents/10_listen_events.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<amplify-block-switcher>

<amplify-block name="Listener (iOS 11+)">

```swift
override func viewDidLoad() {
super.viewDidLoad()
Expand All @@ -22,4 +26,39 @@ override func viewDidLoad() {
}
}
}
```
```

</amplify-block>

<amplify-block name="Combine (iOS 13+)">

```swift
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.

// Assumes `sink` is declared as an instance variable in your view
sink = Amplify.Hub.publisher(for: .auth).sink { payload in
switch payload.eventName {
case HubPayload.EventName.Auth.signedIn:
print("User signed in")
// Update UI

case HubPayload.EventName.Auth.sessionExpired:
print("Session expired")
// Re-authenticate the user

case HubPayload.EventName.Auth.signedOut:
print("User signed out")
// Update UI

default:
break
}
}
}
```

</amplify-block>

</amplify-block-switcher>
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<amplify-block-switcher>

<amplify-block name="Listener (iOS 11+)">

```swift
func resetPassword(username: String) {

_ = Amplify.Auth.resetPassword(for: username) { result in

do {
let resetResult = try result.get()
switch resetResult.nextStep {
Expand All @@ -17,4 +19,33 @@ func resetPassword(username: String) {
}
}
```

</amplify-block>

<amplify-block name="Combine (iOS 13+)">

```swift
func resetPassword(username: String) -> AnyCancellable {
Amplify.Auth.resetPassword(for: username)
.resultPublisher
.sink {
if case let .failure(authError) = $0 {
print("Reset password failed with error \(authError)")
}
}
receiveValue: { resetResult in
switch resetResult.nextStep {
case .confirmResetPasswordWithCode(let deliveryDetails, let info):
print("Confirm reset password with code send to - \(deliveryDetails) \(info)")
case .done:
print("Reset completed")
}
}
}
```

</amplify-block>

</amplify-block-switcher>

Usually resetting the password require you to verify that it is the actual user that tried to reset the password. So the next step above will be `.confirmResetPasswordWithCode`.
Original file line number Diff line number Diff line change
@@ -1,18 +1,54 @@
```swift
func confirmResetPassword(username: String,
newPassword: String,
confirmationCode: String) {
<amplify-block-switcher>

<amplify-block name="Listener (iOS 11+)">

```swift
func confirmResetPassword(
username: String,
newPassword: String,
confirmationCode: String
) {
_ = Amplify.Auth.confirmResetPassword(
for: username,
with: newPassword,
confirmationCode: confirmationCode) { result in
switch result {
case .success:
print("Password reset confirmed")
case .failure(let error):
print("Reset password failed with error \(error)")
}
confirmationCode: confirmationCode
) { result in
switch result {
case .success:
print("Password reset confirmed")
case .failure(let error):
print("Reset password failed with error \(error)")
}
}
}
```

</amplify-block>

<amplify-block name="Combine (iOS 13+)">

```swift
func confirmResetPassword(
username: String,
newPassword: String,
confirmationCode: String
) -> AnyCancellable {
Amplify.Auth.confirmResetPassword(
for: username,
with: newPassword,
confirmationCode: confirmationCode
).resultPublisher
.sink {
if case let .failure(authError) = $0 {
print("Reset password failed with error \(authError)")
}
}
receiveValue: {
print("Password reset confirmed")
}
}
```
```

</amplify-block>

</amplify-block-switcher>
Loading