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

Commit 446579e

Browse files
authored
Merge pull request #11 from FileFighter/feature/addUser
Feature/add user
2 parents e2d4e70 + 7340ce1 commit 446579e

File tree

16 files changed

+298
-35
lines changed

16 files changed

+298
-35
lines changed
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://www.apache.org/licenses/LICENSE-2.0.html

webapp_frontend/src/background/api/auth.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import Axios from "axios";
33

44
import {hostname, userPath} from "./api";
55

6-
import {AddUser, UserState} from "../redux/actions/userTypes";
6+
import {UserState} from "../redux/actions/userTypes";
77
import store from "../redux/store";
88
import {addAccessToken, addRefreshToken, checkedCookies, removeTokens} from "../redux/actions/tokens";
99
import {addUser} from "../redux/actions/user";
10-
import {AccessToken, AddAccessToken, AddRefreshToken, CheckedCookies, RemoveTokens, TokensState} from "../redux/actions/tokenTypes";
10+
import {AccessToken, RemoveTokens, TokensState} from "../redux/actions/tokenTypes";
1111
import {deleteCookie, getCookie, setCookie} from "../methods/cookies";
1212

1313

@@ -26,10 +26,10 @@ export interface BackendLoginData {
2626
export const checkForCookie=()=>{
2727
let refreshTokenCookieValue=getCookie(cookieName)
2828
if (refreshTokenCookieValue){
29-
store.dispatch(addRefreshToken(refreshTokenCookieValue) as AddRefreshToken)
29+
store.dispatch(addRefreshToken(refreshTokenCookieValue))
3030
getAccessTokenWithRefreshToken();
3131
}
32-
store.dispatch(checkedCookies(true) as CheckedCookies)
32+
store.dispatch(checkedCookies(true))
3333

3434

3535
}
@@ -48,8 +48,8 @@ console.log("[Auth] loginWithUsernameAndPassword")
4848
return Axios.get(hostname + userPath + '/login', config)
4949
.then((data) => {
5050
console.log(data.data)
51-
store.dispatch(addRefreshToken(data.data.tokenValue) as AddRefreshToken)
52-
store.dispatch(addUser(data.data.user as UserState) as AddUser)
51+
store.dispatch(addRefreshToken(data.data.tokenValue))
52+
store.dispatch(addUser(data.data.user as UserState))
5353

5454
if (stayLoggedIn){
5555
setCookie(cookieName,data.data.tokenValue,60)
@@ -67,7 +67,6 @@ console.log("[Auth] loginWithUsernameAndPassword")
6767
})
6868
}
6969

70-
7170
export const getAccessTokenWithRefreshToken = () => {
7271
console.log("getAccessTokenWithRefreshToken")
7372

@@ -83,7 +82,7 @@ export const getAccessTokenWithRefreshToken = () => {
8382
.then((data) => {
8483
setAuthHeaderToAxios(data.data.tokenValue)
8584

86-
store.dispatch(addAccessToken({token: data.data.tokenValue, timestamp: data.data.validUntil}as AccessToken) as AddAccessToken);
85+
store.dispatch(addAccessToken({token: data.data.tokenValue, timestamp: data.data.validUntil}as AccessToken));
8786

8887
})
8988
.catch(((error) => {
@@ -96,13 +95,11 @@ export const getAccessTokenWithRefreshToken = () => {
9695
}
9796

9897
export const logout=()=>{
99-
store.dispatch(removeTokens()as RemoveTokens);
98+
store.dispatch(removeTokens());
10099
deleteCookie(cookieName);
101100
}
102101

103102
function setAuthHeaderToAxios(accessToken: string) {
104103
Axios.defaults.headers.common['Authorization'] =
105104
`Bearer ${accessToken}`;
106-
}
107-
108-
105+
}
Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,42 @@
1+
import Axios, {AxiosError, AxiosResponse} from "axios";
2+
import {hostname, userPath} from "./api";
13

2-
3-
function test(){
4-
return null
4+
export interface IRegisterServerResponse {
5+
httpStatus: number,
6+
httpMessage: string
7+
outputMessage?: string
58
}
69

7-
export default test;
10+
export const registerNewUser = (username: string, password: string, passwordConfirmation: string): Promise<IRegisterServerResponse> => {
11+
12+
return new Promise((resolve, reject) => {
13+
const newUser = {
14+
username: username,
15+
password: password,
16+
confirmationPassword: passwordConfirmation
17+
}
18+
19+
20+
return Axios.post(hostname + userPath + '/register', newUser)
21+
.then((data: AxiosResponse<object>) => {
22+
console.log(data)
23+
const response: IRegisterServerResponse = {
24+
httpStatus: data.status,
25+
httpMessage: data.statusText
26+
}
27+
if (data.status === 201) {
28+
response.outputMessage = "User was successfully created."
29+
}
30+
resolve(response);
31+
})
32+
.catch((error: AxiosError) => {
33+
console.log(error.response)
34+
const response: IRegisterServerResponse = {
35+
httpStatus: error.response!.status,
36+
httpMessage: error.response!.statusText,
37+
outputMessage: error.response!.data.message
38+
}
39+
reject(response);
40+
})
41+
})
42+
}
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+
}
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
1-
function reverseString(string:string):string {
1+
export function reverseString(string:string):string {
22
if (string === "")
33
return "";
44
else
55
return reverseString(string.substr(1)) + string.charAt(0);
66
}
77

8-
function stringReplaceSubstringOneTimeFromBeginningAndEnd(string:string, substring:string, replaceWith:string):string {
8+
export function stringReplaceSubstringOneTimeFromBeginningAndEnd(string:string, substring:string, replaceWith:string):string {
99
string = string.replace(substring, replaceWith);
1010
string = reverseString(string);
1111
string = string.replace(substring, replaceWith);
1212
string = reverseString(string);
1313
return string;
1414
}
1515

16-
export {reverseString, stringReplaceSubstringOneTimeFromBeginningAndEnd}
16+
export function deleteSpaces(string:string):string {
17+
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;';
26+
}
Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
1-
import {ADD_REFRESH_TOKEN, ADD_ACCESS_TOKEN, AccessToken, CHECKED_COOKIES, REMOVE_TOKENS} from "./tokenTypes";
1+
import {
2+
ADD_REFRESH_TOKEN,
3+
ADD_ACCESS_TOKEN,
4+
AccessToken,
5+
CHECKED_COOKIES,
6+
REMOVE_TOKENS,
7+
AddRefreshToken, AddAccessToken, RemoveTokens, CheckedCookies
8+
} from "./tokenTypes";
29

310

4-
export const addRefreshToken = (content: string) => ({
11+
export const addRefreshToken = (content: string):AddRefreshToken => ({
512
type: ADD_REFRESH_TOKEN,
613
payload: content
714
});
815

9-
export const addAccessToken = (content: AccessToken) => ({
16+
export const addAccessToken = (content: AccessToken):AddAccessToken => ({
1017
type: ADD_ACCESS_TOKEN,
1118
payload: content
1219
});
1320

14-
export const removeTokens = ()=>({
21+
export const removeTokens = ():RemoveTokens=>({
1522
type: REMOVE_TOKENS
1623
})
1724

18-
export const checkedCookies = (content: boolean) => ({
25+
export const checkedCookies = (content: boolean):CheckedCookies => ({
1926
type: CHECKED_COOKIES,
2027
payload: content
2128
});
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import {ADD_USER, UserState} from "./userTypes";
1+
import {ADD_USER, AddUser, UserState} from "./userTypes";
22

3-
export const addUser = (content: UserState) => ({
3+
export const addUser = (content: UserState):AddUser => ({
44
type: ADD_USER,
55
payload: content
66
});

0 commit comments

Comments
 (0)