Swiftyinsta: Ask about credentials.code

Created on 9 Oct 2019  ·  19Comments  ·  Source: TheM4hd1/SwiftyInsta

Hello,
Please, tell me. I have a problem.

With request in func login, I get an SMS with the code, that I registered in credentials.code = "121332".
I can’t send this code back. How to do it? What am I doing wrong?

This is my code

class ViewController: UIViewController {

let handler = APIHandler()    
override func viewDidLoad() {
    super.viewDidLoad()
    login()
}

func login() {
    var credentials = Credentials(username: "username", password: "password", verifyBy: .text)
    credentials.code = "121332"

    handler.authenticate(with: .user(credentials)) { [weak self] in
        switch $0 {
        case .success(let response, _):
            print("Login successful.")
            guard let key = response.persist() else { return print("`Authentication.Response` could not be persisted.") }
            UserDefaults.standard.set(key, forKey: "current.account")
            UserDefaults.standard.synchronize()

            self?.getCurrentUser()
        case .failure(let error):
            if error.requiresInstagramCode {
            } else {
            }
        }
    }
}

func getCurrentUser() {
    self.handler.users.user(.me) { ( result: Result<User?, Error>) in
        switch result {
        case .success(let user):
            let username = user?.name ?? "John Doe"
            print("success, username: \(username)")
        case .failure(let error):
            print("failure, error: \(error.localizedDescription)")
        }
    }
}

}

bug help wanted

Most helpful comment

Sorry I haven't managed to post an example yet, I've been really busy.
I hope I can post one soon.

All 19 comments

You need to move credentials.code = "121332" after the authentication handler returns .failure(let error).
Doing it async inside the if error.requiresInstagramCode { } might be a good option.

Problem still exists
https://prnt.sc/pgyf15
apparently does not send this code

Try without the breakpoint. Cause this way you're actually stopping the code as it reaches that row, so it can't propagate any request as credentials.code changes. :blush:
If it still doesn't change anything, try retaining credentials too (although it shouldn't be an issue from what I can see from your snippet, tbh).

https://prnt.sc/pgynah
Problem still exists. I would like to draw your attention, I pass the credentials to .user, because if there is .credentials (as in the example), then an error occurs

.user is the correct way.
.credentials was used in development and I forgot to change it. Thanks for pointing out. I'll change it right now.
I still don't get the error though cause I'm testing it my way and everything works as intended 🤔

Since I can't see it in your last screenshot, does it really not work with persisting the credentials that way, and then updating code after it returns the Error requiring it?
Cause that appears to be the only thing you're doing different, tbh 😞

If I us .credentials, i have this error https://prnt.sc/pgyvlx
I posted all my code in the main message.
Am I doing something wrong?

how do you test sending a message back?

.user is the right one, .credentials was kept in the documentation by mistake.
Move your declaration of credentials below the handler one. As a "retained" property.
It should work then.

let handler = APIHandler()
let credentials = Credentials( … )

func login() {
  …
}

its all code, there is an error anyway (

import UIKit
import SwiftyInsta

class ViewController: UIViewController {
let handler = APIHandler()
var credentials = Credentials(username: "username", password: "password", verifyBy: .text)

override func viewDidLoad() {
    super.viewDidLoad()
    login()
}

func login() {

    handler.authenticate(with: .user(credentials)) { [weak self] in
        switch $0 {
        case .success(let response, _):
            print("Login successful.")
            guard let key = response.persist() else { return print("`Authentication.Response` could not be persisted.") }
            UserDefaults.standard.set(key, forKey: "current.account")
            UserDefaults.standard.synchronize()
            self?.getCurrentUser()
        case .failure(let error):
            if error.requiresInstagramCode {
                self!.credentials.code = "178063"
            } else {
            }
        }
    }
}

func getCurrentUser() {
    self.handler.users.user(.me) { ( result: Result<User?, Error>) in
        switch result {
        case .success(let user):
            let username = user?.name ?? "John Doe"
            print("success, username: \(username)")
        case .failure(let error):
            print("failure, error: \(error.localizedDescription)")
        }
    }
}

}

And what is the error this time? @rmelnik7777

2019-10-09 17:32:25.333892+0300 TestInsta[9747:281440] [] nw_connection_receive_internal_block_invoke [C1] Receive reply failed with error "Operation canceled"

https://prnt.sc/pgzldk

That actually looks like some sort of _Xcode_ warning, not something SwiftyInsta generated 🤔

I have a similar problem. It is not clear how to get into the failure block to set an authorization code from SMS. Can you help with an example?

Hello I have same problem too. Can you share a working sample application for two factor auth ?

@sbertix will post a clear example soon.
be patient.

Sorry I haven't managed to post an example yet, I've been really busy.
I hope I can post one soon.

I'm totally new with swift but maybe I found the bug:

  • Here, you create a mutable copy of the user (instance of Credentials) and set the value of the handler property.
  • But in this line, it's the original instance of Credentials, so the value of handler property is not set. The authentification.code() function is not called.

I didn't have a chance to investigate it further but it might as well be. Apparently on the current commit Credentials are declared as a struct and not a class, which was the premise of the all the passing by reference thing.
It's apparently something I either fixed along the way in my local version, or some changes that I pushed to the master along the way, or possibly not even the issue per se.
I'll try to review everything for #96, but I haven't had any time as of lately. Apologies.
@Lyusan

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sbertix picture sbertix  ·  27Comments

sbertix picture sbertix  ·  8Comments

effecttwins picture effecttwins  ·  16Comments

reefer picture reefer  ·  18Comments

biox86 picture biox86  ·  12Comments