From c5406006da233d0694019f8e211eb051731005f0 Mon Sep 17 00:00:00 2001 From: NyxiumYuuki Date: Mon, 31 May 2021 23:09:58 +0200 Subject: [PATCH] updated --- .../service-authentication/changePassword.js | 22 +++++++++++++++++++ .../service-authentication/mongodbQueries.js | 14 ++++++++++++ backend/service-authentication/register.js | 8 +++---- backend/service-authentication/server.js | 5 +++++ frontend/src/app/app.component.html | 3 +++ frontend/src/app/app.component.ts | 18 ++++++++++++--- frontend/src/app/app.module.ts | 9 +++++++- frontend/src/app/login/login.component.html | 5 +++-- 8 files changed, 74 insertions(+), 10 deletions(-) create mode 100644 backend/service-authentication/changePassword.js diff --git a/backend/service-authentication/changePassword.js b/backend/service-authentication/changePassword.js new file mode 100644 index 0000000..af69b9b --- /dev/null +++ b/backend/service-authentication/changePassword.js @@ -0,0 +1,22 @@ +const {sendError, sendMessage} = require ("./message"); +const queries = require('./mongodbQueries'); + +async function changePassword (req,res) { + if (typeof req.body.username === 'undefined') + return sendError(res, 'Vous n\'avez pas envoyé le champ username'); + + if (typeof req.body.password === 'undefined') + return sendError(res, 'Vous n\'avez pas envoyé le champ password'); + + if (typeof req.body.newpassword === 'undefined') + return sendError(res, 'Vous n\'avez pas envoyé le champ newpassword'); + + const change = await queries.changePasswordQuery(req.body.username, req.body.password, req.body.newpassword); + if (change){ + return sendMessage(res, change); + } + else{ + return sendError(res, 'cant change'); + } +} +module.exports = changePassword; diff --git a/backend/service-authentication/mongodbQueries.js b/backend/service-authentication/mongodbQueries.js index e9028f2..58cb2ec 100644 --- a/backend/service-authentication/mongodbQueries.js +++ b/backend/service-authentication/mongodbQueries.js @@ -42,3 +42,17 @@ function getUsersQuery(username){ } module.exports.getUsersQuery = getUsersQuery +function changePasswordQuery(login, password, newPassword){ + return new Promise((resolve, reject) => { + mongoDB.collection(config.mongodbUtilisateurs).findOneAndUpdate( + {'login': login, 'password': password}, + {$set: { 'login': login, 'password': newPassword}} + ,function(err,res){ + if(res !== undefined){ + console.log(res); + resolve(res.lastErrorObject.n === 1); + } + }); + }); +} +module.exports.changePasswordQuery = changePasswordQuery; diff --git a/backend/service-authentication/register.js b/backend/service-authentication/register.js index 2dcd42b..84473e1 100644 --- a/backend/service-authentication/register.js +++ b/backend/service-authentication/register.js @@ -2,19 +2,19 @@ const {sendError, sendMessage} = require ("./message"); const queries = require('./mongodbQueries'); async function register(req,res) { - if (typeof req.body.login === 'undefined') + if (typeof req.body.username === 'undefined') return sendError(res, 'Vous n\'avez pas envoyé le champ login'); if (typeof req.body.password === 'undefined') return sendError(res, 'Vous n\'avez pas envoyé le champ password'); - const register = await queries.register(req.body.login, req.body.password); + const register = await queries.register(req.body.username, req.body.password); if (register){ - console.log('Register : '+req.body.login); + console.log('Register : '+req.body.username); return sendMessage(res, null); } else{ - return sendError(res, 'Error registering'); + return sendError(res, 'Username already taken'); } } module.exports = register; diff --git a/backend/service-authentication/server.js b/backend/service-authentication/server.js index cb75bfa..ce19425 100644 --- a/backend/service-authentication/server.js +++ b/backend/service-authentication/server.js @@ -18,6 +18,7 @@ mongoConnect.connectToServer(function( err, client ) { if (err) console.log(err); const checkLogin = require('./checkLogin'); const register = require('./register'); + const changePassword = require('./changePassword'); const getUsers = require('./getUsers'); const queries = require('./mongodbQueries'); const auth = require('./auth'); @@ -46,6 +47,10 @@ mongoConnect.connectToServer(function( err, client ) { getUsers(req,res); }); + app.post('/changePassword', (req, res) => { + changePassword(req,res); + }); + app.listen(port, () => { console.log (`listening on port ${port}`); }); diff --git a/frontend/src/app/app.component.html b/frontend/src/app/app.component.html index 0680b43..d73f057 100644 --- a/frontend/src/app/app.component.html +++ b/frontend/src/app/app.component.html @@ -1 +1,4 @@ + diff --git a/frontend/src/app/app.component.ts b/frontend/src/app/app.component.ts index 0c5a793..0bd1426 100644 --- a/frontend/src/app/app.component.ts +++ b/frontend/src/app/app.component.ts @@ -1,10 +1,22 @@ -import { Component } from '@angular/core'; +import { Component, OnInit } from '@angular/core'; +import { Router } from '@angular/router'; +import {Title} from '@angular/platform-browser'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.scss'] }) -export class AppComponent { - title = 'frontend'; +export class AppComponent implements OnInit{ + + constructor(private titleService: Title, private router: Router) { + } + + ngOnInit(): void { + this.setTitle('Chat Polytech'); + } + + public setTitle(newTitle: string): void { + this.titleService.setTitle(newTitle); + } } diff --git a/frontend/src/app/app.module.ts b/frontend/src/app/app.module.ts index 95c1535..900c015 100644 --- a/frontend/src/app/app.module.ts +++ b/frontend/src/app/app.module.ts @@ -9,6 +9,9 @@ import {CommonModule, DatePipe} from "@angular/common"; import { PrivateComponent } from './private/private.component'; import { ChatComponent } from './chat/chat.component'; import { MessageComponent } from './message/message.component'; +import { RegisterComponent } from './register/register.component'; +import { ChangePasswordComponent } from './change-password/change-password.component'; +import { NavbarComponent } from './navbar/navbar.component'; @NgModule({ @@ -17,10 +20,14 @@ import { MessageComponent } from './message/message.component'; LoginComponent, PrivateComponent, ChatComponent, - MessageComponent + MessageComponent, + RegisterComponent, + ChangePasswordComponent, + NavbarComponent ], imports: [ BrowserModule, + AppRoutingModule, HttpClientModule, FormsModule, CommonModule, diff --git a/frontend/src/app/login/login.component.html b/frontend/src/app/login/login.component.html index aa64ec3..7d7545e 100644 --- a/frontend/src/app/login/login.component.html +++ b/frontend/src/app/login/login.component.html @@ -7,8 +7,8 @@
- - + +
@@ -20,3 +20,4 @@
{{errorMessage}}
+