config for oauth
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
||||
summerizer.py
|
||||
.env
|
||||
@@ -5,6 +5,7 @@ import SideNavigation from "./components/SideNavigation";
|
||||
import MarkdownContent from "./components/MarkdownContent";
|
||||
import "./App.css";
|
||||
import Callback from "./Callback";
|
||||
import {config} from "./confs/appConfig";
|
||||
|
||||
const App = () => {
|
||||
return (
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import React, { createContext, useEffect, useState } from "react";
|
||||
import { UserManager } from "oidc-client-ts";
|
||||
import {oidcConfig} from "./oicdConfig";
|
||||
import {appConfig} from "./confs/appConfig";
|
||||
|
||||
|
||||
export const AuthContext = createContext({
|
||||
@@ -11,19 +11,32 @@ export const AuthContext = createContext({
|
||||
|
||||
const AuthProvider = ({ children }) => {
|
||||
const [user, setUser] = useState(null);
|
||||
const userManager = new UserManager(oidcConfig);
|
||||
const userManager = new UserManager(appConfig.oidcConfig);
|
||||
|
||||
useEffect(() => {
|
||||
userManager.getUser().then((user) => {
|
||||
userManager.getUser()
|
||||
.then((user) => {
|
||||
if (user && !user.expired) {
|
||||
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]);
|
||||
|
||||
const login = () => {
|
||||
console.log('triggered');
|
||||
userManager.signinRedirect().catch((err) => {console.log(err)});
|
||||
userManager.signinRedirect().catch((err) => {
|
||||
console.log(appConfig);
|
||||
console.log(err);
|
||||
});
|
||||
}
|
||||
const logout = () => userManager.signoutRedirect();
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import React, { useEffect } from "react";
|
||||
import { UserManager } from "oidc-client-ts";
|
||||
import {oidcConfig} from "./oicdConfig";
|
||||
import {appConfig} from "./confs/appConfig";
|
||||
|
||||
|
||||
const Callback = () => {
|
||||
useEffect(() => {
|
||||
const userManager = new UserManager(oidcConfig);
|
||||
const userManager = new UserManager(appConfig.oidcConfig);
|
||||
userManager.signinRedirectCallback().then(() => {
|
||||
window.location.href = "/";
|
||||
});
|
||||
|
||||
@@ -16,7 +16,7 @@ const MainNavigation = () => {
|
||||
{ user && user.profile ? (
|
||||
<div>
|
||||
<h1>{user.profile.name}</h1>
|
||||
<Link to="/logout">Logout</Link>
|
||||
<button onClick={logout}>logout</button>
|
||||
</div>
|
||||
) : (
|
||||
<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};
|
||||
12
src/index.js
12
src/index.js
@@ -2,12 +2,18 @@ import React from "react";
|
||||
import ReactDOM from "react-dom/client";
|
||||
import App from "./App";
|
||||
import AuthProvider from "./AuthProvider";
|
||||
import {config} from "./confs/appConfig";
|
||||
|
||||
//ReactDOM.render(<App />, document.getElementById("root"));
|
||||
|
||||
const root = ReactDOM.createRoot(document.getElementById("root"));
|
||||
root.render(
|
||||
const initializeApp = async () => {
|
||||
await config();
|
||||
const root = ReactDOM.createRoot(document.getElementById("root"));
|
||||
root.render(
|
||||
<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 {
|
||||
const fetchPromise = fetch(url)
|
||||
const token = localStorage.getItem("accessToken");
|
||||
const headers = {
|
||||
...(token? {Authorization: `Bearer ${token}`} : {}),
|
||||
}
|
||||
const fetchPromise = fetch(url, {headers})
|
||||
.then((response) => {
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
//webpack.config.js
|
||||
const path = require('path');
|
||||
const HtmlWebpackPlugin = require('html-webpack-plugin');
|
||||
const webpack = require('webpack')
|
||||
|
||||
module.exports = {
|
||||
entry: './src/index.js',
|
||||
@@ -27,7 +28,11 @@ module.exports = {
|
||||
plugins: [
|
||||
new HtmlWebpackPlugin({
|
||||
template: "./public/index.html",
|
||||
})
|
||||
}),
|
||||
new webpack.DefinePlugin({
|
||||
FRONT_END_CLIENT_ID: process.env.FRONT_END_CLIENT_ID,
|
||||
SERVER_HOST: process.env.SERVER_HOST,
|
||||
}),
|
||||
],
|
||||
devServer: {
|
||||
static: path.join(__dirname, 'public'),
|
||||
|
||||
Reference in New Issue
Block a user