1

I recently migrated my application from Parse Server to my own custom server. I am trying to find a way to access the session token without having Parse SDK imported on my project. I need the session token, so I do not have to re-authenticate my current logged in users.

So far I access it with the following methods, that requires to import parse sdk:

        PFUser.current()?.saveInBackground(block: { [weak self] (success, error) in
            if let error = error {
                Log.error?.message(error.localizedDescription)
                return
            }
            guard let sessionToken = PFUser.current()?.sessionToken else {
                Log.error?.message("Did not get session token even after saving user")
                return
            }
        })

The other solution I was looking at is the private documents directory of parse, but I could only get applicationId, installationId and config from there:

class ParseHelper {    
    var parsePrivateDocumentsDirectory: URL? {
        let filemanager = FileManager.default
        guard let libraryDirectory = filemanager.urls(for: .libraryDirectory, in: .userDomainMask).last else {
            return nil
        }

        guard let parseDirectory = URL(string: "Private%20Documents/Parse/", relativeTo: libraryDirectory) else {
            return nil
        }
        return parseDirectory
    }

    func parseFile(_ filename: String) -> URL? {
        let filemanager = FileManager.default
        guard let parseDirectory = self.parsePrivateDocumentsDirectory else {
            return nil
        }

        guard let installationIdUrl = URL(string: filename, relativeTo: parseDirectory) else {
            return nil
        }

        if filemanager.fileExists(atPath: installationIdUrl.path) {
            return installationIdUrl
        } else {
            return nil
        }
    }

    var installationId: String? {
        guard let fileURL = self.parseFile("installationId"), let data = try? Data(contentsOf: fileURL) else {
            return nil
        }
        let string = String(data: data, encoding: String.Encoding.utf8)
        return string
    }

    var applicationId: String? {
        guard let fileURL = self.parseFile("applicationId"), let data = try? Data(contentsOf: fileURL) else {
            return nil
        }
        let string = String(data: data, encoding: String.Encoding.utf8)
        return string
    }

    var config: String? {
        guard let fileURL = self.parseFile("config"), let data = try? Data(contentsOf: fileURL) else {
            return nil
        }
        let string = String(data: data, encoding: String.Encoding.utf8)
        return string
    }
}

1 Answer 1

2

The only files available in Private Documents/Parse are:

  • applicationId
  • currentInstallation
  • currentUser
  • installationId

The sessionToken on the other hand, is stored in the keychain.
The key should be $APP_BUNDLE_ID.com.parse.sdk (Official info and source code, and here's the source of the function used to save the data).

In case you run into any problems, here's an issue the the official repo about server migration and sessions

Sign up to request clarification or add additional context in comments.

2 Comments

these links lead to 404's
Only the 2 links that include "master" since they refer files from years ago. Not that hard to go back in time using git: github.com/parse-community/Parse-SDK-iOS-OSX/blob/… and github.com/parse-community/Parse-SDK-iOS-OSX/blob/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.