From 3c8c900d2c788b88b2a97785ade94cd790c38bb5 Mon Sep 17 00:00:00 2001 From: Luka Radenovic Date: Fri, 15 Apr 2022 12:44:30 +0200 Subject: [PATCH] Return role_id when callback is called --- areas/auth/auth.py | 4 ++++ areas/users/user_service.py | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/areas/auth/auth.py b/areas/auth/auth.py index 47a1a5b..9f95b77 100644 --- a/areas/auth/auth.py +++ b/areas/auth/auth.py @@ -4,6 +4,7 @@ from flask_cors import cross_origin from datetime import timedelta from areas import api_v1 +from areas.apps import AppRole from config import * from helpers import HydraOauth, BadRequest, KratosApi @@ -39,6 +40,8 @@ def hydra_callback(): identity=token, expires_delta=timedelta(days=365) ) + app_role = AppRole.query.filter_by(user_id=identity["id"]).first() + return jsonify( { "accessToken": access_token, @@ -47,6 +50,7 @@ def hydra_callback(): "email": user_info["email"], "name": user_info["name"], "preferredUsername": user_info["preferred_username"], + "role_id": app_role.role_id if app_role else None, }, } ) diff --git a/areas/users/user_service.py b/areas/users/user_service.py index eb185c7..c9c0cc2 100644 --- a/areas/users/user_service.py +++ b/areas/users/user_service.py @@ -51,6 +51,14 @@ class UserService: if app_role: app_role.role_id = data["role_id"] if "role_id" in data else None db.session.commit() + else: + appRole = AppRole( + user_id=id, + role_id=data["role_id"] if "role_id" in data else None, + app_id=1, + ) + db.session.add(appRole) + db.session.commit() return UserService.get_user(id)