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

Commit a429bc3

Browse files
committed
Added beginning of registration logic
1 parent a588fdb commit a429bc3

File tree

1 file changed

+37
-1
lines changed
  • webapp_frontend/src/background/api

1 file changed

+37
-1
lines changed

webapp_frontend/src/background/api/auth.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Axios from "axios";
1+
import Axios, {AxiosError, AxiosResponse} from "axios";
22

33

44
import {hostname, userPath} from "./api";
@@ -8,6 +8,8 @@ import store from "../redux/store";
88
import {addAccessToken, addRefreshToken} from "../redux/actions/tokens";
99
import {addUser} from "../redux/actions/user";
1010
import {AccessToken, AddAccessToken, AddRefreshToken, TokensState} from "../redux/actions/tokenTypes";
11+
import {Simulate} from "react-dom/test-utils";
12+
import error = Simulate.error;
1113

1214

1315
// reference: https://daveceddia.com/access-redux-store-outside-react/
@@ -18,6 +20,10 @@ export interface BackendLoginData {
1820

1921
}
2022

23+
export interface BackendRegistrationData {
24+
25+
}
26+
2127
export const loginWithUsernameAndPassword = (userName: string, password: string): Promise<BackendLoginData> => {
2228

2329
return new Promise<BackendLoginData>((resolve, reject) => {
@@ -43,6 +49,36 @@ export const loginWithUsernameAndPassword = (userName: string, password: string)
4349
})
4450
}
4551

52+
export const registerNewUser = (username: string, password: string, passwordConfirmation: string): Promise<BackendRegistrationData> => {
53+
54+
let accessToken: AccessToken|null = (store.getState().tokens as TokensState).accessToken;
55+
56+
return new Promise<BackendRegistrationData>((resolve, reject) => {
57+
let config = {
58+
headers: {
59+
Authorization: `Bearer ${accessToken}`,
60+
},
61+
payload: {
62+
username: username,
63+
password: password,
64+
confirmationPassword: passwordConfirmation
65+
}
66+
};
67+
68+
return Axios.get(hostname + username + "/register", config)
69+
.then((data:AxiosResponse<object>) => {
70+
console.log(data.status + ": " + data.statusText);
71+
alert(data.status + ": " + data.statusText);
72+
})
73+
.catch((error:AxiosError) => {
74+
//if(error.response!.status === 409){
75+
console.log("Error " + error.response!.status + ": " + error.response!.statusText);
76+
//}
77+
reject(error);
78+
})
79+
})
80+
}
81+
4682

4783
export const getAccessTokenWithRefreshToken = () => {
4884
console.log("getAccessTokenWithRefreshToken")

0 commit comments

Comments
 (0)