From 7a865b08cce1b08654660764200b87a78a6da3ff Mon Sep 17 00:00:00 2001 From: Nesswit Date: Sun, 22 Sep 2019 02:33:00 +0900 Subject: [PATCH] Save twitch id to database --- backend/flask/app/models.py | 1 + backend/flask/app/oauth.py | 5 +++- backend/flask/app/socket.py | 4 +-- .../migrations/versions/763fc9f85cf2_.py | 29 +++++++++++++++++++ frontend/recognition.html | 2 +- 5 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 backend/flask/migrations/versions/763fc9f85cf2_.py diff --git a/backend/flask/app/models.py b/backend/flask/app/models.py index 7d9f9b0..f2ed96d 100644 --- a/backend/flask/app/models.py +++ b/backend/flask/app/models.py @@ -47,6 +47,7 @@ def json(self): class User(UserMixin, SerializableMixin, db.Model): __tablename__ = "user" id = db.Column(db.Integer, primary_key=True) + twitch_id = db.Column(db.String(120), nullable=False) def json(self): data = self._serialize() diff --git a/backend/flask/app/oauth.py b/backend/flask/app/oauth.py index aa40b36..cf19f34 100644 --- a/backend/flask/app/oauth.py +++ b/backend/flask/app/oauth.py @@ -52,10 +52,13 @@ def twitch_logged_in(blueprint, token): oauth = OAuth(provider=blueprint.name, provider_user_id=user_id, token=token) if not oauth.user: - user = User() + user = User(twitch_id=user_id) oauth.user = user db.session.add_all([user, oauth]) db.session.commit() + else: + oauth.user.twitch_id = user_id + db.session.commit() login_user(oauth.user) flash("성공적으로 로그인했습니다.") diff --git a/backend/flask/app/socket.py b/backend/flask/app/socket.py index 3803932..88866d2 100644 --- a/backend/flask/app/socket.py +++ b/backend/flask/app/socket.py @@ -13,5 +13,5 @@ def connect_handler(): @socketio.on('message', namespace='/recognition') def handle_message(message): - if current_user.oauth: - send(message, json=True, broadcast=True, namespace=f'/overlay/{current_user.oauth[0].provider_user_id}') \ No newline at end of file + if current_user.twitch_id: + send(message, json=True, broadcast=True, namespace=f'/overlay/{current_user.twitch_id}') \ No newline at end of file diff --git a/backend/flask/migrations/versions/763fc9f85cf2_.py b/backend/flask/migrations/versions/763fc9f85cf2_.py new file mode 100644 index 0000000..702f813 --- /dev/null +++ b/backend/flask/migrations/versions/763fc9f85cf2_.py @@ -0,0 +1,29 @@ +"""empty message + +Revision ID: 763fc9f85cf2 +Revises: 59f3082483dd +Create Date: 2019-09-21 17:15:08.386984 + +""" +from alembic import op +import sqlalchemy as sa +import sqlalchemy_utils + + +# revision identifiers, used by Alembic. +revision = '763fc9f85cf2' +down_revision = '59f3082483dd' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('user', sa.Column('twitch_id', sa.String(length=120), nullable=False, server_default='')) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('user', 'twitch_id') + # ### end Alembic commands ### diff --git a/frontend/recognition.html b/frontend/recognition.html index 9979779..619ce4a 100644 --- a/frontend/recognition.html +++ b/frontend/recognition.html @@ -21,7 +21,7 @@