Using Flask-Social only with Oauth provider, without registration and registration

Is it possible to use Flask-Social and Flask-Security if I only want to use Facebook Login, for example, to register a user and log in, i.e. no local registration / login forms?

I looked at the sample application with the flag software and the documentation, but could not determine if this was possible. In the sample application, users cannot log into Facebook if they have not previously registered. After registering with the sample application, they can link their Facebook account with their local account.

When I tried to call social.facebook.get_connection (), I got the AttributeError 'AnonymousUser' object has no attribute 'id' because there is no current_user , which is determined by the fly protection after registration / login.

+7
python flask flask-security facebook-oauth
source share
1 answer

This can be done without any extra work using the @login_failed.connect_via decorator. With app as an instance of a Flask application, it will look like

 @login_failed.connect_via(app): def on_login_failed(sender, provider, oauth_response): connection_values = get_connection_values_from_oauth_response(provider, oauth_response) ds = current_app.security.datastore user = ds.create_user( ... ) #fill in relevant stuff here ds.commit() connection_values['user_id'] = user.id connect_handler(connection_values, provider) login_user(user) db.commit() return render_template('success.html') 

As for filling in the necessary materials for creating a user, I just create a random line for the password and have no problems leaving the email message zero. I also just included the same answer on the Flask-Social github page.

+4
source share

All Articles