-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* packages update * JWT token authentication prepared. - Defined access and refresh tokens - Prepared interceptor for obtaining new access token or redirecting to the login page when refresh token is expired * urls.ts is meant to contain all the urs that the app will have. Each page has defined custom variable URL * IPFS and IPFSList components redefined * BaseLayout modified * user context state logic moved into state/user.ts with some minor modifications * Login component updated, it uses some basic logic to login. Available email: [email protected], password: geslo123 * urls applied in NavigationBar * ProtectedRoute replaced with Page component, which ensures that all his defined routes are protected by checking if user is logged in and if we have access token defined * Error404 page initialized * Authentication and api setup is now in api/api.ts * App updated with different user store, urls applied, and some routes moved into Page component * .eslintignore added <- I can not handle lint every save there is made (cpu -> 700%). Add * to disable lint. * IPFS pql/test action fixed * Adding keys for each ipfs in list. Hashes are used for keys * Baselayout margin changed to padding * Redirect to error page where the un-know ipfs hash is opened * IPFS removed from this issue Co-authored-by: Žiga Franko <[email protected]>
- Loading branch information
Showing
22 changed files
with
236 additions
and
208 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import axios from 'axios'; | ||
|
||
const OBTAIN_TOKEN = '/token/obtain/'; | ||
// const REFRESH_TOKEN_URL = '/token/refresh/'; | ||
|
||
export const ACCESS_TOKEN = 'access_token'; | ||
export const REFRESH_TOKEN = 'refresh_token'; | ||
|
||
interface JWTTokenResponse { | ||
refresh: string; | ||
access: string; | ||
} | ||
|
||
export const axiosInstance = axios.create({ | ||
baseURL: 'http://127.0.0.1:7424/api/', | ||
timeout: 5000, | ||
headers: { | ||
// 'Authorization': localStorage.getItem('access_token') ? 'JWT ' + localStorage.getItem('access_token') : null, | ||
'Content-Type': 'application/json', | ||
accept: 'application/json', | ||
}, | ||
}); | ||
|
||
export const saveTokens = (jwtToken: JWTTokenResponse): JWTTokenResponse => { | ||
localStorage.setItem(ACCESS_TOKEN, jwtToken.access); | ||
localStorage.setItem(REFRESH_TOKEN, jwtToken.refresh); | ||
return jwtToken; | ||
}; | ||
|
||
const localGet = (key: string, defaultValue = ''): string => { | ||
const value = localStorage.getItem(key); | ||
if (value === null) return defaultValue; | ||
return value; | ||
}; | ||
|
||
export const getAccessToken = (): string => localGet(ACCESS_TOKEN); | ||
export const getRefreshToken = (): string => localGet(REFRESH_TOKEN); | ||
|
||
axiosInstance.interceptors.response.use( | ||
(res) => res, | ||
(err) => { | ||
// const originalRequest = err.config; | ||
// If refresh tokens is expired redirect to login page | ||
// if (err.response.status === 401 && originalRequest.url === REFRESH_TOKEN_URL) { | ||
// window.location.href = '/login/'; | ||
// return Promise.reject(err); | ||
// } | ||
|
||
// // If access token is expired update it | ||
// if (err.response.status === 401 && err.response.statusText === 'Unauthorized') { | ||
// return axiosInstance | ||
// .post<JWTTokenResponse>(REFRESH_TOKEN_URL, {refresh: localStorage.getItem(REFRESH_TOKEN)}) | ||
// .then(res => res.data) | ||
// .then(saveTokens) | ||
// .then(res => { | ||
// axiosInstance.defaults.headers['Authorization'] = 'JWT ' + res.access; | ||
// originalRequest.headers['Authorization'] = 'JWT ' + res.access; | ||
|
||
// return axiosInstance(originalRequest); | ||
// }) | ||
// } | ||
|
||
return Promise.reject(err); | ||
}, | ||
); | ||
|
||
export const obtainTokenApi = async (email: string, password: string): Promise<JWTTokenResponse> => | ||
axiosInstance | ||
.post<JWTTokenResponse>(OBTAIN_TOKEN, { email, password }) | ||
.then((res) => res.data); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// import { axiosInstance, obtainTokenApi, saveTokens } from "../api" | ||
|
||
// const LOGIN_URL = ''; | ||
|
||
export interface UserAuthenticationData { | ||
email: string; | ||
password: string; | ||
} | ||
|
||
const loginUser = (data: UserAuthenticationData): Promise<void> => { | ||
const { email, password } = data; | ||
|
||
if (email !== '[email protected]') throw new Error('Email does not exist!'); | ||
if (password !== 'geslo123') throw new Error('Password is incorrect!'); | ||
// axiosInstance.post<{}>(LOGIN_URL, data) | ||
// .then((res) => res.data); | ||
return Promise.resolve(); | ||
}; | ||
|
||
export const authenticateUser = async (data: UserAuthenticationData): Promise<void> => { | ||
await loginUser(data); | ||
// const jwtTokens = await obtainTokenApi(data.email, data.password); | ||
// saveTokens(jwtTokens); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import React, { useContext } from 'react'; | ||
import { Redirect, Route, Switch } from 'react-router-dom'; | ||
import Ipfs from '../pages/ipfs/Ipfs'; | ||
import IpfsList from '../pages/ipfs/IpfsList'; | ||
import UserContext from '../state/user'; | ||
import { IPFS_BASE_PAGE, IPFS_PAGE, LOGIN_PAGE } from './urls'; | ||
|
||
const Page = (): JSX.Element => { | ||
const { user } = useContext(UserContext); | ||
|
||
if (!user.isLoggedIn) return <Redirect to={LOGIN_PAGE} />; | ||
|
||
return ( | ||
<Switch> | ||
<Route exact path={IPFS_BASE_PAGE} component={IpfsList} /> | ||
<Route path={IPFS_PAGE} component={Ipfs} /> | ||
</Switch> | ||
); | ||
}; | ||
|
||
export default Page; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.