[python] Authlib를 사용하여 애플리케이션에 인증된 사용자를 로그아웃시키는 방법은?
- Authlib 설치하기:
pip install authlib
- 로그아웃 API 엔드포인트 생성하기:
from flask import Flask, request from authlib.integrations.flask_client import OAuth app = Flask(__name__) oauth = OAuth(app) @app.route('/logout') def logout(): # 현재 인증된 사용자 정보를 얻어옴 current_user = oauth.current_user() if current_user: # 로그아웃 처리 oauth.logout() # 로그아웃 후 리다이렉트할 URL 설정 next_url = request.args.get('next') or '/' return redirect(next_url)
- 로그아웃 링크 생성하기:
from flask import url_for @app.route('/') def home(): logout_url = url_for('logout') # 로그아웃 URL 생성 return f'<a href="{logout_url}">로그아웃</a>'
이제 사용자가 애플리케이션의 로그아웃 링크를 클릭하면, Authlib는 현재 사용자를 로그아웃 처리하고 로그아웃 후에 리다이렉트할 URL로 이동합니다.