Skip to content

Commit

Permalink
add session and keep it in redis
Browse files Browse the repository at this point in the history
Signed-off-by: viste <[email protected]>
  • Loading branch information
Viste committed Aug 20, 2024
1 parent cae0f98 commit 6f7849a
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 6 deletions.
3 changes: 3 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from dotenv import load_dotenv
from flask import Flask
from flask_migrate import Migrate
from flask_session import Session
from flask_talisman import Talisman
from werkzeug.middleware.proxy_fix import ProxyFix

Expand All @@ -17,7 +18,9 @@
talisman = Talisman(app)
app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1)
app.config.from_object(Config)

db.init_app(app)
Session(app)
migrate = Migrate(app, db)


Expand Down
2 changes: 1 addition & 1 deletion core/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def setup_routes(app, oauth):
authorize_url='https://id.vk.com/authorize',
access_token_url='https://id.vk.com/oauth2/auth',
client_kwargs={
'scope': 'email phone',
'scope': 'email',
'token_endpoint_auth_method': 'client_secret_post',
'token_placement': 'header',
'response_type': 'code'
Expand Down
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
flask[async]
flask-admin[async]
flask-login[async]
Flask-Session
Flask-SQLAlchemy
Flask-Migrate
alembic
Expand All @@ -25,4 +26,5 @@ Flask-OAuthlib
requests-oauthlib
pytest
pytest-mock
requests
requests
redis
2 changes: 2 additions & 0 deletions tools/auth.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from flask import current_app
from flask import session
from flask_login import LoginManager, login_user, logout_user
from werkzeug.security import check_password_hash
Expand Down Expand Up @@ -35,6 +36,7 @@ def authenticate_vk_user(vk_id, screen_name, first_name, last_name, profile_pict
session['id'] = user.id
session['username'] = user.username
login_user(user)
current_app.logger.debug(f"User {user.username} authenticated and logged in")
return True


Expand Down
11 changes: 7 additions & 4 deletions tools/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os

import redis


class Config:
SQLALCHEMY_DATABASE_URI = os.getenv('SQLALCHEMY_DATABASE_URI', 'mariadb+pymysql://user:pass@localhost/base?charset=utf8mb4')
Expand All @@ -11,10 +13,11 @@ class Config:
SQLALCHEMY_MAX_OVERFLOW = 5
SQLALCHEMY_ENGINE_OPTIONS = {"pool_pre_ping": True}

TELEGRAM_BOT_TOKEN = os.getenv('TG_BOT_TOKEN')
TELEGRAM_BOT_NAME = 'dev_vlab_bot'

# VKontakte OAuth configuration
VK_CLIENT_ID = os.getenv('VK_CLIENT_ID')
VK_CLIENT_SECRET = os.getenv('VK_CLIENT_SECRET')
VK_REDIRECT_URI = os.getenv('VK_REDIRECT_URI', 'http://localhost:5000/vk/authorize')
SESSION_TYPE = 'redis'
SESSION_PERMANENT = False
SESSION_USE_SIGNER = True
SESSION_KEY_PREFIX = 'vlab_session:'
SESSION_REDIS = redis.StrictRedis(host='redis-master.redis.svc.cluster.local', port=6379, db=0)

0 comments on commit 6f7849a

Please sign in to comment.