7

I want my PFSessions to be exclusive, meaning, If a user is already logged in on a certain device in a certain location, if another device logs in with the same credentials, I want the previous session to be terminated, with a message of an alert view of course. Sort of like the old AOL Instant Messaging format. so does anyone know how I would go about doing so in swift? would it require CoreLocation?

I found this article on current user location, how can I use this as part of a solution to my question ?

https://www.veasoftware.com/tutorials/2014/10/18/xcode-6-tutorial-ios-8-current-location-in-swift

UPDATE

so I've just read this parse article on their revocable sessions settings

http://blog.parse.com/announcements/announcing-new-enhanced-sessions/

however, when I log into the same account with a different device, the sessions are allowed to live correspondingly, which i do not want. How do I solve my dilemma?

UPDATE

I've gotten this very detailed description on how to achieve the overall method I'm trying to implement :

enter image description here

however I am not very well versed in cloud code implementation, can someone very briefly portray a piece of code that is similar to what he's trying to relay onto me?

UPDATE

So I did some more research and tied in what I was told in regards on how to use the cloud code calls in parse, and being I want to destroy previous sessions of the currentUser, I've written the following code in my login "success" logic:

            PFUser.logInWithUsernameInBackground(userName, password: passWord) {
        (user, error: NSError?) -> Void in
        if user != nil || error == nil {
            dispatch_async(dispatch_get_main_queue()) {
                self.performSegueWithIdentifier("loginSuccess", sender: self)

                 PFCloud.callFunctionInBackground("currentUser", withParameters: ["PFUser":"currentUser"])
                    //..... Get other currentUser session tokens and destroy them

            }

        } else {

If thats even the correct format or code, but I’m sure this is the right direction right? Can anybody edit or expand on the code I'm trying to achieve?

John Durand
  • 1,934
  • 5
  • 22
  • 34
  • ran into the same problem and solved it.

    http://stackoverflow.com/questions/34620442/is-there-a-way-to-enforce-single-device-login-on-parse-com

    – Markus Jan 09 '16 at 04:55

1 Answers1

2

You should start by looking into the cloud code quick start
You would then define following cloud code function:

Parse.Cloud.define('destroyUserSessions', function(req, res) {
    //the user that sends the request
    var currentUser = req.user;
    //send from client 
    var currentUserInstallationId = req.param.installationId;
    var Session = Parse.Object.extend('Session');
    var userSessionQuery = new Parse.Query(Session);
    //all sessions of this user
    userSessionQuery.equalTo('user', currentUser);
    //except the session for this installation -> to not log the request performing user out
    userSessionQuery.notEqualTo('installationId', currentUserInstallationId);

    userSessionQuery.find({
        success: function(userSessionsToBeRevoked) {
            Parse.Object.destroyAll(userSessionsToBeRevoked, {
                success: function() {
                    //you have deleted all sessions except the one for the current installation
                    var installationIds = userSessionsToBeRevoked.map(function(session) {
                        return session.installationId;
                    }); 
                    //you can use the installation Ids to send push notifications to installations that are now logged out
                },
                error: function(err) {
                    //TODO: Handle error
                }
            });
        }, 
        error: function(err) {
            //TODO: Handle error
        }
    });
});

NOTE: This code was not tested and makes several assumptions e.g that you have revokable sessions enabled and there's a user logged in when performing the request

You would call the function like this:

let installationId = PFInstallation.currentInstallation().installationId

PFCloud.callFunctionInBackground("destroyUserSessions", withParameters: ["installationId": installationId]) { success, error in 
    //TODO: Handle success or errors
}

Hope this may get you started.

herby
  • 389
  • 3
  • 8