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

Ask about credentials.code #115

Closed
rmelnik7777 opened this issue Oct 9, 2019 · 19 comments
Closed

Ask about credentials.code #115

rmelnik7777 opened this issue Oct 9, 2019 · 19 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@rmelnik7777
Copy link

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)")
        }
    }
}

}

@sbertix
Copy link
Collaborator

sbertix commented Oct 9, 2019

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.

@rmelnik7777
Copy link
Author

rmelnik7777 commented Oct 9, 2019

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

@sbertix
Copy link
Collaborator

sbertix commented Oct 9, 2019

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. 😊
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).

@sbertix sbertix added the help wanted Extra attention is needed label Oct 9, 2019
@rmelnik7777
Copy link
Author

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

@sbertix
Copy link
Collaborator

sbertix commented Oct 9, 2019

.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 🤔

@sbertix
Copy link
Collaborator

sbertix commented Oct 9, 2019

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 😞

@rmelnik7777
Copy link
Author

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?

@rmelnik7777
Copy link
Author

how do you test sending a message back?

@sbertix
Copy link
Collaborator

sbertix commented Oct 9, 2019

.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() {
  
}

@rmelnik7777
Copy link
Author

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)")
        }
    }
}

}

@sbertix
Copy link
Collaborator

sbertix commented Oct 9, 2019

And what is the error this time? @rmelnik7777

@rmelnik7777
Copy link
Author

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

@sbertix
Copy link
Collaborator

sbertix commented Oct 9, 2019

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

@hassmich
Copy link

hassmich commented Oct 9, 2019

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?

@Yagia
Copy link

Yagia commented Oct 9, 2019

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

@TheM4hd1 TheM4hd1 added the bug Something isn't working label Oct 9, 2019
@TheM4hd1
Copy link
Owner

TheM4hd1 commented Oct 9, 2019

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

@sbertix
Copy link
Collaborator

sbertix commented Oct 13, 2019

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

@champion-leo
Copy link

champion-leo commented Oct 22, 2019

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.

@sbertix
Copy link
Collaborator

sbertix commented Oct 23, 2019

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

sbertix added a commit that referenced this issue Nov 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

6 participants