Edwards
Edwards

Reputation: 131

Swift: How to check in AppDelegate if user is already logged in

My login with Deployd and Alamofire is working completely fine. Here is the code:

    let credential = NSURLCredential(user: "username", password: "password", persistence: .ForSession)
    let parameters = ["username" : "username", "password" : "password"]
    Alamofire.request(.POST, "http://xxxxxxxxxxxx/users/login", parameters: parameters)
        .authenticate(usingCredential: credential)
        .response { request, response, _, error in
            if response!.statusCode == 200 {
                self.performSegueWithIdentifier("loggedIn", sender: self)
            } else {
                print("invalid credential bro!")
        }
    }

How can I check in the AppDelegate if the user is already logged in? I am using the Deployd API on a custom server for user authentication and would like to hide the 'Login Screen' (LoginViewController) if the user is already logged in and has an active session.

Upvotes: 1

Views: 2390

Answers (1)

Nazariy Vlizlo
Nazariy Vlizlo

Reputation: 987

Just check if access_token exists, like in FB API. Or you can hold it in NSUserDefaults if you logged successfully:

if currentAccessToken != nil {
    print("User logged in")
}

Upvotes: 2

Related Questions