1
- import Axios from "axios" ;
1
+ import Axios , { AxiosError , AxiosResponse } from "axios" ;
2
2
3
3
4
4
import { hostname , userPath } from "./api" ;
@@ -8,6 +8,8 @@ import store from "../redux/store";
8
8
import { addAccessToken , addRefreshToken } from "../redux/actions/tokens" ;
9
9
import { addUser } from "../redux/actions/user" ;
10
10
import { AccessToken , AddAccessToken , AddRefreshToken , TokensState } from "../redux/actions/tokenTypes" ;
11
+ import { Simulate } from "react-dom/test-utils" ;
12
+ import error = Simulate . error ;
11
13
12
14
13
15
// reference: https://daveceddia.com/access-redux-store-outside-react/
@@ -18,6 +20,10 @@ export interface BackendLoginData {
18
20
19
21
}
20
22
23
+ export interface BackendRegistrationData {
24
+
25
+ }
26
+
21
27
export const loginWithUsernameAndPassword = ( userName : string , password : string ) : Promise < BackendLoginData > => {
22
28
23
29
return new Promise < BackendLoginData > ( ( resolve , reject ) => {
@@ -43,6 +49,36 @@ export const loginWithUsernameAndPassword = (userName: string, password: string)
43
49
} )
44
50
}
45
51
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
+
46
82
47
83
export const getAccessTokenWithRefreshToken = ( ) => {
48
84
console . log ( "getAccessTokenWithRefreshToken" )
0 commit comments