diff --git a/backend/Controllers/UserController.cs b/backend/Controllers/UserController.cs index 163ca91..c6a6f14 100644 --- a/backend/Controllers/UserController.cs +++ b/backend/Controllers/UserController.cs @@ -18,6 +18,10 @@ public class UserController : ControllerBase return BadRequest("Username and password required"); } + if (ApplicationState.DbContext!.Users.FirstOrDefault(user => user.Username == input["username"]!.ToString()) != null) { + return BadRequest("User already exists"); + } + // Hash password var passwordHasher = new PasswordHasher(); string hashedPassword = passwordHasher.HashPassword(null, input["password"]!.ToString()); @@ -81,4 +85,49 @@ public class UserController : ControllerBase return Ok(); } + + [HttpPost("Logout")] + public IActionResult LogOut() + { + // Validate + if (Request.Cookies["session"] == null) { + return BadRequest("You are not logged in"); + } + + // Get user + var user = ApplicationState.DbContext!.Users.FirstOrDefault(user => user.SessionToken == Request.Cookies["session"]!.ToString()); + if (user == null) { + return BadRequest("Invalid session token"); + } + + // Log out + user.SessionToken = null; + ApplicationState.DbContext!.SaveChanges(); + + Response.Cookies.Delete("session"); + + return Ok(); + } + + [HttpGet("UserInfo")] + public IActionResult UserInfo() + { + // Validate + if (Request.Cookies["session"] == null) { + return BadRequest("You are not logged in"); + } + + // Get user + var user = ApplicationState.DbContext!.Users.FirstOrDefault(user => user.SessionToken == Request.Cookies["session"]!.ToString()); + if (user == null) { + return BadRequest("Invalid session token"); + } + + var data = new { + username = user.Username, + touchCode = user.TouchCode, + }; + + return new JsonResult(data); + } } diff --git a/frontend/package-lock.json b/frontend/package-lock.json index 566626d..b28040b 100644 --- a/frontend/package-lock.json +++ b/frontend/package-lock.json @@ -8,8 +8,10 @@ "name": "slik-dispenser", "version": "0.0.0", "dependencies": { + "@vueuse/core": "^10.7.0", "vue": "^3.3.4", - "vue-router": "^4.2.5" + "vue-router": "^4.2.5", + "vuex": "^4.1.0" }, "devDependencies": { "@vitejs/plugin-vue": "^4.4.0", @@ -384,6 +386,11 @@ "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.15.tgz", "integrity": "sha512-eF2rxCRulEKXHTRiDrDy6erMYWqNw4LPdQ8UQA4huuxaQsVeRPFl2oM8oDGxMFhJUWZf9McpLtJasDDZb/Bpeg==" }, + "node_modules/@types/web-bluetooth": { + "version": "0.0.20", + "resolved": "https://registry.npmjs.org/@types/web-bluetooth/-/web-bluetooth-0.0.20.tgz", + "integrity": "sha512-g9gZnnXVq7gM7v3tJCWV/qw7w+KeOlSHAhgF9RytFyifW6AF61hdT2ucrYhPq9hLs5JIryeupHV3qGk95dH9ow==" + }, "node_modules/@vitejs/plugin-vue": { "version": "4.5.0", "resolved": "https://registry.npmjs.org/@vitejs/plugin-vue/-/plugin-vue-4.5.0.tgz", @@ -504,6 +511,89 @@ "resolved": "https://registry.npmjs.org/@vue/shared/-/shared-3.3.9.tgz", "integrity": "sha512-ZE0VTIR0LmYgeyhurPTpy4KzKsuDyQbMSdM49eKkMnT5X4VfFBLysMzjIZhLEFQYjjOVVfbvUDHckwjDFiO2eA==" }, + "node_modules/@vueuse/core": { + "version": "10.7.0", + "resolved": "https://registry.npmjs.org/@vueuse/core/-/core-10.7.0.tgz", + "integrity": "sha512-4EUDESCHtwu44ZWK3Gc/hZUVhVo/ysvdtwocB5vcauSV4B7NiGY5972WnsojB3vRNdxvAt7kzJWE2h9h7C9d5w==", + "dependencies": { + "@types/web-bluetooth": "^0.0.20", + "@vueuse/metadata": "10.7.0", + "@vueuse/shared": "10.7.0", + "vue-demi": ">=0.14.6" + }, + "funding": { + "url": "https://github.com/sponsors/antfu" + } + }, + "node_modules/@vueuse/core/node_modules/vue-demi": { + "version": "0.14.6", + "resolved": "https://registry.npmjs.org/vue-demi/-/vue-demi-0.14.6.tgz", + "integrity": "sha512-8QA7wrYSHKaYgUxDA5ZC24w+eHm3sYCbp0EzcDwKqN3p6HqtTCGR/GVsPyZW92unff4UlcSh++lmqDWN3ZIq4w==", + "hasInstallScript": true, + "bin": { + "vue-demi-fix": "bin/vue-demi-fix.js", + "vue-demi-switch": "bin/vue-demi-switch.js" + }, + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/antfu" + }, + "peerDependencies": { + "@vue/composition-api": "^1.0.0-rc.1", + "vue": "^3.0.0-0 || ^2.6.0" + }, + "peerDependenciesMeta": { + "@vue/composition-api": { + "optional": true + } + } + }, + "node_modules/@vueuse/metadata": { + "version": "10.7.0", + "resolved": "https://registry.npmjs.org/@vueuse/metadata/-/metadata-10.7.0.tgz", + "integrity": "sha512-GlaH7tKP2iBCZ3bHNZ6b0cl9g0CJK8lttkBNUX156gWvNYhTKEtbweWLm9rxCPIiwzYcr/5xML6T8ZUEt+DkvA==", + "funding": { + "url": "https://github.com/sponsors/antfu" + } + }, + "node_modules/@vueuse/shared": { + "version": "10.7.0", + "resolved": "https://registry.npmjs.org/@vueuse/shared/-/shared-10.7.0.tgz", + "integrity": "sha512-kc00uV6CiaTdc3i1CDC4a3lBxzaBE9AgYNtFN87B5OOscqeWElj/uza8qVDmk7/U8JbqoONLbtqiLJ5LGRuqlw==", + "dependencies": { + "vue-demi": ">=0.14.6" + }, + "funding": { + "url": "https://github.com/sponsors/antfu" + } + }, + "node_modules/@vueuse/shared/node_modules/vue-demi": { + "version": "0.14.6", + "resolved": "https://registry.npmjs.org/vue-demi/-/vue-demi-0.14.6.tgz", + "integrity": "sha512-8QA7wrYSHKaYgUxDA5ZC24w+eHm3sYCbp0EzcDwKqN3p6HqtTCGR/GVsPyZW92unff4UlcSh++lmqDWN3ZIq4w==", + "hasInstallScript": true, + "bin": { + "vue-demi-fix": "bin/vue-demi-fix.js", + "vue-demi-switch": "bin/vue-demi-switch.js" + }, + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/antfu" + }, + "peerDependencies": { + "@vue/composition-api": "^1.0.0-rc.1", + "vue": "^3.0.0-0 || ^2.6.0" + }, + "peerDependenciesMeta": { + "@vue/composition-api": { + "optional": true + } + } + }, "node_modules/csstype": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/csstype/-/csstype-3.1.2.tgz", @@ -737,6 +827,17 @@ "peerDependencies": { "vue": "^3.2.0" } + }, + "node_modules/vuex": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/vuex/-/vuex-4.1.0.tgz", + "integrity": "sha512-hmV6UerDrPcgbSy9ORAtNXDr9M4wlNP4pEFKye4ujJF8oqgFFuxDCdOLS3eNoRTtq5O3hoBDh9Doj1bQMYHRbQ==", + "dependencies": { + "@vue/devtools-api": "^6.0.0-beta.11" + }, + "peerDependencies": { + "vue": "^3.2.0" + } } } } diff --git a/frontend/package.json b/frontend/package.json index e2b3870..574e648 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -8,8 +8,10 @@ "preview": "vite preview" }, "dependencies": { + "@vueuse/core": "^10.7.0", "vue": "^3.3.4", - "vue-router": "^4.2.5" + "vue-router": "^4.2.5", + "vuex": "^4.1.0" }, "devDependencies": { "@vitejs/plugin-vue": "^4.4.0", diff --git a/frontend/src/App.vue b/frontend/src/App.vue index af6f58f..e8e6271 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -1,23 +1,49 @@