|
1 |
| -import Axios, { AxiosResponse } from "axios"; |
| 1 | +import Axios, { AxiosResponse } from "axios" |
2 | 2 |
|
3 |
| -import { hostname, userPath } from "./api"; |
| 3 | +import { hostname, userPath } from "./api" |
4 | 4 |
|
5 |
| -import { UserState } from "../redux/actions/userTypes"; |
6 |
| -import store from "../redux/store"; |
| 5 | +import { UserState } from "../redux/actions/userTypes" |
| 6 | +import store from "../redux/store" |
7 | 7 | import {
|
8 | 8 | addAccessToken,
|
9 | 9 | addRefreshToken,
|
10 | 10 | checkedCookies,
|
11 |
| - removeTokens |
12 |
| -} from "../redux/actions/tokens"; |
13 |
| -import { AccessToken, CookieStatus } from "../redux/actions/tokenTypes"; |
14 |
| -import { deleteCookie, getCookie, setCookie } from "../methods/cookies"; |
15 |
| -import { updateUser } from "../redux/actions/user"; |
16 |
| -import { hashPassword } from "../methods/passwords"; |
| 11 | + removeTokens, |
| 12 | +} from "../redux/actions/tokens" |
| 13 | +import { AccessToken, CookieStatus } from "../redux/actions/tokenTypes" |
| 14 | +import { deleteCookie, getCookie, setCookie } from "../methods/cookies" |
| 15 | +import { updateUser } from "../redux/actions/user" |
| 16 | +import { hashPassword } from "../methods/passwords" |
17 | 17 |
|
18 | 18 | // reference: https://daveceddia.com/access-redux-store-outside-react/
|
19 | 19 |
|
20 |
| -const cookieName: string = "refreshToken"; |
| 20 | +const cookieName: string = "refreshToken" |
21 | 21 |
|
22 | 22 | export interface BackendLoginData {
|
23 |
| - tokenValue: string; |
24 |
| - user: UserState; |
| 23 | + tokenValue: string |
| 24 | + user: UserState |
25 | 25 | }
|
26 | 26 |
|
27 | 27 | export interface BackendAuthData {
|
28 |
| - tokenValue: string; |
29 |
| - userId: number; |
30 |
| - validUntil: number; |
| 28 | + tokenValue: string |
| 29 | + userId: number |
| 30 | + validUntil: number |
31 | 31 | }
|
32 | 32 |
|
33 | 33 | export const checkForCookie = () => {
|
34 |
| - let refreshTokenCookieValue = getCookie(cookieName); |
| 34 | + let refreshTokenCookieValue = getCookie(cookieName) |
35 | 35 | if (!refreshTokenCookieValue) {
|
36 |
| - return store.dispatch(checkedCookies(CookieStatus.FINISHED)); |
| 36 | + return store.dispatch(checkedCookies(CookieStatus.FINISHED)) |
37 | 37 | }
|
38 | 38 |
|
39 |
| - store.dispatch(addRefreshToken(refreshTokenCookieValue)); |
40 |
| - store.dispatch(checkedCookies(CookieStatus.LOADING)); |
41 |
| - getAccessTokenWithRefreshToken(); |
42 |
| -}; |
| 39 | + store.dispatch(addRefreshToken(refreshTokenCookieValue)) |
| 40 | + store.dispatch(checkedCookies(CookieStatus.LOADING)) |
| 41 | + getAccessTokenWithRefreshToken() |
| 42 | +} |
43 | 43 |
|
44 | 44 | export const loginWithUsernameAndPassword = async (
|
45 | 45 | userName: string,
|
46 | 46 | password: string,
|
47 | 47 | stayLoggedIn: boolean
|
48 | 48 | ): Promise<BackendLoginData> => {
|
49 |
| - console.log("[Auth] loginWithUsernameAndPassword", userName); |
50 |
| - let hashed = await hashPassword(password); |
| 49 | + console.log("[Auth] loginWithUsernameAndPassword", userName) |
| 50 | + let hashed = await hashPassword(password) |
51 | 51 | return new Promise<BackendLoginData>((resolve, reject) => {
|
52 | 52 | let config = {
|
53 | 53 | headers: {
|
54 |
| - Authorization: `Basic ${btoa(userName + ":" + hashed)}` |
55 |
| - } |
56 |
| - }; |
| 54 | + Authorization: `Basic ${btoa(userName + ":" + hashed)}`, |
| 55 | + }, |
| 56 | + } |
57 | 57 |
|
58 | 58 | return Axios.get<BackendLoginData>(
|
59 | 59 | hostname + userPath + "/login",
|
60 | 60 | config
|
61 | 61 | )
|
62 | 62 | .then((data: AxiosResponse<BackendLoginData>) => {
|
63 |
| - console.log(data.data); |
64 |
| - store.dispatch(addRefreshToken(data.data.tokenValue)); |
65 |
| - store.dispatch(updateUser(data.data.user as UserState)); |
| 63 | + console.log(data.data) |
| 64 | + store.dispatch(addRefreshToken(data.data.tokenValue)) |
| 65 | + store.dispatch(updateUser(data.data.user as UserState)) |
66 | 66 |
|
67 | 67 | if (stayLoggedIn) {
|
68 |
| - setCookie(cookieName, data.data.tokenValue, 60); |
| 68 | + setCookie(cookieName, data.data.tokenValue, 60) |
69 | 69 | }
|
70 | 70 |
|
71 |
| - getAccessTokenWithRefreshToken(); |
| 71 | + getAccessTokenWithRefreshToken() |
72 | 72 | })
|
73 | 73 | .catch((error) => {
|
74 |
| - reject(error); |
75 |
| - }); |
76 |
| - }); |
77 |
| -}; |
| 74 | + reject(error) |
| 75 | + }) |
| 76 | + }) |
| 77 | +} |
78 | 78 |
|
79 | 79 | export const getAccessTokenWithRefreshToken = () => {
|
80 |
| - console.log("getAccessTokenWithRefreshToken"); |
| 80 | + console.log("getAccessTokenWithRefreshToken") |
81 | 81 |
|
82 |
| - let refreshToken: string | null = store.getState().tokens.refreshToken; |
| 82 | + let refreshToken: string | null = store.getState().tokens.refreshToken |
83 | 83 |
|
84 | 84 | let config = {
|
85 | 85 | headers: {
|
86 |
| - Authorization: `Bearer ${refreshToken}` |
87 |
| - } |
88 |
| - }; |
| 86 | + Authorization: `Bearer ${refreshToken}`, |
| 87 | + }, |
| 88 | + } |
89 | 89 |
|
90 | 90 | Axios.get<BackendAuthData>(hostname + userPath + "/auth", config)
|
91 | 91 | .then((data: AxiosResponse<BackendAuthData>) => {
|
92 |
| - store.dispatch(checkedCookies(CookieStatus.FINISHED)); |
93 |
| - setAuthHeaderToAxios(data.data.tokenValue); |
| 92 | + store.dispatch(checkedCookies(CookieStatus.FINISHED)) |
| 93 | + setAuthHeaderToAxios(data.data.tokenValue) |
94 | 94 |
|
95 | 95 | store.dispatch(
|
96 | 96 | addAccessToken({
|
97 | 97 | token: data.data.tokenValue,
|
98 |
| - timestamp: data.data.validUntil |
| 98 | + timestamp: data.data.validUntil, |
99 | 99 | } as AccessToken)
|
100 |
| - ); |
| 100 | + ) |
101 | 101 | if (!store.getState().user.username) {
|
102 |
| - getOwnUserData(data.data.userId); |
| 102 | + getOwnUserData(data.data.userId) |
103 | 103 | }
|
104 | 104 | })
|
105 | 105 | .catch((error) => {
|
106 |
| - store.dispatch(removeTokens()); |
107 |
| - store.dispatch(checkedCookies(CookieStatus.FINISHED)); |
| 106 | + store.dispatch(removeTokens()) |
| 107 | + store.dispatch(checkedCookies(CookieStatus.FINISHED)) |
108 | 108 |
|
109 |
| - console.log(error); |
| 109 | + console.log(error) |
110 | 110 | //you probably want to notify the user, maybe with a toast or similar
|
111 |
| - }); |
112 |
| -}; |
| 111 | + }) |
| 112 | +} |
113 | 113 |
|
114 | 114 | const getOwnUserData = (userId: number) => {
|
115 | 115 | Axios.get<UserState>(`${hostname}${userPath}/${userId}/info`)
|
116 | 116 | .then((response: AxiosResponse<UserState>) => {
|
117 |
| - store.dispatch(updateUser(response.data)); |
| 117 | + store.dispatch(updateUser(response.data)) |
118 | 118 | })
|
119 | 119 | .catch((error) => {
|
120 |
| - console.log(error); |
121 |
| - }); |
122 |
| -}; |
| 120 | + console.log(error) |
| 121 | + }) |
| 122 | +} |
123 | 123 |
|
124 | 124 | export const logout = () => {
|
125 |
| - store.dispatch(removeTokens()); |
126 |
| - deleteCookie(cookieName); |
127 |
| -}; |
| 125 | + store.dispatch(removeTokens()) |
| 126 | + deleteCookie(cookieName) |
| 127 | +} |
128 | 128 |
|
129 | 129 | function setAuthHeaderToAxios(accessToken: string) {
|
130 |
| - Axios.defaults.headers.common["Authorization"] = `Bearer ${accessToken}`; |
| 130 | + Axios.defaults.headers.common["Authorization"] = `Bearer ${accessToken}` |
131 | 131 | }
|
0 commit comments