Skip to content

Instantly share code, notes, and snippets.

@29satnam
Last active January 20, 2016 10:33
Show Gist options
  • Save 29satnam/5175a7fac170d29b4102 to your computer and use it in GitHub Desktop.
Save 29satnam/5175a7fac170d29b4102 to your computer and use it in GitHub Desktop.
To get the current user, send a GET request to /me. Include the sid cookie to identify your session - Depoyd. This is iOS Swift way of doing the same.
let token = prefs.valueForKey("SESSIONID") as? String
let session = NSURLSession.sharedSession()
let url = NSURL(string: "http://xxxxxxxxxx.com:2403/users/me")
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "GET"
request.setValue( "Bearer \(token!)", forHTTPHeaderField: "Authorization")
let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
if (data != nil) {
let res = response as! NSHTTPURLResponse!;
if 200..<300 ~= res.statusCode {
do {
let jsonData:NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers ) as! NSDictionary
let username:String = jsonData.valueForKey("username") as! String
print(username)
} catch _ as NSError {
}
}
}
})
task.resume()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment