Add guest profiles

This commit is contained in:
2023-03-23 15:15:11 +09:00
committed by Bluub
parent cf3c9b8c86
commit a0bf718e1d
8 changed files with 83 additions and 20 deletions

View File

@@ -1,7 +1,10 @@
POSTGRES_USER=
POSTGRES_PASSWORD=
POSTGRES_NAME=
POSTGRES_HOST=
DATABASE_URL=
JWT_SECRET=
API_URL=
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

View File

@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "User" ADD COLUMN "isGuest" BOOLEAN NOT NULL DEFAULT false;

View File

@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "User" ADD COLUMN "partyPlayed" INTEGER NOT NULL DEFAULT 0;

View File

@@ -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[]

View File

@@ -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<JwtToken> {
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<Profile>,
): Promise<User> {
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 })

View File

@@ -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;
}

View File

@@ -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<User> {
return this.usersService.updateUser({
where: { id: +id },
data: updateUserDto,
});
}
@Delete(':id')
remove(@Param('id') id: string): Promise<User> {
return this.usersService.deleteUser({ id: +id });

View File

@@ -39,6 +39,18 @@ export class UsersService {
});
}
async createGuest(): Promise<User> {
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;