Skip to content
This repository has been archived by the owner on Feb 20, 2022. It is now read-only.

Commit

Permalink
User.current_user() from a valid session token
Browse files Browse the repository at this point in the history
  • Loading branch information
deanq committed Feb 20, 2015
1 parent 8a811db commit 3c51cbc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.mkd
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,26 @@ u.save()
u.delete()
~~~~~

To get the current user from a Parse session:

~~~~~ {python}
from parse_rest.connection import SessionToken, register
# Acquire a valid parse session somewhere
# Example: token = request.session.get('session_token')
# Method 1: Using a `with` statement
# Do this to isolate use of session token in this block only
with SessionToken(token):
me = User.current_user()
# Method 2: register your parse connection with `session_token` parameter
# Do this to use the session token for all subsequent queries
register(PARSE_APPID, PARSE_APIKEY, session_token=token)
me = User.current_user()
~~~~~


Push
---------------

Expand Down
15 changes: 15 additions & 0 deletions parse_rest/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,21 @@ def testUserAsQueryArg(self):
g.save()
self.assertEqual(1, len(Game.Query.filter(creator=user)))

def testCanGetCurrentUser(self):
user = User.signup(self.username, self.password)
self.assertIsNotNone(user.sessionToken)

register(
getattr(settings_local, 'APPLICATION_ID'),
getattr(settings_local, 'REST_API_KEY'),
session_token=user.sessionToken
)

current_user = User.current_user()
self.assertIsNotNone(current_user)
self.assertEqual(current_user.sessionToken, user.sessionToken)
self.assertEqual(current_user.username, user.username)


class TestPush(unittest.TestCase):
"""
Expand Down
5 changes: 5 additions & 0 deletions parse_rest/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ def login_auth(cls, auth):
login_url = User.ENDPOINT_ROOT
return cls(**User.POST(login_url, authData=auth))

@classmethod
def current_user(cls):
user_url = '/'.join([API_ROOT, 'users/me'])
return cls(**User.GET(user_url))

@staticmethod
def request_password_reset(email):
'''Trigger Parse\'s Password Process. Return True/False
Expand Down

0 comments on commit 3c51cbc

Please sign in to comment.