File tree Expand file tree Collapse file tree 5 files changed +75
-17
lines changed Expand file tree Collapse file tree 5 files changed +75
-17
lines changed Original file line number Diff line number Diff line change 1717templates = Jinja2Templates (directory = TEMPLATES_PATH )
1818templates .env .add_extension ("jinja2.ext.i18n" )
1919
20+
2021# Configure logger
2122logger = LoggerCustomizer .make_logger (
2223 config .LOG_PATH ,
Original file line number Diff line number Diff line change 1+ from typing import Optional
2+
13from fastapi import Depends , HTTPException
24from starlette .requests import Request
35from starlette .status import HTTP_401_UNAUTHORIZED
@@ -90,3 +92,21 @@ async def current_user(
9092 detail = "Your token is not valid. Please log in again" ,
9193 )
9294 return schema .CurrentUser (user_id = user_id , username = username )
95+
96+
97+ def get_jinja_current_user (request : Request ) -> Optional [schema .CurrentUser ]:
98+ """Return the currently logged in user.
99+ Returns logged in User object if exists, None if not.
100+ Set as a jinja global parameter.
101+ """
102+ if "Authorization" not in request .cookies :
103+ return None
104+ jwt_payload = get_jwt_token (request .cookies ["Authorization" ])
105+ username = jwt_payload .get ("sub" )
106+ user_id = jwt_payload .get ("user_id" )
107+ if not user_id :
108+ raise HTTPException (
109+ status_code = HTTP_401_UNAUTHORIZED ,
110+ detail = "Your token is not valid. Please log in again" ,
111+ )
112+ return schema .CurrentUser (user_id = user_id , username = username )
Original file line number Diff line number Diff line change 66from fastapi .staticfiles import StaticFiles
77from sqlalchemy .orm import Session
88
9+ import app .internal .features as internal_features
910from app import config
1011from app .database import engine , models
1112from app .dependencies import (
1213 MEDIA_PATH ,
1314 SOUNDS_PATH ,
1415 STATIC_PATH ,
1516 UPLOAD_PATH ,
17+ SessionLocal ,
1618 get_db ,
1719 logger ,
1820 templates ,
19- SessionLocal ,
2021)
2122from app .internal import daily_quotes , json_data_loader
22- import app .internal .features as internal_features
2323from app .internal .languages import set_ui_language
24+ from app .internal .security .dependencies import get_jinja_current_user
2425from app .internal .security .ouath2 import auth_exception_handler
2526from app .routers .salary import routes as salary
2627from app .utils .extending_openapi import custom_openapi
@@ -51,6 +52,7 @@ def create_tables(engine, psql_environment):
5152app .logger = logger
5253
5354app .add_exception_handler (status .HTTP_401_UNAUTHORIZED , auth_exception_handler )
55+ templates .env .globals ["jinja_current_user" ] = get_jinja_current_user
5456
5557# This MUST come before the app.routers imports.
5658set_ui_language ()
Original file line number Diff line number Diff line change 3131 </ div >
3232 < div class ="collapse navbar-collapse " id ="navbarToggler ">
3333 < ul class ="navbar-nav mr-auto mb-2 mb-lg-0 ">
34- < li class ="nav-item ">
35- < a class ="nav-link " href ="{{ url_for('profile') }} "> Profile</ a >
36- </ li >
37- < li class ="nav-item ">
38- < a class ="nav-link " href ="{{ url_for('login') }} "> Sign In</ a >
39- </ li >
40- < li class ="nav-item ">
41- < a class ="nav-link " href ="{{ url_for('logout') }} "> {{ gettext("Sign Out") }}</ a >
42- </ li >
43- < li class ="nav-item ">
44- < a class ="nav-link " href ="{{ url_for('register') }} "> Sign Up</ a >
45- </ li >
46- < li class ="nav-item ">
47- < a class ="nav-link " href ="{{ url_for('agenda') }} "> Agenda</ a >
48- </ li >
34+ {% if jinja_current_user(request) %}
35+ < li class ="nav-item ">
36+ < a class ="nav-link " href ="{{ url_for('profile') }} "> Profile</ a >
37+ </ li >
38+ < li class ="nav-item ">
39+ < a class ="nav-link " href ="{{ url_for('logout') }} "> Sign Out</ a >
40+ </ li >
41+ < li class ="nav-item ">
42+ < a class ="nav-link " href ="{{ url_for('agenda') }} "> Agenda</ a >
43+ </ li >
44+ {% else %}
45+ < li class ="nav-item ">
46+ < a class ="nav-link " href ="{{ url_for('login') }} "> Sign In</ a >
47+ </ li >
48+ < li class ="nav-item ">
49+ < a class ="nav-link " href ="{{ url_for('register') }} "> Sign Up</ a >
50+ </ li >
51+ {% endif %}
4952 < li class ="nav-item ">
5053 < a class ="nav-link " href ="{{ url_for( 'audio_settings') }} "> Audio Settings</ a >
5154 </ li >
Original file line number Diff line number Diff line change 1+ REGISTER_DETAIL = {
2+ "username" : "correct_user" ,
3+ "full_name" : "full_name" ,
4+ "password" : "correct_password" ,
5+ "confirm_password" : "correct_password" ,
6+ "email" : "example@email.com" ,
7+ "description" : "" ,
8+ }
9+
10+ LOGIN_DATA = {"username" : "correct_user" , "password" : "correct_password" }
11+
12+
13+ def test_user_not_logged_in (session , security_test_client ):
14+ security_test_client .get (security_test_client .app .url_path_for ("logout" ))
15+ response = security_test_client .get ("/about" )
16+ assert b"Sign Out" not in response .content
17+ assert b"Sign In" in response .content
18+
19+
20+ def test_user_is_logged_in (session , security_test_client ):
21+ security_test_client .get (security_test_client .app .url_path_for ("logout" ))
22+ security_test_client .post (
23+ security_test_client .app .url_path_for ("register" ),
24+ data = REGISTER_DETAIL ,
25+ )
26+ security_test_client .post (
27+ security_test_client .app .url_path_for ("login" ),
28+ data = LOGIN_DATA ,
29+ )
30+ response = security_test_client .get ("/about" )
31+ assert b"Sign Out" in response .content
32+ assert b"Sign In" not in response .content
You can’t perform that action at this time.
0 commit comments