From a0bf718e1d9b4628075e061edcfb4f6609362213 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Thu, 23 Mar 2023 15:15:11 +0900 Subject: [PATCH] Add guest profiles --- .env.example | 17 ++++---- .../migrations/20230323054114_/migration.sql | 2 + .../migrations/20230323061423_/migration.sql | 2 + back/prisma/schema.prisma | 2 + back/src/auth/auth.controller.ts | 39 +++++++++++++++++++ back/src/auth/dto/profile.dto.ts | 16 ++++++++ back/src/users/users.controller.ts | 13 ------- back/src/users/users.service.ts | 12 ++++++ 8 files changed, 83 insertions(+), 20 deletions(-) create mode 100644 back/prisma/migrations/20230323054114_/migration.sql create mode 100644 back/prisma/migrations/20230323061423_/migration.sql create mode 100644 back/src/auth/dto/profile.dto.ts diff --git a/.env.example b/.env.example index c3123a6..890b8ff 100644 --- a/.env.example +++ b/.env.example @@ -1,7 +1,10 @@ -POSTGRES_USER= -POSTGRES_PASSWORD= -POSTGRES_NAME= -POSTGRES_HOST= -DATABASE_URL= -JWT_SECRET= -API_URL= \ No newline at end of file +POSTGRES_USER=user +POSTGRES_PASSWORD=eip +POSTGRES_NAME=chromacase +POSTGRES_HOST=db +DATABASE_URL=postgresql://user:eip@db:5432/chromacase +JWT_SECRET=wow +POSTGRES_DB=chromacase +API_URL=http://localhost:80/api +SCORO_URL=ws://localhost:6543 + diff --git a/back/prisma/migrations/20230323054114_/migration.sql b/back/prisma/migrations/20230323054114_/migration.sql new file mode 100644 index 0000000..cf267c1 --- /dev/null +++ b/back/prisma/migrations/20230323054114_/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "User" ADD COLUMN "isGuest" BOOLEAN NOT NULL DEFAULT false; diff --git a/back/prisma/migrations/20230323061423_/migration.sql b/back/prisma/migrations/20230323061423_/migration.sql new file mode 100644 index 0000000..804ec95 --- /dev/null +++ b/back/prisma/migrations/20230323061423_/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "User" ADD COLUMN "partyPlayed" INTEGER NOT NULL DEFAULT 0; diff --git a/back/prisma/schema.prisma b/back/prisma/schema.prisma index 037006c..2bbe9a1 100644 --- a/back/prisma/schema.prisma +++ b/back/prisma/schema.prisma @@ -14,6 +14,8 @@ model User { username String @unique password String email String + isGuest Boolean @default(false) + partyPlayed Int @default(0) LessonHistory LessonHistory[] SongHistory SongHistory[] searchHistory SearchHistory[] diff --git a/back/src/auth/auth.controller.ts b/back/src/auth/auth.controller.ts index 446ee6b..6e868f2 100644 --- a/back/src/auth/auth.controller.ts +++ b/back/src/auth/auth.controller.ts @@ -8,6 +8,7 @@ import { Delete, BadRequestException, HttpCode, + Put, } from '@nestjs/common'; import { AuthService } from './auth.service'; import { JwtAuthGuard } from './jwt-auth.guard'; @@ -25,6 +26,7 @@ import { import { User } from '../models/user'; import { JwtToken } from './models/jwt'; import { LoginDto } from './dto/login.dto'; +import { Profile } from './dto/profile.dto'; @ApiTags('auth') @Controller('auth') @@ -51,6 +53,18 @@ export class AuthController { return this.authService.login(req.user); } + @HttpCode(200) + @UseGuards(LocalAuthGuard) + @Post('guest') + async guest(): Promise { + try { + const user = await this.usersService.createGuest(); + return this.authService.login(user); + } catch { + throw new BadRequestException(); + } + } + @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOkResponse({ description: 'Successfully logged in', type: User }) @@ -60,6 +74,31 @@ export class AuthController { return req.user; } + @UseGuards(JwtAuthGuard) + @ApiBearerAuth() + @ApiOkResponse({ description: 'Successfully edited profile', type: User }) + @ApiUnauthorizedResponse({ description: 'Invalid token' }) + @Put('me') + editProfile( + @Request() req: any, + @Body() profile: Partial, + ): Promise { + return this.usersService.updateUser({ + where: { id: req.user.id }, + data: { + // If every field is present, the account is no longuer a guest profile. + // TODO: Add some condition to change a guest account to a normal account, like require a subscription or something like that. + isGuest: + profile.email && profile.username && profile.password + ? false + : undefined, + username: profile.username, + password: profile.password, + email: profile.email, + }, + }); + } + @UseGuards(JwtAuthGuard) @ApiBearerAuth() @ApiOkResponse({ description: 'Successfully deleted', type: User }) diff --git a/back/src/auth/dto/profile.dto.ts b/back/src/auth/dto/profile.dto.ts new file mode 100644 index 0000000..f1115c9 --- /dev/null +++ b/back/src/auth/dto/profile.dto.ts @@ -0,0 +1,16 @@ +import { IsNotEmpty } from 'class-validator'; +import { ApiProperty } from '@nestjs/swagger'; + +export class Profile { + @ApiProperty() + @IsNotEmpty() + username: string; + + @ApiProperty() + @IsNotEmpty() + password: string; + + @ApiProperty() + @IsNotEmpty() + email: string; +} diff --git a/back/src/users/users.controller.ts b/back/src/users/users.controller.ts index ca5b5f2..c505546 100644 --- a/back/src/users/users.controller.ts +++ b/back/src/users/users.controller.ts @@ -3,14 +3,12 @@ import { Get, Post, Body, - Patch, Param, Delete, NotFoundException, } from '@nestjs/common'; import { UsersService } from './users.service'; import { CreateUserDto } from './dto/create-user.dto'; -import { UpdateUserDto } from './dto/update-user.dto'; import { ApiNotFoundResponse, ApiTags } from '@nestjs/swagger'; import { User } from 'src/models/user'; @@ -37,17 +35,6 @@ export class UsersController { return ret; } - @Patch(':id') - update( - @Param('id') id: string, - @Body() updateUserDto: UpdateUserDto, - ): Promise { - return this.usersService.updateUser({ - where: { id: +id }, - data: updateUserDto, - }); - } - @Delete(':id') remove(@Param('id') id: string): Promise { return this.usersService.deleteUser({ id: +id }); diff --git a/back/src/users/users.service.ts b/back/src/users/users.service.ts index 403c927..bd19078 100644 --- a/back/src/users/users.service.ts +++ b/back/src/users/users.service.ts @@ -39,6 +39,18 @@ export class UsersService { }); } + async createGuest(): Promise { + return this.prisma.user.create({ + data: { + username: 'Guest', + isGuest: true, + // Not realyl clean but better than a separate table or breaking the api by adding nulls. + email: '', + password: '', + }, + }); + } + async updateUser(params: { where: Prisma.UserWhereUniqueInput; data: Prisma.UserUpdateInput;