From 5359f38ba75e76b6ce3a5a535d12bb8250da2579 Mon Sep 17 00:00:00 2001 From: Zainuddin Mohammed Date: Thu, 17 Oct 2024 16:14:53 -0500 Subject: [PATCH] Added user.put.ts for changing username, nearly identical to user.post.ts except with update() instead of create(). Changing username is functional. --- cert-dev.pem | 19 ++++ components/BottomNavBar.vue | 12 ++ components/LogInOverlay.vue | 56 ++++----- components/LogInSignUp.vue | 14 ++- components/TopNavBar.vue | 14 +-- components/TopRightNavbar.vue | 38 +++---- pages/index.vue | 208 +++++++++++++++++----------------- prisma/schema.prisma | 26 +++-- server/api/user.put.ts | 38 +++++++ 9 files changed, 253 insertions(+), 172 deletions(-) create mode 100644 cert-dev.pem create mode 100644 server/api/user.put.ts diff --git a/cert-dev.pem b/cert-dev.pem new file mode 100644 index 0000000..463b004 --- /dev/null +++ b/cert-dev.pem @@ -0,0 +1,19 @@ +-----BEGIN CERTIFICATE----- +MIIDHTCCAgWgAwIBAgIJMSxRVGwPXrarMA0GCSqGSIb3DQEBCwUAMCwxKjAoBgNV +BAMTIWRldi11NGptbThnaTYxeXJ6eXJrLnVzLmF1dGgwLmNvbTAeFw0yNDA0MTYx +NjQ0MDBaFw0zNzEyMjQxNjQ0MDBaMCwxKjAoBgNVBAMTIWRldi11NGptbThnaTYx +eXJ6eXJrLnVzLmF1dGgwLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC +ggEBAM9Jg2WQCaB1da7jd6h2HhSB1Ujz7pP//0bnzzvXRJe2jXC0K78WWZMx56Je +mDbt02e9jr/AYP/IdPZ6alxQhzRu3LQSe+CKmrNyKQ3qVsHO0LpfpT62F28iyR1+ +9StOv5p6du80WLRuOLOLf52bnfvOZtKiGMkSmQIFks7rlf6+5kttIvC+rC7EEzjX +c8HaHO/hhVQzYbZDXFpee6brMscoqwOGfRB1jfYrn6Qnwn7CTiS4IgNiZwsMfvbu +K7DBpNTipMOPYMK3kQ3J9tQaLBu2qtdVq8y8+2mZn2anOr+9Ge9eLCR367vZVP9M +HniCfKD+MJrEK/4u8P9Bj+90PHMCAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAd +BgNVHQ4EFgQUcppxfd/gtygwJpRxZCq8uD7R0UcwDgYDVR0PAQH/BAQDAgKEMA0G +CSqGSIb3DQEBCwUAA4IBAQBJrHzC8FXvJKJ/zweygVQdFsVds68ysclP2jDf2Dcr +wIDomsj0EsaFBoY60xxsf8Te3AU9wgt+1CLARhm34ilSnCmsXNYphcosm6vlMZd/ +WHmU2Zm3tNO3l/soq0VAPaZrYij3JmQe1ag8Ei4RCoQuL2/5CeBZrmOMrmlyMaSZ +rFW0ZGXBMNtwyyHYsKA+ygXj6Sh86TQSnjTzBxopTSp5gO+onddP7GHtB9Pf9mpg +LplnjMsshkxPYzW0Ki3nfNPPJrlNDDBbEgJKlHklItZcUmZvyVics2jVA7s769nM +QTDcmkX2s8+k7aSH033twUJKpzFLCIam0eK689JyVKFC +-----END CERTIFICATE----- \ No newline at end of file diff --git a/components/BottomNavBar.vue b/components/BottomNavBar.vue index 60cbe93..a43299e 100644 --- a/components/BottomNavBar.vue +++ b/components/BottomNavBar.vue @@ -9,23 +9,35 @@

Help

+
+

Change Username

+
+ \ No newline at end of file diff --git a/components/LogInOverlay.vue b/components/LogInOverlay.vue index 647b5cb..73477b1 100644 --- a/components/LogInOverlay.vue +++ b/components/LogInOverlay.vue @@ -1,42 +1,46 @@ diff --git a/components/LogInSignUp.vue b/components/LogInSignUp.vue index 596c1e5..05e9998 100644 --- a/components/LogInSignUp.vue +++ b/components/LogInSignUp.vue @@ -1,11 +1,13 @@ + + diff --git a/pages/index.vue b/pages/index.vue index c316458..8809626 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -1,21 +1,21 @@ \ No newline at end of file diff --git a/prisma/schema.prisma b/prisma/schema.prisma index e77af37..ea4a34e 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -7,26 +7,34 @@ generator client { provider = "prisma-client-js" } +generator erd { + provider = "prisma-erd-generator" +} + model Player { - user_id String @id - username String @unique - email String @unique + user_id String @id + username String @unique + email String @unique + wins Int @default(0) + goals Int @default(0) + games Int @default(0) matches PlayersInMatches[] } model Match { - id Int @id @default(autoincrement()) - datetime DateTime @default(now()) + id Int @id @default(autoincrement()) + datetime DateTime @default(now()) players PlayersInMatches[] } model PlayersInMatches { - player Player @relation(fields: [playerID], references: [user_id]) - playerID String + player Player @relation(fields: [playerID], references: [user_id]) + playerID String playerScore Int - match Match @relation(fields: [matchID], references: [id]) - matchID Int + match Match @relation(fields: [matchID], references: [id]) + matchID Int + @@id([playerID, matchID]) } diff --git a/server/api/user.put.ts b/server/api/user.put.ts new file mode 100644 index 0000000..25bc61c --- /dev/null +++ b/server/api/user.put.ts @@ -0,0 +1,38 @@ + +// + +export default defineEventHandler(async (event) => { + + const prisma = event.context.prisma + const claims = event.context.claims + + const body = await readBody(event) + const username = body.username + + const existingUsername = await prisma.player.findFirst({ + where: { + username + } + }) + + let msg + if(!existingUsername){ + const player = await prisma.player.update({ + where: { + user_id: claims['sub'], + }, + data: { + username, + } + }) + msg = 200 + setCookie(event, 'sruser', username) + } else { + msg = 403 + } + + return msg +}) + +// 403 - username is not unique +// 200 went okay \ No newline at end of file