Compare commits
1 Commits
321dea202d
...
55ddd17bf0
| Author | SHA1 | Date | |
|---|---|---|---|
| 55ddd17bf0 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
summerizer.py
|
summerizer.py
|
||||||
|
.env
|
||||||
@@ -5,6 +5,7 @@ import SideNavigation from "./components/SideNavigation";
|
|||||||
import MarkdownContent from "./components/MarkdownContent";
|
import MarkdownContent from "./components/MarkdownContent";
|
||||||
import "./App.css";
|
import "./App.css";
|
||||||
import Callback from "./Callback";
|
import Callback from "./Callback";
|
||||||
|
import {config} from "./confs/appConfig";
|
||||||
|
|
||||||
const App = () => {
|
const App = () => {
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import React, { createContext, useEffect, useState } from "react";
|
import React, { createContext, useEffect, useState } from "react";
|
||||||
import { UserManager } from "oidc-client-ts";
|
import { UserManager } from "oidc-client-ts";
|
||||||
import {oidcConfig} from "./oicdConfig";
|
import {appConfig} from "./confs/appConfig";
|
||||||
|
|
||||||
|
|
||||||
export const AuthContext = createContext({
|
export const AuthContext = createContext({
|
||||||
@@ -11,19 +11,32 @@ export const AuthContext = createContext({
|
|||||||
|
|
||||||
const AuthProvider = ({ children }) => {
|
const AuthProvider = ({ children }) => {
|
||||||
const [user, setUser] = useState(null);
|
const [user, setUser] = useState(null);
|
||||||
const userManager = new UserManager(oidcConfig);
|
const userManager = new UserManager(appConfig.oidcConfig);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
userManager.getUser().then((user) => {
|
userManager.getUser()
|
||||||
|
.then((user) => {
|
||||||
if (user && !user.expired) {
|
if (user && !user.expired) {
|
||||||
setUser(user);
|
setUser(user);
|
||||||
|
localStorage.setItem("accessToken", user.access_token);
|
||||||
|
} else if (user && user.expired) {
|
||||||
|
userManager.signinSilent()
|
||||||
|
.then((newUser) => {
|
||||||
|
setUser(newUser);
|
||||||
|
localStorage.setItem("accessToken", newUser.access_token);
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.error(err);
|
||||||
|
})
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}, [userManager]);
|
}, [userManager]);
|
||||||
|
|
||||||
const login = () => {
|
const login = () => {
|
||||||
console.log('triggered');
|
userManager.signinRedirect().catch((err) => {
|
||||||
userManager.signinRedirect().catch((err) => {console.log(err)});
|
console.log(appConfig);
|
||||||
|
console.log(err);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
const logout = () => userManager.signoutRedirect();
|
const logout = () => userManager.signoutRedirect();
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
import React, { useEffect } from "react";
|
import React, { useEffect } from "react";
|
||||||
import { UserManager } from "oidc-client-ts";
|
import { UserManager } from "oidc-client-ts";
|
||||||
import {oidcConfig} from "./oicdConfig";
|
import {appConfig} from "./confs/appConfig";
|
||||||
|
|
||||||
|
|
||||||
const Callback = () => {
|
const Callback = () => {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const userManager = new UserManager(oidcConfig);
|
const userManager = new UserManager(appConfig.oidcConfig);
|
||||||
userManager.signinRedirectCallback().then(() => {
|
userManager.signinRedirectCallback().then(() => {
|
||||||
window.location.href = "/";
|
window.location.href = "/";
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ const MainNavigation = () => {
|
|||||||
{ user && user.profile ? (
|
{ user && user.profile ? (
|
||||||
<div>
|
<div>
|
||||||
<h1>{user.profile.name}</h1>
|
<h1>{user.profile.name}</h1>
|
||||||
<Link to="/logout">Logout</Link>
|
<button onClick={logout}>logout</button>
|
||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<li>
|
<li>
|
||||||
|
|||||||
30
src/confs/appConfig.js
Normal file
30
src/confs/appConfig.js
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
|
||||||
|
const appConfig = {
|
||||||
|
serverHost : null,
|
||||||
|
kc_client_id: null,
|
||||||
|
oidcConfig : null
|
||||||
|
};
|
||||||
|
|
||||||
|
const config = async () => {
|
||||||
|
await fetch("http://127.0.0.1:5000/api/config/server_host")
|
||||||
|
.then((res) => res.json())
|
||||||
|
.then((data) => appConfig.serverHost = data['value'])
|
||||||
|
.catch((err) => console.error(err));
|
||||||
|
await fetch("http://127.0.0.1:5000/api/config/kc_client_id")
|
||||||
|
.then((res) => res.json())
|
||||||
|
.then((data) => appConfig["kc_client_id"] = data["value"])
|
||||||
|
.catch((err) => console.error(err));
|
||||||
|
appConfig.oidcConfig = {
|
||||||
|
authority: "https://login.hangman-lab.top/realms/Hangman-Lab",
|
||||||
|
client_id: appConfig.kc_client_id,
|
||||||
|
redirect_uri: `${appConfig.serverHost}/callback`,
|
||||||
|
post_logout_redirect_uri: appConfig.serverHost,
|
||||||
|
response_type: "code",
|
||||||
|
scope: "openid profile email",
|
||||||
|
|
||||||
|
};
|
||||||
|
console.log(appConfig);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
export {appConfig, config};
|
||||||
18
src/index.js
18
src/index.js
@@ -2,12 +2,18 @@ import React from "react";
|
|||||||
import ReactDOM from "react-dom/client";
|
import ReactDOM from "react-dom/client";
|
||||||
import App from "./App";
|
import App from "./App";
|
||||||
import AuthProvider from "./AuthProvider";
|
import AuthProvider from "./AuthProvider";
|
||||||
|
import {config} from "./confs/appConfig";
|
||||||
|
|
||||||
//ReactDOM.render(<App />, document.getElementById("root"));
|
//ReactDOM.render(<App />, document.getElementById("root"));
|
||||||
|
|
||||||
const root = ReactDOM.createRoot(document.getElementById("root"));
|
const initializeApp = async () => {
|
||||||
root.render(
|
await config();
|
||||||
<AuthProvider>
|
const root = ReactDOM.createRoot(document.getElementById("root"));
|
||||||
<App />
|
root.render(
|
||||||
</AuthProvider>
|
<AuthProvider>
|
||||||
);
|
<App />
|
||||||
|
</AuthProvider>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
initializeApp();
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
export const oidcConfig = {
|
|
||||||
authority: "https://login.hangman-lab.top/realms/Hangman-Lab",
|
|
||||||
client_id: "labdev",
|
|
||||||
redirect_uri: "http://localhost:3000/callback",
|
|
||||||
post_logout_redirect_uri: "http://localhost:3000",
|
|
||||||
response_type: "code",
|
|
||||||
scope: "openid profile email",
|
|
||||||
};
|
|
||||||
@@ -19,7 +19,11 @@ export async function fetchWithCache(url, cacheKey = url, cacheExpiry = 60) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const fetchPromise = fetch(url)
|
const token = localStorage.getItem("accessToken");
|
||||||
|
const headers = {
|
||||||
|
...(token? {Authorization: `Bearer ${token}`} : {}),
|
||||||
|
}
|
||||||
|
const fetchPromise = fetch(url, {headers})
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`HTTP error! Status: ${response.status}`);
|
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
//webpack.config.js
|
//webpack.config.js
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const HtmlWebpackPlugin = require('html-webpack-plugin');
|
const HtmlWebpackPlugin = require('html-webpack-plugin');
|
||||||
|
const webpack = require('webpack')
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
entry: './src/index.js',
|
entry: './src/index.js',
|
||||||
@@ -27,7 +28,11 @@ module.exports = {
|
|||||||
plugins: [
|
plugins: [
|
||||||
new HtmlWebpackPlugin({
|
new HtmlWebpackPlugin({
|
||||||
template: "./public/index.html",
|
template: "./public/index.html",
|
||||||
})
|
}),
|
||||||
|
new webpack.DefinePlugin({
|
||||||
|
FRONT_END_CLIENT_ID: process.env.FRONT_END_CLIENT_ID,
|
||||||
|
SERVER_HOST: process.env.SERVER_HOST,
|
||||||
|
}),
|
||||||
],
|
],
|
||||||
devServer: {
|
devServer: {
|
||||||
static: path.join(__dirname, 'public'),
|
static: path.join(__dirname, 'public'),
|
||||||
|
|||||||
Reference in New Issue
Block a user