From 369ab63f8a3888369f271e8e120bfcdfc55c427b Mon Sep 17 00:00:00 2001 From: Alexandre POMMIER Date: Mon, 26 Jun 2023 10:47:15 +0200 Subject: [PATCH] clen a bit and add verifypassword who now add to channel if good pass --- containers/api/src/app.controller.ts | 27 +++- containers/api/src/chat/chat.service.ts | 13 +- containers/react/src/DataBase/DataRank.js | 58 --------- .../react/src/DataBase/DataUserProfile.js | 8 -- .../react/src/DataBase/DummyDBWinLoss.js | 37 ------ .../react/src/components/Messages/Modal.tsx | 22 ++-- .../react/src/components/Profile/Logout.tsx | 15 ++- containers/react/src/pages/2fa.tsx | 119 ++---------------- containers/react/src/pages/QrCode.tsx | 46 ------- containers/react/src/pages/Social.tsx | 9 -- containers/react/src/pages/canvas.tsx | 33 ++++- 11 files changed, 93 insertions(+), 294 deletions(-) delete mode 100644 containers/react/src/DataBase/DataRank.js delete mode 100644 containers/react/src/DataBase/DataUserProfile.js delete mode 100644 containers/react/src/DataBase/DummyDBWinLoss.js delete mode 100644 containers/react/src/pages/Social.tsx diff --git a/containers/api/src/app.controller.ts b/containers/api/src/app.controller.ts index 6104ba99..741f1250 100644 --- a/containers/api/src/app.controller.ts +++ b/containers/api/src/app.controller.ts @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/06/17 01:00:00 by apommier #+# #+# */ -/* Updated: 2023/06/26 08:00:13 by apommier ### ########.fr */ +/* Updated: 2023/06/26 10:16:19 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -46,6 +46,21 @@ export class AppController { return await this.userService.findOne(req.user.username); } + @UseGuards(JwtAuthGuard) + @Post('/logout') + async logout(@Request() req, @Body() data: any) { + const user = await this.userService.findOne(req.user.username) + // return await this.userService.refuseInvite(user, data.username); + if(!user) + return ; + if (user.sessionNumber === 1) + { + user.status = 0; + } + user.sessionNumber--; + this.userService.save(user); + } + @UseGuards(JwtAuthGuard) @Post('/user') async getUser(@Body() data: any) { @@ -204,6 +219,7 @@ export class AppController { async addLoss(@Request() req, @Body() data: any) { const user = await this.userService.findOne(req.user.username); user.loss++; + user.status = 1; const Esp = 1 / (1 + Math.pow(10, (data.opRank - user.rank) / this.scaleFactor)) const newRank = user.rank + this.kFactor * (0 - Esp); user.rank = newRank; @@ -267,7 +283,10 @@ export class AppController { @Post('/quit') async setOffline(@Request() req) { const user = await this.userService.findOne(req.user.username); + if (!user) + return ; user.sessionNumber--; + console.log("seesion number=", user.sessionNumber) if (!user.sessionNumber) user.status = 0; await this.userService.save(user); @@ -413,13 +432,15 @@ export class AppController { @UseGuards(JwtAuthGuard) @Post('/password') async setPassword(@Body() data: any) { + // console.log("PASSSSSSSSSSSSSSSSSSSSSSSSSSs= ", data.password); return await this.chatService.setPassword(data.convId, data.password) } @UseGuards(JwtAuthGuard) @Post('/verifyPassword') - async verifyPassword(@Body() data: any) { - return await this.chatService.verifyPassword(data.convId, data.password) + async verifyPassword(@Request() req, @Body() data: any) { + // const user = await this.userService.findOne(req.user.username); + return await this.chatService.verifyPassword(data.convId, data.password, req.user.username) } @UseGuards(JwtAuthGuard) diff --git a/containers/api/src/chat/chat.service.ts b/containers/api/src/chat/chat.service.ts index 39bd418a..7a53445a 100644 --- a/containers/api/src/chat/chat.service.ts +++ b/containers/api/src/chat/chat.service.ts @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/06/17 01:00:25 by apommier #+# #+# */ -/* Updated: 2023/06/26 06:56:08 by apommier ### ########.fr */ +/* Updated: 2023/06/26 10:17:36 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -108,9 +108,16 @@ async setPassword(convId: number, password: string) { this.save(conv); } -async verifyPassword(convId: number, password: string) { +async verifyPassword(convId: number, password: string, username: string) { const conv = await this.findConv(convId); - return await bcrypt.compare(password, conv.password); + const ret = await bcrypt.compare(password, conv.password); + if (ret === true) + { + conv.members = conv.members || []; + conv.members.push(username); + this.save(conv); + } + return ret; } async muteUser(convId: number, username: string, time: string) { diff --git a/containers/react/src/DataBase/DataRank.js b/containers/react/src/DataBase/DataRank.js deleted file mode 100644 index 1bdcaf5e..00000000 --- a/containers/react/src/DataBase/DataRank.js +++ /dev/null @@ -1,58 +0,0 @@ -export const Rank = [ - { - rank: '1', - name: 'jean', - }, - { - rank: '2', - name: 'marc', - }, - { - rank: '3', - name: 'dujardain', - }, - { - rank: '4', - name: 'mom', - }, - { - rank: '5', - name: 'fary', - }, - { - rank: '6', - name: 'aba', - }, - { - rank: '7', - name: 'preach', - }, - { - rank: '1', - name: 'jean', - }, - { - rank: '2', - name: 'marc', - }, - { - rank: '3', - name: 'dujardain', - }, - { - rank: '4', - name: 'mom', - }, - { - rank: '5', - name: 'fary', - }, - { - rank: '6', - name: 'aba', - }, - { - rank: '7', - name: 'preach', - }, -] \ No newline at end of file diff --git a/containers/react/src/DataBase/DataUserProfile.js b/containers/react/src/DataBase/DataUserProfile.js deleted file mode 100644 index 7563a575..00000000 --- a/containers/react/src/DataBase/DataUserProfile.js +++ /dev/null @@ -1,8 +0,0 @@ -import DefaultPic from '../assets/profile.jpg'; - -export const UserProfile = { - Pic: DefaultPic, - UserName: 'Dipper Ratman', - } - -// export default UserProfile \ No newline at end of file diff --git a/containers/react/src/DataBase/DummyDBWinLoss.js b/containers/react/src/DataBase/DummyDBWinLoss.js deleted file mode 100644 index c14f4fb3..00000000 --- a/containers/react/src/DataBase/DummyDBWinLoss.js +++ /dev/null @@ -1,37 +0,0 @@ -export const DBWinLoss = [ - { - title: 'Victory', - score: '10 - 6', - opponent: 'chef bandit' - }, - { - title: 'Defeat', - score: '9 - 10', - opponent: 'ex tueur' - }, - { - title: 'Victory', - score: '10 - 0', - opponent: 'tueur' - }, - { - title: 'Victory', - score: '10 - 9', - opponent: 'boulanger' - }, - { - title: 'Defeat', - score: '3 - 10', - opponent: 'charcutier' - }, - { - title: 'Deafet', - score: '9 - 10', - opponent: 'preach' - }, - { - title: 'Victory', - score: '10 - 9', - opponent: 'aba' - }, -] \ No newline at end of file diff --git a/containers/react/src/components/Messages/Modal.tsx b/containers/react/src/components/Messages/Modal.tsx index 925d6801..272b240a 100644 --- a/containers/react/src/components/Messages/Modal.tsx +++ b/containers/react/src/components/Messages/Modal.tsx @@ -29,7 +29,6 @@ interface ModalProps { } const Modal = ({handleClose}: ModalProps) => { - // const [multi, setMulti] = useState(false); const [selectTags, setSelectTag] = useState([{ id: 1, selectedOption: ''}]); const [selectedOptionArray, setSelectedOptionArray] = useState([]); const [users, setUsers] = useState([]); @@ -39,7 +38,6 @@ const Modal = ({handleClose}: ModalProps) => { const [channel, setChannel] = useState(''); useEffect(()=> { - const getConv = async ()=>{ try { const tmpUsers = await api.get("/users"); @@ -50,7 +48,7 @@ const Modal = ({handleClose}: ModalProps) => { setUsers(tmpUsers.data); setUser(tmpUser.data); setConvs(tmpConvs.data); - } catch(err){ + } catch(err) { console.log(err) } } @@ -76,6 +74,7 @@ const Modal = ({handleClose}: ModalProps) => { try { console.log("channel= ", channel) await api.post("/join", {convId: channel}) + window.location.reload(); } catch(err) { console.log(err); } @@ -108,13 +107,11 @@ const Modal = ({handleClose}: ModalProps) => { e.stopPropagation()} className="modalSetting" - // variant={dropIn} initial="hidden" animate="visible" exit="exit" > - {/*

New Conversation

*/} -
+
{selectTags.map((selectTag) => (
@@ -159,19 +156,14 @@ const Modal = ({handleClose}: ModalProps) => { ) ))} + // {/* {channel.private ? ( + // + // ):("")} */} )} - {/* {channel.private ? ( - - ):("")} */} - -
Join
- - - -
+
) diff --git a/containers/react/src/components/Profile/Logout.tsx b/containers/react/src/components/Profile/Logout.tsx index 81da718b..25fcbd16 100644 --- a/containers/react/src/components/Profile/Logout.tsx +++ b/containers/react/src/components/Profile/Logout.tsx @@ -1,14 +1,25 @@ import React from "react"; - +import api from "../../script/axiosApi" function Logout(){ - + + const logout = async () =>{ + + try { + await api.post("/logout") + } catch (err) { + console.log(err); + } + } + + logout(); localStorage.clear(); const path = 'http://' + process.env.REACT_APP_BASE_URL + '/'; // history(path, { replace: true }); // window.location.replace(path); // window.history.pushState({}, '', path); + window.history.pushState({}, '', path); window.location.reload(); return (<>) diff --git a/containers/react/src/pages/2fa.tsx b/containers/react/src/pages/2fa.tsx index 166b1130..880731c4 100644 --- a/containers/react/src/pages/2fa.tsx +++ b/containers/react/src/pages/2fa.tsx @@ -1,112 +1,18 @@ -import React, { useCallback, useState, useEffect } from 'react'; +import React, { useState, useEffect } from 'react'; import api from '../script/axiosApi.tsx'; -// function DoubleAuth() { - -// // const enabled = await api.get("/2fa"); - -// // const response = await api.get("/2fa"); -// // const enabled = response.data; -// // console.log(`enable= ${enabled.data}`) -// // const enabled = 0; -// let enabled; - -// useEffect(() => { -// async function get2fa() -// { -// const response = await api.get("/2fa"); -// const enabled = response.data; -// console.log(`enable= ${enabled.data}`) -// } -// // const enabled = 0; -// }, []) - - - - // useEffect(() => { - // async function get2fa() - // { - // api.get('/api/QRcode', { responseType: 'blob' }) - // .then(response => { - // const reader = new FileReader(); - // reader.onloadend = () => { - // setImageSrc(reader.result); - // }; - // reader.readAsDataURL(response.data); - // }) - // .catch(error => { - // console.error(error); - // }); - - // } }, []); - -// // const [verificationCode, setVerificationCode] = useState(''); -// // const [invalidCode, setInvalidCode] = useState(false); - -// const handleSubmit = () => { -// // async (e) => { -// // e.preventDefault(); - -// // const result = await verifyOtp(verificationCode); - -// // if (result) return (window.location = '/'); - -// // setInvalidCode(true); -// // }, -// // [verificationCode] -// }; - -// let sourceCode - -// if (!enabled) -// { -// api.get('/QRcode') -// .then(response => { -// sourceCode = response.data; -// console.log(sourceCode); -// }) -// .catch(error => { -// console.error(error); -// }); -// } - -// return ( -//
-// {!enabled && ( -//
-//

Scan the QR code on your authenticator app

-// -//
-// )} - -//
-// {/* setVerificationCode(e.target.value)} -// /> */} - -// - -// {/* {invalidCode &&

Invalid verification code

} */} -//
-//
-// ); -// } - - -// import { toFileStream } from 'qrcode'; const DoubleAuth = () => { - const [imageSrc, setImageSrc] = useState(''); - + // const [imageSrc, setImageSrc] = useState(''); + const [imageSrc, setImageSrc] = useState(''); + useEffect(() => { async function getCode(){ await api.get('/QRcode', { responseType: 'blob' }) .then(response => { const reader = new FileReader(); + if (!reader) + return ; reader.onloadend = () => { setImageSrc(reader.result); }; @@ -119,23 +25,12 @@ const DoubleAuth = () => { getCode(); }, []); - // return ( - //
- // {imageSrc && QR Code} - //
- // ); - - // - return (

Scan the QR code on your authenticator app

- {imageSrc && QR Code} + {imageSrc && QR Code}
- {/*
- -
*/}
); diff --git a/containers/react/src/pages/QrCode.tsx b/containers/react/src/pages/QrCode.tsx index f1d9811a..4432810e 100644 --- a/containers/react/src/pages/QrCode.tsx +++ b/containers/react/src/pages/QrCode.tsx @@ -52,9 +52,6 @@ function QrCode () { const otpData = await api.post("/otp"); setUrl(otpData.data.otpauth_url); setSecret(otpData.data.base32_secret); - // const tmpUser = await api.get("/profile") - // console.log("test") - // console.table(convs); } catch(err){ console.log(err); @@ -70,34 +67,22 @@ function QrCode () { const handleKeyPress = async (e: { key: string; })=>{ - // console.log(`e in press= ${e.key}`) if (e.key !== "Enter") return ; try{ - console.log("code= ", code) const res = await api.post("/verifyOtp", {token: code}) - console.log("res= ", res.data) - console.log("res= ", res) if (!res.data) { setErr(true); } if (res.data === 1) { - console.log("registered") - // history.push('/login') - const path = 'http://' + process.env.REACT_APP_BASE_URL + '/'; window.history.pushState({}, '', path); window.location.reload(); - } else - { console.log("bad code") - //alert ?? retry - } - // redirect('/test') } catch(err){ console.log(err) @@ -107,8 +92,6 @@ function QrCode () { const handleDesactivate = async () => { try { await api.post("/deleteOtp") - // const path = 'http://' + process.env.REACT_APP_BASE_URL + '/'; - // window.history.pushState({}, '', path); window.location.reload(); } catch(err) { console.log(err); @@ -116,18 +99,6 @@ function QrCode () { }; return ( - // - //

QRcode

- //

{secret}

- //
- // - // {} - - // - {secret}

Or Scan The QRCode

- {/*
{ref}
*/} )} @@ -166,22 +136,6 @@ function QrCode () { {err ? ():("")} - - {/* {!localStorage.getItem('token') && ( - <> -

Double Auth

- setCode(e.target.value)} - /> - - ) : ()} - */} - {/* {!activated && ( - - )} */} ) } diff --git a/containers/react/src/pages/Social.tsx b/containers/react/src/pages/Social.tsx deleted file mode 100644 index df980ece..00000000 --- a/containers/react/src/pages/Social.tsx +++ /dev/null @@ -1,9 +0,0 @@ -import React from "react"; - -function Social (){ - return ( -
je suis la partie social
- ) -} - -export default Social \ No newline at end of file diff --git a/containers/react/src/pages/canvas.tsx b/containers/react/src/pages/canvas.tsx index 02c03015..e53f1f92 100644 --- a/containers/react/src/pages/canvas.tsx +++ b/containers/react/src/pages/canvas.tsx @@ -1,3 +1,4 @@ +import { useEffect } from 'react'; import api from '../script/axiosApi.tsx'; import io from 'socket.io-client'; @@ -9,6 +10,22 @@ interface GameProps { function DrawCanvas(option: number, gameParam: GameProps) { + + useEffect(() => { + const handleBeforeUnload = async (event: { preventDefault: () => void; returnValue: string; }) => { + try { + await api.post("/status", {status: 1}); + } catch (err) { + console.log(err); + } + }; + + window.addEventListener('beforeunload', handleBeforeUnload); + return () => { + window.removeEventListener('beforeunload', handleBeforeUnload); + }; + }, []); + console.log(`option= ${option}`); const superpowerModifier = option & 1; // Retrieves the superpower modifier const obstacleModifier = (option >> 1) & 1; // Retrieves the obstacle modifier @@ -38,7 +55,7 @@ function DrawCanvas(option: number, gameParam: GameProps) { } console.log("start function"); - const canvas = document.getElementById('myCanvas') as HTMLCanvasElement | null;; + const canvas = document.getElementById('myCanvas') as HTMLCanvasElement | null; if (!canvas) return ; @@ -433,6 +450,20 @@ socket.on('pong:hisPoint', (data) => { console.log(err) } } + else + { + const data = { + myScore: myScore, + opScore: 5, + opName: opName, + opRank: opRank, + }; + await api.post('/loss', data); + // await api.post('/status', {status: 1}); + } + + + //here socket.emit('pong:disconnect', {id: myId}); window.location.replace("http://" + process.env.REACT_APP_BASE_URL + "/pong"); };