Skip to content
This repository was archived by the owner on Apr 5, 2024. It is now read-only.

Commit 29fbf26

Browse files
committed
Added max Password
1 parent ddb4cad commit 29fbf26

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
lines changed

webapp_frontend/src/background/api/registration.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export const registerNewUser = (username: string, password: string, passwordConf
1818

1919
return Axios.post(hostname + userPath + '/register', newUser)
2020
.then((data: AxiosResponse<object>) => {
21+
console.log(data)
2122
const response: IRegisterServerResponse = {
2223
httpStatus: data.status,
2324
httpMessage: data.statusText
@@ -28,12 +29,11 @@ export const registerNewUser = (username: string, password: string, passwordConf
2829
resolve(response);
2930
})
3031
.catch((error: AxiosError) => {
32+
console.log(error.response)
3133
const response: IRegisterServerResponse = {
3234
httpStatus: error.response!.status,
33-
httpMessage: error.response!.statusText
34-
}
35-
if (error.response!.status === 409) {
36-
response.outputMessage = "Username is already taken."
35+
httpMessage: error.response!.statusText,
36+
outputMessage: error.response!.data.message
3737
}
3838
reject(response);
3939
})
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import {toast} from "react-toastify";
22

3-
function notMinStrLength(text:string, minAnz:number):boolean {
3+
export function notMinStrLength(text:string, minAnz:number):boolean {
44
return text.length < minAnz;
55
}
66

7-
function notMinStrLengthAlert(minAnz:number|string, where:number|string):void {
7+
export function notMinStrLengthAlert(minAnz:number|string, where:number|string):void {
88
toast.error(
99
"Mindestzeichenanzahl bei " +
1010
where +
@@ -13,8 +13,10 @@ function notMinStrLengthAlert(minAnz:number|string, where:number|string):void {
1313
);
1414
}
1515

16-
function passwordsDoNotMatchAlert():void {
16+
export function passwordsDoNotMatchAlert():void {
1717
toast.error("Passwörter stimmen nicht überein");
1818
}
1919

20-
export {notMinStrLength, notMinStrLengthAlert, passwordsDoNotMatchAlert};
20+
export function biggerMaxStrLength(text:string, maxAnz: number):boolean {
21+
return text.length > maxAnz;
22+
}

webapp_frontend/src/background/methods/strings.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,12 @@ export function stringReplaceSubstringOneTimeFromBeginningAndEnd(string:string,
1515

1616
export function deleteSpaces(string:string):string {
1717
return string.replace(/\s/,"")
18+
}
19+
20+
export function trimString(string:string, maxLength: number):string {
21+
return string.length > maxLength ? string.substr(0, maxLength-1) : string;
22+
}
23+
24+
export function trimStringWithDotsAtEnd(string:string, maxLength: number):string {
25+
return trimString(string, maxLength) + '&hellip;';
1826
}

webapp_frontend/src/components/pages/Registration.tsx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import React, {ChangeEvent, FormEvent, ReactElement, useEffect, useState} from "react";
22
import {Container, Row, Col, Form, FormGroup, Button, Alert} from "react-bootstrap";
33
import {deleteSpaces} from "../../background/methods/strings";
4-
import {notMinStrLength} from "../../background/methods/checkInput";
4+
import {biggerMaxStrLength, notMinStrLength} from "../../background/methods/checkInput";
55
import info_svg from "../../assets/images/icons/material.io/info-24px.svg";
66
import check_svg from "../../assets/images/icons/material.io/check_circle-24px.svg";
77
import error_svg from "../../assets/images/icons/material.io/error-24px.svg";
88
import {registerNewUser} from "../../background/api/registration";
99

1010
export default function Registration(): ReactElement {
1111
const MIN_PASSWORD_LENGTH = 8;
12+
const MAX_PASSWORD_LENGTH = 20;
1213

1314
const [username, setUsername] = useState<string>("");
1415
const [password, setPassword] = useState<string>("");
@@ -68,10 +69,20 @@ export default function Registration(): ReactElement {
6869
}
6970
}
7071

72+
const makePasswordInputFitRules = (input:string):string|null => {
73+
input = deleteSpaces(input);
74+
if (biggerMaxStrLength(input, MAX_PASSWORD_LENGTH)){
75+
return null
76+
}
77+
return input;
78+
}
79+
7180
const handlePasswordChange = (event: ChangeEvent<HTMLInputElement>) => {
7281
event.preventDefault();
73-
let value = event.target.value;
74-
value = deleteSpaces(value);
82+
let value:string|null = makePasswordInputFitRules(event.target.value);
83+
if (!value){
84+
value = password;
85+
}
7586
setPasswordInformationLength(!notMinStrLength(value, MIN_PASSWORD_LENGTH));
7687
setPasswordInformationLowercase(value.match(/[a-z]/) !== null);
7788
setPasswordInformationUppercase(value.match(/[A-Z]/) !== null);
@@ -81,8 +92,10 @@ export default function Registration(): ReactElement {
8192

8293
const handlePasswordConfirmationChange = async (event: ChangeEvent<HTMLInputElement>) => {
8394
event.preventDefault();
84-
let value = event.target.value;
85-
value = deleteSpaces(value);
95+
let value:string|null = makePasswordInputFitRules(event.target.value);
96+
if (!value){
97+
value = passwordConfirmation;
98+
}
8699
setPasswordConfirmation(value);
87100
}
88101

0 commit comments

Comments
 (0)