From b06449c7d294cd346d4ea9a834c6e7a33bd0910c Mon Sep 17 00:00:00 2001 From: Janis Date: Wed, 10 Dec 2025 11:25:58 +0100 Subject: [PATCH] feat: FRO-2 Implement Login Component --- knockoutwhistfrontend | 2 +- knockoutwhistweb/app/auth/AuthAction.scala | 4 +- .../app/controllers/UserController.scala | 51 +++++++++---------- .../app/logic/user/SessionManager.scala | 1 + .../app/views/mainmenu/navbar.scala.html | 6 --- knockoutwhistweb/conf/application.conf | 9 ++++ knockoutwhistweb/conf/routes | 5 +- 7 files changed, 40 insertions(+), 38 deletions(-) diff --git a/knockoutwhistfrontend b/knockoutwhistfrontend index 5d080bb..d6270e6 160000 --- a/knockoutwhistfrontend +++ b/knockoutwhistfrontend @@ -1 +1 @@ -Subproject commit 5d080bba47778d51c8dbbb99d4d7c2b156c5316c +Subproject commit d6270e6f477282b273825813782d369f8a2bcb15 diff --git a/knockoutwhistweb/app/auth/AuthAction.scala b/knockoutwhistweb/app/auth/AuthAction.scala index c14b1dc..b802596 100644 --- a/knockoutwhistweb/app/auth/AuthAction.scala +++ b/knockoutwhistweb/app/auth/AuthAction.scala @@ -23,12 +23,12 @@ class AuthAction @Inject()(val sessionManager: SessionManager, val parser: BodyP case Some(user) => block(new AuthenticatedRequest(user, request)) case None => - Future.successful(Results.Redirect(routes.UserController.login())) + Future.successful(Results.Unauthorized) } } protected def getUserFromSession(request: RequestHeader): Option[User] = { - val session = request.cookies.get("sessionId") + val session = request.cookies.get("accessToken") if (session.isDefined) return sessionManager.getUserBySession(session.get.value) None diff --git a/knockoutwhistweb/app/controllers/UserController.scala b/knockoutwhistweb/app/controllers/UserController.scala index 7cd46de..392c7ff 100644 --- a/knockoutwhistweb/app/controllers/UserController.scala +++ b/knockoutwhistweb/app/controllers/UserController.scala @@ -1,10 +1,13 @@ package controllers import auth.{AuthAction, AuthenticatedRequest} +import dto.subDTO.UserDTO import logic.user.{SessionManager, UserManager} +import model.users.User import play.api.* import play.api.libs.json.Json import play.api.mvc.* +import play.api.mvc.Cookie.SameSite.{Lax, None, Strict} import javax.inject.* @@ -21,22 +24,6 @@ class UserController @Inject()( val authAction: AuthAction ) extends BaseController { - def login(): Action[AnyContent] = { - Action { implicit request => - val session = request.cookies.get("sessionId") - if (session.isDefined) { - val possibleUser = sessionManager.getUserBySession(session.get.value) - if (possibleUser.isDefined) { - Redirect(routes.MainMenuController.mainMenu()) - } else { - Ok(views.html.main("Login")(views.html.login.login())) - } - } else { - Ok(views.html.main("Login")(views.html.login.login())) - } - } - } - def login_Post(): Action[AnyContent] = { Action { implicit request => val jsonBody = request.body.asJson @@ -51,12 +38,17 @@ class UserController @Inject()( val possibleUser = userManager.authenticate(username.get, password.get) if (possibleUser.isDefined) { Ok(Json.obj( - "status" -> "success", - "redirectUrl" -> routes.MainMenuController.mainMenu().url, - "content" -> views.html.mainmenu.creategame(possibleUser).toString - )).withCookies( - Cookie("sessionId", sessionManager.createSession(possibleUser.get)) - ) + "user" -> Json.obj( + "id" -> possibleUser.get.id, + "username" -> possibleUser.get.name + ) + )).withCookies(Cookie( + name = "accessToken", + value = sessionManager.createSession(possibleUser.get), + httpOnly = true, + secure = false, + sameSite = Some(Lax) + )) } else { Unauthorized("Invalid username or password") } @@ -65,14 +57,21 @@ class UserController @Inject()( } } } + + def getUserInfo(): Action[AnyContent] = authAction { implicit request: AuthenticatedRequest[AnyContent] => + val user: User = request.user + Ok(Json.obj( + "id" -> user.id, + "username" -> user.name + )) + } - // Pass the request-handling function directly to authAction (no nested Action) - def logout(): Action[AnyContent] = authAction { implicit request: AuthenticatedRequest[AnyContent] => - val sessionCookie = request.cookies.get("sessionId") + def logoutPost(): Action[AnyContent] = authAction { implicit request: AuthenticatedRequest[AnyContent] => + val sessionCookie = request.cookies.get("accessToken") if (sessionCookie.isDefined) { sessionManager.invalidateSession(sessionCookie.get.value) } - Redirect(routes.UserController.login()).discardingCookies(DiscardingCookie("sessionId")) + NoContent.discardingCookies(DiscardingCookie("accessToken")) } } \ No newline at end of file diff --git a/knockoutwhistweb/app/logic/user/SessionManager.scala b/knockoutwhistweb/app/logic/user/SessionManager.scala index 4096ffb..d778b6b 100644 --- a/knockoutwhistweb/app/logic/user/SessionManager.scala +++ b/knockoutwhistweb/app/logic/user/SessionManager.scala @@ -9,6 +9,7 @@ trait SessionManager { def createSession(user: User): String + def getUserBySession(sessionId: String): Option[User] def invalidateSession(sessionId: String): Unit diff --git a/knockoutwhistweb/app/views/mainmenu/navbar.scala.html b/knockoutwhistweb/app/views/mainmenu/navbar.scala.html index 6ddc4c0..8a9ab75 100644 --- a/knockoutwhistweb/app/views/mainmenu/navbar.scala.html +++ b/knockoutwhistweb/app/views/mainmenu/navbar.scala.html @@ -45,15 +45,9 @@
  • Settings
  • -
  • Logout
  • - } else { -
    - Login - Sign Up -
    } diff --git a/knockoutwhistweb/conf/application.conf b/knockoutwhistweb/conf/application.conf index 1ee3721..3d41095 100644 --- a/knockoutwhistweb/conf/application.conf +++ b/knockoutwhistweb/conf/application.conf @@ -13,3 +13,12 @@ auth { publicKeyFile = ${?PUBLIC_KEY_FILE} publicKeyPem = ${?PUBLIC_KEY_PEM} } + +play.filters.enabled += "play.filters.cors.CORSFilter" + +play.filters.cors { + allowedOrigins = ["http://localhost:5173"] + allowedCredentials = true + allowedHttpMethods = ["GET", "POST", "PUT", "DELETE", "OPTIONS"] + allowedHttpHeaders = ["Accept", "Content-Type", "Origin", "X-Requested-With"] +} diff --git a/knockoutwhistweb/conf/routes b/knockoutwhistweb/conf/routes index ac9c094..f73842b 100644 --- a/knockoutwhistweb/conf/routes +++ b/knockoutwhistweb/conf/routes @@ -18,10 +18,9 @@ POST /createGame controllers.MainMenuController.createGame() POST /joinGame controllers.MainMenuController.joinGame() # User authentication routes -GET /login controllers.UserController.login() POST /login controllers.UserController.login_Post() - -GET /logout controllers.UserController.logout() +POST /logout controllers.UserController.logoutPost() +GET /userInfo controllers.UserController.getUserInfo() # In-game routes GET /game/:id controllers.IngameController.game(id: String)