def login_succeeded(): connection = Connection( institution=None, plaid_token=client.access_token, ) # TODO: add timeout here data = client.connect_get().json() user = g.user user.connections.append(connection) for account in data['accounts']: connection.institution = account['institution_type'] account = Account( unique_id=account['_id'], available_balance=account['balance']['available'], current_balance=account['balance']['current'], name=account.get('meta', dict()).get('name', None), number=account.get('meta', dict()).get('number', None), limit=account.get('meta', dict()).get('limit', None), type=account['type'], subtype=account.get('subtype', None), ) connection.accounts.append(account) db.session.add(user) db.session.commit() return success(connection)
def login(): if session.get('email', None): return fail('Already logged in') access_token = session.get('fb_token', None) if access_token is None: return fail('No facebook access token') r = requests.get('https://graph.facebook.com/v2.3/me', params={ 'fields': 'email', 'access_token': access_token, }) data = r.json() if 'error' in data: return fail(data['error']['message']) email = data.get('email', None) if not email: return fail('Bad response from facebook API') user = User.query.filter_by(email=email).first() if not user: return fail('No account') session['email'] = email return success(user)
def oauth_callback(): code = request.args.get('code', '') if not code: return abort(404) r = requests.get('https://graph.facebook.com/v2.3/oauth/access_token', params={ 'client_id': config['FACEBOOK']['APP_ID'], 'client_secret': config['FACEBOOK']['APP_SECRET'], 'redirect_uri': config['FACEBOOK']['REDIRECT_URI'], 'code': code, }) data = r.json() if 'error' in data: return fail(data['error']['message']) access_token = data.get('access_token', None) if not access_token: return error('Didn\'t receive access token?') session['fb_token'] = access_token return success(access_token)
def new_account(): access_token = session.get('fb_token', None) if access_token is None: return fail('No login token') r = requests.get('https://graph.facebook.com/v2.3/me', params={ 'fields': 'email,first_name,last_name', 'access_token': access_token, }) data = r.json() if 'error' in data: return fail(data['error']['message']) email = data.get('email', None) first_name = data.get('first_name', None) last_name = data.get('last_name', None) if not email or not first_name or not last_name: return fail('Missing fields') user = User.query.filter_by(email=email).first() if user: return fail('User already exists') user = User( name='{} {}'.format(first_name, last_name), email=email, ) db.session.add(user) db.session.commit() session['email'] = email return success(user)
def oauth_redirect(): return success('https://www.facebook.com/dialog/oauth?' 'client_id={}&redirect_uri={}&scope=email'.format( config['FACEBOOK']['APP_ID'], config['FACEBOOK']['REDIRECT_URI'], ))
def health(): return success('ok')
def home(): return success(User.query.all())
def institutions(): data = client.institutions().json() return success(data)
def categories(): data = client.categories().json() return success(data)