Skip to content

Commit

Permalink
Merge pull request #140 from DocShow-AI/remember_me
Browse files Browse the repository at this point in the history
Implement remember me functionality
  • Loading branch information
liberty-rising authored Dec 27, 2023
2 parents 65c04c3 + b6d3fd2 commit 1915c7b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
32 changes: 23 additions & 9 deletions backend/routes/auth_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@
verify_refresh_token,
update_user_refresh_token,
)
from settings import ACCESS_TOKEN_EXPIRE_MINUTES, REFRESH_TOKEN_EXPIRE_DAYS
from settings import (
ACCESS_TOKEN_EXPIRE_MINUTES,
REFRESH_TOKEN_EXPIRE_DAYS,
REMEMBER_ME_ACCESS_TOKEN_EXPIRE_MINUTES,
REMEMBER_ME_REFRESH_TOKEN_EXPIRE_DAYS,
)


auth_router = APIRouter()
Expand All @@ -45,6 +50,7 @@ async def login_for_access_token(
username: Optional[str] = Form(None),
email: Optional[EmailStr] = Form(None),
password: str = Form(...),
remember: bool = Form(False),
):
"""
Authenticate a user and set a JWT token in a cookie upon successful authentication.
Expand All @@ -61,21 +67,29 @@ async def login_for_access_token(
form_data = CustomOAuth2PasswordRequestForm(
username=username, email=email, password=password
)
print("form_data", form_data)
user = authenticate_user(form_data.username, form_data.email, form_data.password)
if not user:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect username or password",
headers={"WWW-Authenticate": "Bearer"},
)

access_token = create_token(
{"sub": user.username}, timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
)
refresh_token = create_token(
{"sub": user.username}, timedelta(days=REFRESH_TOKEN_EXPIRE_DAYS)
)
if remember:
access_token = create_token(
{"sub": user.username},
timedelta(minutes=REMEMBER_ME_ACCESS_TOKEN_EXPIRE_MINUTES),
)
refresh_token = create_token(
{"sub": user.username},
timedelta(days=REMEMBER_ME_REFRESH_TOKEN_EXPIRE_DAYS),
)
else:
access_token = create_token(
{"sub": user.username}, timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
)
refresh_token = create_token(
{"sub": user.username}, timedelta(days=REFRESH_TOKEN_EXPIRE_DAYS)
)
update_user_refresh_token(
user_id=user.id,
refresh_token=refresh_token,
Expand Down
8 changes: 7 additions & 1 deletion backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@
APP_HOST = config("APP_HOST")

ACCESS_TOKEN_EXPIRE_MINUTES = config("ACCESS_TOKEN_EXPIRE_MINUTES", default=30)
REFRESH_TOKEN_EXPIRE_DAYS = config("REFRESH_TOKEN_EXPIRE_DAYS", default=7)
REFRESH_TOKEN_EXPIRE_DAYS = config("REFRESH_TOKEN_EXPIRE_DAYS", default=1)
REMEMBER_ME_ACCESS_TOKEN_EXPIRE_MINUTES = config(
"REMEMBER_ME_ACCESS_TOKEN_EXPIRE_MINUTES", default=30
)
REMEMBER_ME_REFRESH_TOKEN_EXPIRE_DAYS = config(
"REMEMBER_ME_REFRESH_TOKEN_EXPIRE_DAYS", default=7
)
JWT_SECRET_KEY = config("JWT_SECRET_KEY")

DB_URL = config("DB_URL")
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/pages/login/LoginPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ function LoginPage({ onLogin }) {

// Determine if usernameOrEmail should be sent as username or email
const isEmail = validator.isEmail(usernameOrEmail);
const data = isEmail ? { email: usernameOrEmail, password } : { username: usernameOrEmail, password };
const data = isEmail
? { email: usernameOrEmail, password, remember: rememberMe }
: { username: usernameOrEmail, password, rememer: rememberMe };

try {
const response = await axios.post(`${API_URL}token/`, qs.stringify (data), {
Expand Down

0 comments on commit 1915c7b

Please sign in to comment.