Performance: OAuth2 overview
OAuth2 impacts page load speed and interaction responsiveness mainly through network requests and token handling during authentication flows.
from flask import redirect def login(): # Redirect user to OAuth2 provider for authorization return redirect('https://authserver.com/authorize?client_id=xyz&response_type=code') # Token exchange done asynchronously after redirect
def login(): # Blocking synchronous token request token = requests.post('https://authserver.com/token', data=payload).json() # Proceed only after token received return render_template('dashboard.html', token=token)
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous token request during login | Minimal | 0 | Blocks paint until response | [X] Bad |
| Redirect to OAuth2 provider for async token | Minimal | 0 | No blocking paint | [OK] Good |
print(user_info) output after successful login?from authlib.integrations.flask_client import OAuth
app = Flask(__name__)
oauth = OAuth(app)
google = oauth.register('google', client_id='id', client_secret='secret', access_token_url='https://accounts.google.com/o/oauth2/token', authorize_url='https://accounts.google.com/o/oauth2/auth', api_base_url='https://www.googleapis.com/oauth2/v1/', client_kwargs={'scope': 'openid email profile'})
@app.route('/login')
def login():
redirect_uri = url_for('authorize', _external=True)
return google.authorize_redirect(redirect_uri)
@app.route('/authorize')
def authorize():
token = google.authorize_access_token()
user_info = google.get('userinfo').json()
print(user_info)
return 'Logged in'user_info contentgoogle.get('userinfo').json() returns a dictionary with user details like email and name.@app.route('/authorize')
def authorize():
token = google.authorize_access_token
user_info = google.get('userinfo').json()
return user_infogoogle.authorize_access_token without parentheses, so it references the method, not calls it.token is a function object, causing errors later.scope value should you use when registering the OAuth client?Pass the test and this pattern moves unknown