mirror of
https://github.com/mgerb/classic-wow-forums
synced 2026-01-11 17:42:48 +00:00
client - get browser fingerprint and set header
This commit is contained in:
3
client/@types/all/index.d.ts
vendored
3
client/@types/all/index.d.ts
vendored
@@ -1,4 +1,5 @@
|
|||||||
// for libraries that don't have types
|
// for libraries that don't have types
|
||||||
declare module 'this-is-a-test-module';
|
// declare module 'this-is-a-test-module';
|
||||||
|
declare module 'fingerprintjs2';
|
||||||
declare module '*.gif';
|
declare module '*.gif';
|
||||||
declare module '*.jpg';
|
declare module '*.jpg';
|
||||||
|
|||||||
@@ -1,9 +1,14 @@
|
|||||||
import axios, { AxiosInstance, AxiosResponse } from 'axios';
|
import axios, { AxiosInstance, AxiosResponse } from 'axios';
|
||||||
|
import fingerprintjs2 from 'fingerprintjs2';
|
||||||
import userStore from '../stores/user-store';
|
import userStore from '../stores/user-store';
|
||||||
|
|
||||||
// create our own instance of axios so we can set request headers
|
// create our own instance of axios so we can set request headers
|
||||||
const ax: AxiosInstance = axios.create();
|
const ax: AxiosInstance = axios.create();
|
||||||
|
export default ax;
|
||||||
|
|
||||||
|
// setup our axios instance - must be done before app bootstraps
|
||||||
|
export const initializeAxios = (): Promise<void> => {
|
||||||
|
return new Promise((resolve: any) => {
|
||||||
// response interceptors
|
// response interceptors
|
||||||
ax.interceptors.response.use(
|
ax.interceptors.response.use(
|
||||||
(config: AxiosResponse) => {
|
(config: AxiosResponse) => {
|
||||||
@@ -19,6 +24,21 @@ ax.interceptors.response.use(
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const user = localStorage.getItem('user');
|
||||||
|
|
||||||
|
if (user) {
|
||||||
|
const jwt = JSON.parse(user!).token;
|
||||||
|
setAuthorizationHeader(jwt);
|
||||||
|
}
|
||||||
|
|
||||||
|
new fingerprintjs2().get((result: string) => {
|
||||||
|
axios.defaults.headers.common['fp'] = result;
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
export function setAuthorizationHeader(jwt: string): void {
|
export function setAuthorizationHeader(jwt: string): void {
|
||||||
ax.defaults.headers.common['Authorization'] = jwt;
|
ax.defaults.headers.common['Authorization'] = jwt;
|
||||||
}
|
}
|
||||||
@@ -27,4 +47,3 @@ export function resetAuthorizationHeader(): void {
|
|||||||
ax.defaults.headers.common['Authorization'] = '';
|
ax.defaults.headers.common['Authorization'] = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
export default ax;
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { BrowserRouter, Route, Switch } from 'react-router-dom';
|
import { BrowserRouter, Route, Switch } from 'react-router-dom';
|
||||||
import { Provider } from 'mobx-react';
|
import { Provider } from 'mobx-react';
|
||||||
|
import { initializeAxios } from './axios/axios';
|
||||||
import { Footer, Header } from './components';
|
import { Footer, Header } from './components';
|
||||||
import { Forum, Home, NotFound, Oauth, Realms, Thread, UserAccount } from './pages';
|
import { Forum, Home, NotFound, Oauth, Realms, Thread, UserAccount } from './pages';
|
||||||
import { stores } from './stores/stores';
|
import { stores } from './stores/stores';
|
||||||
@@ -10,11 +11,31 @@ import './scss/index.scss';
|
|||||||
|
|
||||||
interface Props {}
|
interface Props {}
|
||||||
|
|
||||||
interface State {}
|
interface State {
|
||||||
|
ready: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
export class Routes extends React.Component<Props, State> {
|
export class Routes extends React.Component<Props, State> {
|
||||||
|
|
||||||
|
constructor(props: Props) {
|
||||||
|
super(props);
|
||||||
|
this.state = {
|
||||||
|
ready: false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async componentDidMount() {
|
||||||
|
await initializeAxios();
|
||||||
|
this.setState({ ready: true });
|
||||||
|
}
|
||||||
|
|
||||||
public render() {
|
public render() {
|
||||||
|
|
||||||
|
// make sure we initialize axios with request headers before we load the app
|
||||||
|
if (!this.state.ready) {
|
||||||
|
return <div></div>;
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Provider {...stores}>
|
<Provider {...stores}>
|
||||||
<BrowserRouter>
|
<BrowserRouter>
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
|
import { orderBy } from 'lodash';
|
||||||
import axios from '../axios/axios';
|
import axios from '../axios/axios';
|
||||||
import { ThreadModel } from '../model';
|
import { ThreadModel } from '../model';
|
||||||
|
|
||||||
const getCategoryThreads = async (category_id: string): Promise<ThreadModel[]> => {
|
const getCategoryThreads = async (category_id: string): Promise<ThreadModel[]> => {
|
||||||
try {
|
try {
|
||||||
const res = await axios.get(`/api/thread?category_id=${category_id}`);
|
const res = await axios.get(`/api/thread?category_id=${category_id}`);
|
||||||
return res.data.data;
|
return orderBy(res.data.data as ThreadModel[], (thread: ThreadModel) => {
|
||||||
|
return -(new Date(thread.updated_at));
|
||||||
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { action, observable } from 'mobx';
|
import { action, observable } from 'mobx';
|
||||||
import { UserModel } from '../model';
|
import { UserModel } from '../model';
|
||||||
import { resetAuthorizationHeader, setAuthorizationHeader } from '../axios/axios';
|
import { resetAuthorizationHeader } from '../axios/axios';
|
||||||
|
|
||||||
export class UserStore {
|
export class UserStore {
|
||||||
|
|
||||||
@@ -22,7 +22,6 @@ export class UserStore {
|
|||||||
const u = localStorage.getItem('user');
|
const u = localStorage.getItem('user');
|
||||||
if (u) {
|
if (u) {
|
||||||
this.user = JSON.parse(u);
|
this.user = JSON.parse(u);
|
||||||
setAuthorizationHeader(this.user!.token);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
"author": "Mitchell Gerber",
|
"author": "Mitchell Gerber",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@types/fingerprintjs2": "^1.5.1",
|
||||||
"@types/lodash": "^4.14.92",
|
"@types/lodash": "^4.14.92",
|
||||||
"@types/node": "^9.3.0",
|
"@types/node": "^9.3.0",
|
||||||
"@types/query-string": "^5.0.1",
|
"@types/query-string": "^5.0.1",
|
||||||
@@ -32,6 +33,7 @@
|
|||||||
"extract-text-webpack-plugin": "3.0.2",
|
"extract-text-webpack-plugin": "3.0.2",
|
||||||
"favicons-webpack-plugin": "^0.0.7",
|
"favicons-webpack-plugin": "^0.0.7",
|
||||||
"file-loader": "^1.1.6",
|
"file-loader": "^1.1.6",
|
||||||
|
"fingerprintjs2": "^1.5.1",
|
||||||
"font-awesome": "^4.7.0",
|
"font-awesome": "^4.7.0",
|
||||||
"html-webpack-plugin": "^2.30.1",
|
"html-webpack-plugin": "^2.30.1",
|
||||||
"lodash": "^4.17.4",
|
"lodash": "^4.17.4",
|
||||||
|
|||||||
@@ -2,6 +2,10 @@
|
|||||||
# yarn lockfile v1
|
# yarn lockfile v1
|
||||||
|
|
||||||
|
|
||||||
|
"@types/fingerprintjs2@^1.5.1":
|
||||||
|
version "1.5.1"
|
||||||
|
resolved "https://registry.yarnpkg.com/@types/fingerprintjs2/-/fingerprintjs2-1.5.1.tgz#bd4a3f59692824f1d781307d35414d8591ac61e0"
|
||||||
|
|
||||||
"@types/history@*":
|
"@types/history@*":
|
||||||
version "4.6.2"
|
version "4.6.2"
|
||||||
resolved "https://registry.yarnpkg.com/@types/history/-/history-4.6.2.tgz#12cfaba693ba20f114ed5765467ff25fdf67ddb0"
|
resolved "https://registry.yarnpkg.com/@types/history/-/history-4.6.2.tgz#12cfaba693ba20f114ed5765467ff25fdf67ddb0"
|
||||||
@@ -2463,6 +2467,10 @@ find-up@^2.0.0, find-up@^2.1.0:
|
|||||||
dependencies:
|
dependencies:
|
||||||
locate-path "^2.0.0"
|
locate-path "^2.0.0"
|
||||||
|
|
||||||
|
fingerprintjs2@^1.5.1:
|
||||||
|
version "1.5.1"
|
||||||
|
resolved "https://registry.yarnpkg.com/fingerprintjs2/-/fingerprintjs2-1.5.1.tgz#010691d425bc37fa0b5a7ec1ae85404bb3f934cb"
|
||||||
|
|
||||||
flatten@^1.0.2:
|
flatten@^1.0.2:
|
||||||
version "1.0.2"
|
version "1.0.2"
|
||||||
resolved "https://registry.yarnpkg.com/flatten/-/flatten-1.0.2.tgz#dae46a9d78fbe25292258cc1e780a41d95c03782"
|
resolved "https://registry.yarnpkg.com/flatten/-/flatten-1.0.2.tgz#dae46a9d78fbe25292258cc1e780a41d95c03782"
|
||||||
|
|||||||
Reference in New Issue
Block a user