Skip to content

Commit

Permalink
add link account path to google authentiation
Browse files Browse the repository at this point in the history
  • Loading branch information
stuzl committed Feb 26, 2016
1 parent 74240a0 commit 32bfcf3
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions examples/server/python/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,29 @@ def google():
r = requests.get(people_api_url, headers=headers)
profile = json.loads(r.text)

# Step 3. (optional) Link accounts.
if request.headers.get('Authorization'):
user = User.query.filter_by(google=profile['sub']).first()
if user:
response = jsonify(message='There is already a Google account that belongs to you')
response.status_code = 409
return response

payload = parse_token(request)

user = User.query.filter_by(id=payload['sub']).first()
if not user:
response = jsonify(message='User not found')
response.status_code = 400
return response
user.google = profile['sub']
user.display_name = user.display_name or profile['name']
db.session.commit()
token = create_token(u)
return jsonify(token=token)

# Step 4. Create a new account or return an existing one.

user = User.query.filter_by(google=profile['sub']).first()
if user:
token = create_token(user)
Expand Down

0 comments on commit 32bfcf3

Please sign in to comment.