Files
swiss-datashare/backend/src/user/user.controller.ts

85 lines
2.3 KiB
TypeScript
Raw Normal View History

2022-12-05 10:02:19 +01:00
import {
Body,
Controller,
Delete,
Get,
Param,
Patch,
Post,
2023-03-04 23:40:02 +01:00
Res,
2022-12-05 10:02:19 +01:00
UseGuards,
} from "@nestjs/common";
import { User } from "@prisma/client";
2023-03-04 23:40:02 +01:00
import { Response } from "express";
import { GetUser } from "src/auth/decorator/getUser.decorator";
2022-12-05 10:02:19 +01:00
import { AdministratorGuard } from "src/auth/guard/isAdmin.guard";
import { JwtGuard } from "src/auth/guard/jwt.guard";
2022-12-05 15:53:24 +01:00
import { CreateUserDTO } from "./dto/createUser.dto";
import { UpdateOwnUserDTO } from "./dto/updateOwnUser.dto";
2022-12-05 10:02:19 +01:00
import { UpdateUserDto } from "./dto/updateUser.dto";
2022-10-10 17:58:42 +02:00
import { UserDTO } from "./dto/user.dto";
2022-12-05 10:02:19 +01:00
import { UserSevice } from "./user.service";
@Controller("users")
export class UserController {
2022-12-05 10:02:19 +01:00
constructor(private userService: UserSevice) {}
// Own user operations
@Get("me")
@UseGuards(JwtGuard)
async getCurrentUser(@GetUser() user: User) {
feat(auth): add OAuth2 login (#276) * feat(auth): add OAuth2 login with GitHub and Google * chore(translations): add files for Japanese * fix(auth): fix link function for GitHub * feat(oauth): basic oidc implementation * feat(oauth): oauth guard * fix: disable image optimizations for logo to prevent caching issues with custom logos * fix: memory leak while downloading large files * chore(translations): update translations via Crowdin (#278) * New translations en-us.ts (Japanese) * New translations en-us.ts (Japanese) * New translations en-us.ts (Japanese) * release: 0.18.2 * doc(translations): Add Japanese README (#279) * Added Japanese README. * Added JAPANESE README link to README.md. * Updated Japanese README. * Updated Environment Variable Table. * updated zh-cn README. * feat(oauth): unlink account * refactor(oauth): make providers extensible * fix(oauth): fix discoveryUri error when toggle google-enabled * feat(oauth): add microsoft and discord as oauth provider * docs(oauth): update README.md * docs(oauth): update oauth2-guide.md * set password to null for new oauth users * New translations en-us.ts (Japanese) (#281) * chore(translations): add Polish files * fix(oauth): fix random username and password * feat(oauth): add totp * fix(oauth): fix totp throttle * fix(oauth): fix qrcode and remove comment * feat(oauth): add error page * fix(oauth): i18n of error page * feat(auth): add OAuth2 login * fix(auth): fix link function for GitHub * feat(oauth): basic oidc implementation * feat(oauth): oauth guard * feat(oauth): unlink account * refactor(oauth): make providers extensible * fix(oauth): fix discoveryUri error when toggle google-enabled * feat(oauth): add microsoft and discord as oauth provider * docs(oauth): update README.md * docs(oauth): update oauth2-guide.md * set password to null for new oauth users * fix(oauth): fix random username and password * feat(oauth): add totp * fix(oauth): fix totp throttle * fix(oauth): fix qrcode and remove comment * feat(oauth): add error page * fix(oauth): i18n of error page * refactor: return null instead of `false` in `getIdOfCurrentUser` functiom * feat: show original oauth error if available * refactor: run formatter * refactor(oauth): error message i18n * refactor(oauth): make OAuth token available someone may use it (to revoke token or get other info etc.) also improved the i18n message * chore(oauth): remove unused import * chore: add database migration * fix: missing python installation for nanoid --------- Co-authored-by: Elias Schneider <login@eliasschneider.com> Co-authored-by: ふうせん <10260662+fusengum@users.noreply.github.com>
2023-10-22 22:09:53 +08:00
const userDTO = new UserDTO().from(user);
userDTO.hasPassword = !!user.password;
return userDTO;
}
2022-12-05 10:02:19 +01:00
@Patch("me")
@UseGuards(JwtGuard)
2022-12-05 15:53:24 +01:00
async updateCurrentUser(
@GetUser() user: User,
@Body() data: UpdateOwnUserDTO,
2022-12-05 15:53:24 +01:00
) {
2022-12-05 10:02:19 +01:00
return new UserDTO().from(await this.userService.update(user.id, data));
}
@Delete("me")
@UseGuards(JwtGuard)
2023-03-04 23:40:02 +01:00
async deleteCurrentUser(
@GetUser() user: User,
@Res({ passthrough: true }) response: Response,
2023-03-04 23:40:02 +01:00
) {
response.cookie("access_token", "accessToken", { maxAge: -1 });
response.cookie("refresh_token", "", {
path: "/api/auth/token",
httpOnly: true,
maxAge: -1,
});
2022-12-05 10:02:19 +01:00
return new UserDTO().from(await this.userService.delete(user.id));
}
// Global user operations
@Get()
@UseGuards(JwtGuard, AdministratorGuard)
async list() {
return new UserDTO().fromList(await this.userService.list());
}
@Post()
@UseGuards(JwtGuard, AdministratorGuard)
2022-12-05 15:53:24 +01:00
async create(@Body() user: CreateUserDTO) {
2022-12-05 10:02:19 +01:00
return new UserDTO().from(await this.userService.create(user));
}
@Patch(":id")
@UseGuards(JwtGuard, AdministratorGuard)
async update(@Param("id") id: string, @Body() user: UpdateUserDto) {
return new UserDTO().from(await this.userService.update(id, user));
}
@Delete(":id")
@UseGuards(JwtGuard, AdministratorGuard)
2022-12-05 15:53:24 +01:00
async delete(@Param("id") id: string) {
2022-12-05 10:02:19 +01:00
return new UserDTO().from(await this.userService.delete(id));
}
}