diff --git a/server/src/app/__pycache__/config.cpython-311.pyc b/server/src/app/__pycache__/config.cpython-311.pyc index d2c5931..8005618 100644 Binary files a/server/src/app/__pycache__/config.cpython-311.pyc and b/server/src/app/__pycache__/config.cpython-311.pyc differ diff --git a/server/src/app/api/v1/__pycache__/auth.cpython-311.pyc b/server/src/app/api/v1/__pycache__/auth.cpython-311.pyc index ae43a0d..5437647 100644 Binary files a/server/src/app/api/v1/__pycache__/auth.cpython-311.pyc and b/server/src/app/api/v1/__pycache__/auth.cpython-311.pyc differ diff --git a/server/src/app/api/v1/auth.py b/server/src/app/api/v1/auth.py index 4db286e..6565f9a 100644 --- a/server/src/app/api/v1/auth.py +++ b/server/src/app/api/v1/auth.py @@ -7,7 +7,7 @@ from src.app.models.user import User, UserCreate from src.app.data.user import create_user, get_user_by_username from src.app.auth.dependancies import get_current_user, authenticate_user -from src.app.auth.security import hash_password, create_access_token +from src.app.auth.security import hash_password, create_access_token, verify_beyond_user_limit from src.app.auth.schemas import Token router = APIRouter(prefix="/auth", tags=["auth"]) @@ -30,12 +30,16 @@ async def user(current_user: Annotated[str, Depends(get_current_user)]): @router.post("/register") async def create(user_data: UserCreate): + if(verify_beyond_user_limit()): + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="The user limit has been reached." + ) if get_user_by_username(user_data.username): raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail="Username already registered" ) - hashed_password = hash_password(user_data.plain_password) user = User(username = user_data.username, hashed_password = hashed_password) created_user = create_user(user) diff --git a/server/src/app/auth/__pycache__/security.cpython-311.pyc b/server/src/app/auth/__pycache__/security.cpython-311.pyc index a2b187c..5d4a815 100644 Binary files a/server/src/app/auth/__pycache__/security.cpython-311.pyc and b/server/src/app/auth/__pycache__/security.cpython-311.pyc differ diff --git a/server/src/app/auth/security.py b/server/src/app/auth/security.py index 5cc3c27..a5cfa6b 100644 --- a/server/src/app/auth/security.py +++ b/server/src/app/auth/security.py @@ -1,5 +1,6 @@ from src.app.config import settings -from typing import Optional +from typing import Optional, Sequence +from src.app.models.user import User from datetime import timedelta, datetime, timezone from argon2 import PasswordHasher from argon2.exceptions import ( @@ -9,8 +10,7 @@ from argon2.exceptions import ( ) import jwt from jwt.exceptions import InvalidTokenError - - +from src.app.data.user import get_users password_hasher = PasswordHasher() @@ -37,4 +37,11 @@ def verify_token(token: str, token_type: str = "access") -> Optional[dict]: except InvalidTokenError: return None +def verify_beyond_user_limit() -> bool: + users: Sequence[User] = get_users() + if (len(users) > settings.USER_LIMIT): + return True + else: + return False + #def create_refresh_token(data: dict) -> str: \ No newline at end of file diff --git a/server/src/app/config.py b/server/src/app/config.py index 9595cb1..ebe0310 100644 --- a/server/src/app/config.py +++ b/server/src/app/config.py @@ -14,6 +14,7 @@ class Settings(BaseSettings): SECRET_KEY : str = Field('random_string', env='SECRET_KEY') ACCESS_TOKEN_EXPIRE_MINUTES: int = 240 ALGORITHM: str = "HS256" + USER_LIMIT: int = 10 class Config: env_file = ".env" diff --git a/user-interface/src/router/index.ts b/user-interface/src/router/index.ts index 0335622..852944f 100644 --- a/user-interface/src/router/index.ts +++ b/user-interface/src/router/index.ts @@ -1,7 +1,7 @@ import { createRouter, createWebHistory } from 'vue-router' import { isAuthenticated } from '@/services/apiAxios' -const pagesWithoutGuard = ['login', 'app'] +const pagesWithoutGuard = ['login', 'app', 'register'] const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), @@ -22,14 +22,20 @@ const router = createRouter({ path: '/login', alias: '/login', name: 'login', - component: () => import('@/views/AuthView.vue'), + component: () => import('@/views/LoginView.vue'), + }, + { + path: '/register', + alias: '/register', + name: 'register', + component: () => import('@/views/RegisterView.vue'), } ], }) router.beforeEach(async (to, from) => { const isAuth = await isAuthenticated() - if (!isAuth && !pagesWithoutGuard.includes(to.name!.toString())) { + if (!isAuth && pagesWithoutGuard.includes(to.name!.toString())) { return { name: 'login' } } }) diff --git a/user-interface/src/services/apiAxios.ts b/user-interface/src/services/apiAxios.ts index f2eba36..828d996 100644 --- a/user-interface/src/services/apiAxios.ts +++ b/user-interface/src/services/apiAxios.ts @@ -14,7 +14,11 @@ api.interceptors.request.use((config) => { }) export const authAPI = { - register: (data: unknown) => api.post('/api/v1/auth/register', data), + register: (username: string, password: string) => + api.post( + '/api/v1/auth/register', + { "username":username, "plain_password":password } + ), login: (username: string, password: string) => api.post( '/api/v1/auth/login', diff --git a/user-interface/src/views/AuthView.vue b/user-interface/src/views/LoginView.vue similarity index 95% rename from user-interface/src/views/AuthView.vue rename to user-interface/src/views/LoginView.vue index 1dabd36..fa380e4 100644 --- a/user-interface/src/views/AuthView.vue +++ b/user-interface/src/views/LoginView.vue @@ -46,7 +46,7 @@ required > - Login + Login diff --git a/user-interface/src/views/RegisterView.vue b/user-interface/src/views/RegisterView.vue new file mode 100644 index 0000000..57c593f --- /dev/null +++ b/user-interface/src/views/RegisterView.vue @@ -0,0 +1,63 @@ + + + + + + + + + + + + + Register + + + + \ No newline at end of file