From 6d7d526ec2c43c180aea02c842a7929993fed65e Mon Sep 17 00:00:00 2001 From: nicolas-arnaud Date: Sun, 5 Mar 2023 00:31:27 +0100 Subject: [PATCH] add auth protection, login and logout and fixed username update --- back/volume/src/auth/auth.controller.ts | 2 +- back/volume/src/main.ts | 4 +- back/volume/src/users/users.controller.ts | 20 ++- back/volume/src/users/users.service.ts | 11 +- front/volume/package-lock.json | 152 +++++++++++++++++++++ front/volume/package.json | 1 + front/volume/src/App.svelte | 142 ++++++++++--------- front/volume/src/Auth.ts | 37 +++++ front/volume/src/components/Friends.svelte | 34 +++-- front/volume/src/components/Profile.svelte | 10 +- 10 files changed, 322 insertions(+), 91 deletions(-) create mode 100644 front/volume/src/Auth.ts diff --git a/back/volume/src/auth/auth.controller.ts b/back/volume/src/auth/auth.controller.ts index 2856a91..1de89b7 100644 --- a/back/volume/src/auth/auth.controller.ts +++ b/back/volume/src/auth/auth.controller.ts @@ -29,7 +29,7 @@ export class AuthController { } @Get('out') - @Redirect('/') + @Redirect('http://' + process.env.HOST + ':' + process.env.FRONT_PORT + '/') logOut (@Req() req: Request) { req.logOut(function (err) { if (err) return err diff --git a/back/volume/src/main.ts b/back/volume/src/main.ts index ab171d8..9988e43 100644 --- a/back/volume/src/main.ts +++ b/back/volume/src/main.ts @@ -33,7 +33,7 @@ async function bootstrap () { app.use(passport.session()) app.enableCors(cors) app.useWebSocketAdapter(new WsAdapter(app)) - await app.listen(port) - logger.log(`Application listening on port ${port}`) + await app.listen(port) + logger.log(`Application listening on port ${port}`) } bootstrap() diff --git a/back/volume/src/users/users.controller.ts b/back/volume/src/users/users.controller.ts index bd2ec55..840ebf7 100644 --- a/back/volume/src/users/users.controller.ts +++ b/back/volume/src/users/users.controller.ts @@ -54,7 +54,7 @@ export class UsersController { async getInvits (@FtUser() profile: Profile) { return await this.usersService.getInvits(profile.id) } - + @Post('avatar') @UseGuards(AuthenticatedGuard) @UseInterceptors( @@ -92,6 +92,22 @@ export class UsersController { return await this.getAvatarById(profile.id, response); } + @Get('user/:name') + async getUserByName( + @Param('name') username: string + ): Promise { + return await this.usersService.findUserByName(username); + } + + @Post('invit/:id') + @UseGuards(AuthenticatedGuard) + async invitUser ( + @FtUser() profile: Profile, + @Param('id', ParseIntPipe) id: number + ) { + return await this.usersService.invit(profile.id, id) + } + @Get('avatar/:id') async getAvatarById ( @Param('id', ParseIntPipe) ftId: number, @@ -124,7 +140,7 @@ export class UsersController { async create (@Body() payload: UserDto, @FtUser() profile: Profile) { const user = await this.usersService.findUser(profile.id) if (user) { - return await this.usersService.update(user.id, payload) + return await this.usersService.update(user, payload) } else { return await this.usersService.create(payload) } diff --git a/back/volume/src/users/users.service.ts b/back/volume/src/users/users.service.ts index 6073ba2..ae149d3 100644 --- a/back/volume/src/users/users.service.ts +++ b/back/volume/src/users/users.service.ts @@ -44,11 +44,9 @@ export class UsersService { .getMany() } - async update (ftId: number, changes: UserDto):Promise < User | null> { - const updatedUser = await this.findUser(ftId) - if (!updatedUser) return null - this.usersRepository.merge(updatedUser, changes) - return await this.usersRepository.save(updatedUser) + async update (user: User, changes: UserDto):Promise < User | null> { + this.usersRepository.merge(user, changes) + return await this.usersRepository.save(user) } async addAvatar (ftId: number, filename: string) { @@ -87,8 +85,7 @@ export class UsersService { return new NotFoundException( `Error: user id ${targetFtId} isn't in our db.` ) - } - const id = user.followers.findIndex( + } const id = user.followers.findIndex( (follower) => follower.ftId === targetFtId ) if (id != -1) { diff --git a/front/volume/package-lock.json b/front/volume/package-lock.json index d0718b7..dcf6c09 100644 --- a/front/volume/package-lock.json +++ b/front/volume/package-lock.json @@ -10,6 +10,7 @@ "dependencies": { "@sveltejs/vite-plugin-svelte": "^2.0.2", "@tsconfig/svelte": "^3.0.0", + "axios": "^1.3.4", "svelte": "^3.55.1", "vite": "^4.1.0" }, @@ -469,6 +470,21 @@ "node": ">= 8" } }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" + }, + "node_modules/axios": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.3.4.tgz", + "integrity": "sha512-toYm+Bsyl6VC5wSkfkbbNB6ROv7KY93PEBBL6xyDczaIHasAiv4wPqQ/c4RjoQzipxRD2W5g21cOqQulZ7rHwQ==", + "dependencies": { + "follow-redirects": "^1.15.0", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, "node_modules/balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", @@ -551,6 +567,17 @@ "fsevents": "~2.3.2" } }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -581,6 +608,14 @@ "node": ">=0.10.0" } }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "engines": { + "node": ">=0.4.0" + } + }, "node_modules/detect-indent": { "version": "6.1.0", "resolved": "https://registry.npmjs.org/detect-indent/-/detect-indent-6.1.0.tgz", @@ -669,6 +704,38 @@ "node": ">=8" } }, + "node_modules/follow-redirects": { + "version": "1.15.2", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.2.tgz", + "integrity": "sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, + "node_modules/form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -868,6 +935,25 @@ "node": ">=8.6" } }, + "node_modules/mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "dependencies": { + "mime-db": "1.52.0" + }, + "engines": { + "node": ">= 0.6" + } + }, "node_modules/min-indent": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/min-indent/-/min-indent-1.0.1.tgz", @@ -1044,6 +1130,11 @@ "svelte": "^3.2.0" } }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + }, "node_modules/queue-microtask": { "version": "1.2.3", "resolved": "https://registry.npmjs.org/queue-microtask/-/queue-microtask-1.2.3.tgz", @@ -1682,6 +1773,21 @@ "picomatch": "^2.0.4" } }, + "asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" + }, + "axios": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.3.4.tgz", + "integrity": "sha512-toYm+Bsyl6VC5wSkfkbbNB6ROv7KY93PEBBL6xyDczaIHasAiv4wPqQ/c4RjoQzipxRD2W5g21cOqQulZ7rHwQ==", + "requires": { + "follow-redirects": "^1.15.0", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, "balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", @@ -1741,6 +1847,14 @@ "readdirp": "~3.6.0" } }, + "combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "requires": { + "delayed-stream": "~1.0.0" + } + }, "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -1760,6 +1874,11 @@ "resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-4.3.0.tgz", "integrity": "sha512-z2wJZXrmeHdvYJp/Ux55wIjqo81G5Bp4c+oELTW+7ar6SogWHajt5a9gO3s3IDaGSAXjDk0vlQKN3rms8ab3og==" }, + "delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==" + }, "detect-indent": { "version": "6.1.0", "resolved": "https://registry.npmjs.org/detect-indent/-/detect-indent-6.1.0.tgz", @@ -1832,6 +1951,21 @@ "to-regex-range": "^5.0.1" } }, + "follow-redirects": { + "version": "1.15.2", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.2.tgz", + "integrity": "sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==" + }, + "form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "requires": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + } + }, "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -1979,6 +2113,19 @@ "picomatch": "^2.3.1" } }, + "mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==" + }, + "mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "requires": { + "mime-db": "1.52.0" + } + }, "min-indent": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/min-indent/-/min-indent-1.0.1.tgz", @@ -2094,6 +2241,11 @@ "dev": true, "requires": {} }, + "proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + }, "queue-microtask": { "version": "1.2.3", "resolved": "https://registry.npmjs.org/queue-microtask/-/queue-microtask-1.2.3.tgz", diff --git a/front/volume/package.json b/front/volume/package.json index 2fb6180..970ab08 100644 --- a/front/volume/package.json +++ b/front/volume/package.json @@ -20,6 +20,7 @@ "dependencies": { "@sveltejs/vite-plugin-svelte": "^2.0.2", "@tsconfig/svelte": "^3.0.0", + "axios": "^1.3.4", "svelte": "^3.55.1", "vite": "^4.1.0" } diff --git a/front/volume/src/App.svelte b/front/volume/src/App.svelte index 8c0c521..8e9c22c 100644 --- a/front/volume/src/App.svelte +++ b/front/volume/src/App.svelte @@ -1,4 +1,5 @@
- - {#if isChannelsOpen} - {#if selectedChannel} -
(selectedChannel = undefined)} on:keydown={() => (selectedChannel = undefined)}> - -
- {/if} - {#if !selectedChannel} -
(isChannelsOpen = false)} - on:keydown={() => (isChannelsOpen = false)} - > - -
+
+ {#if $store === null} +

+ {:else} +

+ + {#if isChannelsOpen} + {#if selectedChannel} +
(selectedChannel = undefined)} + on:keydown={() => (selectedChannel = undefined)} + > + +
+ {/if} + {#if !selectedChannel} +
(isChannelsOpen = false)} + on:keydown={() => (isChannelsOpen = false)} + > + +
+ {/if} + {/if} + {#if isSpectateOpen} +
(isSpectateOpen = false)} + on:keydown={() => (isSpectateOpen = false)} + > + +
+ {/if} + {#if isFriendOpen} +
(isFriendOpen = false)} + on:keydown={() => (isFriendOpen = false)} + > + +
+ {/if} + {#if isHistoryOpen} +
(isHistoryOpen = false)} + on:keydown={() => (isHistoryOpen = false)} + > + +
+ {/if} + {#if isProfileOpen} +
(isProfileOpen = false)} + on:keydown={() => (isProfileOpen = false)} + > + +
+ {/if} + + {/if} - {/if} - {#if isSpectateOpen} -
(isSpectateOpen = false)} - on:keydown={() => (isSpectateOpen = false)} - > - -
- {/if} - {#if isFriendOpen} -
(isFriendOpen = false)} - on:keydown={() => (isFriendOpen = false)} - > - -
- {/if} - {#if isHistoryOpen} -
(isHistoryOpen = false)} - on:keydown={() => (isHistoryOpen = false)} - > - -
- {/if} - {#if isProfileOpen} -
(isProfileOpen = false)} - on:keydown={() => (isProfileOpen = false)} - > - -
- {/if} - - +