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

Commit 3d04f97

Browse files
authored
Feature/login logic (#10)
* added user state and cookies check * added basic flow of components * added basic ui for login and hooked up checkbox * can’t believe that I wrote this on paper * FF-161 cleaning up * formatting * major refactoring using the store directly from the background functions * cleaing up after refactor * some layout * fixed ts errors
1 parent 654cea5 commit 3d04f97

File tree

15 files changed

+254
-41
lines changed

15 files changed

+254
-41
lines changed

webapp_frontend/src/background/api/api.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import {constants} from "../constants";
33

44
export const hostname:string =constants.url.API_URL+'/api';
55

6+
export const userPath:string='/v1/users';
7+
68

79
interface BackendHealthData {
810
uptimeInSeconds: number;
@@ -21,4 +23,4 @@ function callBackendHealth():Promise<BackendHealthData>{
2123
});
2224
}
2325

24-
export { callBackendHealth}
26+
export { callBackendHealth}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import Axios from "axios";
2+
3+
4+
import {hostname, userPath} from "./api";
5+
6+
import {AddUser, UserState} from "../redux/actions/userTypes";
7+
import store from "../redux/store";
8+
import {addAccessToken, addRefreshToken} from "../redux/actions/tokens";
9+
import {addUser} from "../redux/actions/user";
10+
import {AccessToken, AddAccessToken, AddRefreshToken, TokensState} from "../redux/actions/tokenTypes";
11+
12+
13+
// reference: https://daveceddia.com/access-redux-store-outside-react/
14+
15+
export interface BackendLoginData {
16+
refreshToken: string,
17+
user: UserState
18+
19+
}
20+
21+
export const loginWithUsernameAndPassword = (userName: string, password: string): Promise<BackendLoginData> => {
22+
23+
return new Promise<BackendLoginData>((resolve, reject) => {
24+
let config = {
25+
headers: {
26+
Authorization: `Basic ${btoa(userName + ':' + password)}`,
27+
},
28+
};
29+
30+
return Axios.get(hostname + userPath + '/login', config)
31+
.then((data) => {
32+
store.dispatch(addRefreshToken(data.data.refreshToken) as AddRefreshToken)
33+
store.dispatch(addUser(data.data.user as UserState) as AddUser)
34+
35+
getAccessTokenWithRefreshToken()
36+
})
37+
.catch(((error) => {
38+
39+
reject(error);
40+
}))
41+
42+
43+
})
44+
}
45+
46+
47+
export const getAccessTokenWithRefreshToken = () => {
48+
console.log("getAccessTokenWithRefreshToken")
49+
50+
let refreshToken: string|null = (store.getState().tokens as TokensState).refreshToken;
51+
52+
let config = {
53+
headers: {
54+
Authorization: `Bearer ${refreshToken}`,
55+
},
56+
};
57+
58+
Axios.get(hostname + userPath + '/auth', config)
59+
.then((data) => {
60+
setAuthHeaderToAxios(data.data.token)
61+
62+
store.dispatch(addAccessToken({token: data.data.token, timestamp: data.data.validUntil}as AccessToken) as AddAccessToken);
63+
64+
})
65+
.catch(((error) => {
66+
console.log(error)
67+
}));
68+
69+
}
70+
71+
function setAuthHeaderToAxios(accessToken: string) {
72+
Axios.defaults.headers.common['Authorization'] =
73+
`Bearer ${accessToken}`;
74+
}
75+
76+

webapp_frontend/src/background/api/login.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

webapp_frontend/src/background/constants.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const prod: constants = {
1111

1212
const dev: constants = {
1313
url: {
14-
API_URL: 'http://filefighter.ddns.net:3001',
14+
API_URL: 'https://cors-anywhere.herokuapp.com/http://filefighter.ddns.net:3001',
1515
}
1616
};
17-
export const constants = process.env.NODE_ENV === 'development' ? dev : prod;
17+
export const constants = process.env.NODE_ENV === 'development' ? dev : prod;
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {TokensState} from "./tokenTypes";
2+
import {UserState} from "./userTypes";
23

34
export interface SystemState {
45
tokens: TokensState
5-
}
6+
user: UserState
7+
}
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
export const ADD_REFRESH_TOKEN = "ADD_REFRESH_TOKEN";
22
export const ADD_ACCESS_TOKEN = "ADD_ACCESS_TOKEN";
3+
export const CHECKED_COOKIES = "CHECKED_COOKIES";
34

45

5-
export interface AccessToken{
6+
export interface AccessToken {
67
token: string | null;
78
timestamp: number | null;
89
}
@@ -11,18 +12,24 @@ export interface AccessToken{
1112
export interface TokensState {
1213
refreshToken: string | null;
1314
accessToken: AccessToken | null;
15+
checkedCookies: boolean;
1416
}
1517

1618

17-
interface AddRefreshToken {
19+
export interface AddRefreshToken {
1820
type: typeof ADD_REFRESH_TOKEN
1921
payload: string
2022
}
2123

22-
interface AddAccessToken {
24+
export interface AddAccessToken {
2325
type: typeof ADD_ACCESS_TOKEN
2426
payload: AccessToken
2527
}
2628

27-
export type TokenActionsTypes = AddRefreshToken | AddAccessToken;
29+
export interface CheckedCookies{
30+
type: typeof CHECKED_COOKIES,
31+
payload: boolean
32+
}
33+
34+
export type TokenActionsTypes = AddRefreshToken | AddAccessToken|CheckedCookies;
2835

webapp_frontend/src/background/redux/actions/tokens.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {ADD_REFRESH_TOKEN, ADD_ACCESS_TOKEN, AccessToken} from "./tokenTypes";
1+
import {ADD_REFRESH_TOKEN, ADD_ACCESS_TOKEN, AccessToken, CHECKED_COOKIES} from "./tokenTypes";
22

33

44
export const addRefreshToken = (content: string) => ({
@@ -9,4 +9,10 @@ export const addRefreshToken = (content: string) => ({
99
export const addAccessToken = (content: AccessToken) => ({
1010
type: ADD_ACCESS_TOKEN,
1111
payload: content
12-
});
12+
});
13+
14+
15+
export const checkedCookies = (content: boolean) => ({
16+
type: CHECKED_COOKIES,
17+
payload: content
18+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import {ADD_USER, UserState} from "./userTypes";
2+
3+
export const addUser = (content: UserState) => ({
4+
type: ADD_USER,
5+
payload: content
6+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export const ADD_USER = "ADD_USER";
2+
3+
export interface UserState {
4+
id: number | null,
5+
username: string | null,
6+
groups: number[],
7+
8+
9+
}
10+
11+
12+
export interface AddUser {
13+
type: typeof ADD_USER,
14+
payload: UserState
15+
}
16+
17+
18+
export type UserActionTypes = AddUser;
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {combineReducers} from "redux";
22
import tokens from "./tokens";
3+
import user from "./user";
34

45
// this combines all the stores from the reducers
5-
export default combineReducers({tokens});
6+
7+
export default combineReducers({tokens, user});

0 commit comments

Comments
 (0)