diff --git a/src/App.js b/src/App.js index 23f7cbe..d5a32e8 100644 --- a/src/App.js +++ b/src/App.js @@ -11,7 +11,7 @@ import Register from './pages/auth/Register'; import Login from './pages/auth/Login'; import LoginWithEmail from './pages/auth/LoginWithEmail'; import RegisterWithEmail from './pages/auth/RegisterWithEmail'; -import UserService from './services/UserService'; +import KeyCloakService from './services/KeyCloakService'; const App = () => { return ( @@ -27,7 +27,7 @@ const App = () => { }/> { - UserService.isLoggedIn() ? <> + KeyCloakService.isLoggedIn() ? <> }/> }/> : <> diff --git a/src/api/loginTest.js b/src/api/loginTest.js new file mode 100644 index 0000000..8a48d2a --- /dev/null +++ b/src/api/loginTest.js @@ -0,0 +1,18 @@ +const loginTest = () => { + let data = { + client_id: 'Id_of_your_client', + username: 'test', + password: 'test', + grant_type: 'password' + }; + + fetch('http://0.0.0.0:8080/auth/realms/test/protocol/openid-connect/token', { + method: 'POST', + headers: {'Content-Type': 'application/json'}, + body: JSON.stringify(data) + }).then(res => { + console.log('Request complete! response:', res); + }); +}; + +export default loginTest; \ No newline at end of file diff --git a/src/components/elements/MobileNavMenu.js b/src/components/elements/MobileNavMenu.js index 17e6bcc..6d1f421 100644 --- a/src/components/elements/MobileNavMenu.js +++ b/src/components/elements/MobileNavMenu.js @@ -8,6 +8,7 @@ import styled from 'styled-components'; import {Link} from 'react-router-dom'; import {CHALLENGES_PAGE} from '../../utils/globals'; import PropsTypes from 'prop-types'; +import KeyCloakService from '../../services/KeyCloakService'; const MobileNavMenuStyle = styled(FlexColumn)` gap: 32px; @@ -21,7 +22,7 @@ const MobileNavMenuStyle = styled(FlexColumn)` align-items: flex-start; z-index: 3; - a { + a, button { cursor: pointer; div { @@ -63,12 +64,20 @@ const MobileNavMenu = (props) => { Register - - - - Sign in - - + {KeyCloakService.isLoggedIn() ? + + + + Sign out + + : + + + + Sign in + + + } ); diff --git a/src/components/elements/NavBar/NavBar.js b/src/components/elements/NavBar/NavBar.js index b0fdfc0..879ed9d 100644 --- a/src/components/elements/NavBar/NavBar.js +++ b/src/components/elements/NavBar/NavBar.js @@ -11,6 +11,7 @@ import registerIco from '../../../assets/register_ico.svg'; import {CHALLENGES_PAGE} from '../../../utils/globals'; import cupIco from '../../../assets/cup_ico.svg'; import NavBarStyle from './NavBarStyle'; +import KeyCloakService from '../../../services/KeyCloakService'; const MenuButton = styled(Container)` width: 20px; @@ -55,12 +56,20 @@ const NavBar = () => { Register - - - - Sign in - - + {KeyCloakService.isLoggedIn() ? + + + + Sign out + + : + + + + Sign in + + + } diff --git a/src/components/elements/NavBar/NavBarStyle.js b/src/components/elements/NavBar/NavBarStyle.js index e31007b..4659422 100644 --- a/src/components/elements/NavBar/NavBarStyle.js +++ b/src/components/elements/NavBar/NavBarStyle.js @@ -23,7 +23,7 @@ const NavBarStyle = styled(Container)` .ul-desktop { display: flex; - a { + a, button { cursor: pointer; div { diff --git a/src/components/sections/Hero.js b/src/components/sections/Hero.js index 8e4b0e3..e65dccb 100644 --- a/src/components/sections/Hero.js +++ b/src/components/sections/Hero.js @@ -7,7 +7,6 @@ import {Link} from 'react-router-dom'; import Media from 'react-media'; import codepenIco from '../../assets/codepen_ico.svg'; import styled from 'styled-components'; -import UserService from '../../services/UserService'; const TitleParagraph = styled(Medium)` font-size: 20px; @@ -52,12 +51,6 @@ const desktopRender = () => { Join us! - - diff --git a/src/index.js b/src/index.js index bf9a083..6c683c5 100644 --- a/src/index.js +++ b/src/index.js @@ -3,7 +3,7 @@ import ReactDOM from 'react-dom/client'; import './index.css'; import './normalize.css'; import App from './App'; -import UserService from './services/UserService'; +import KeyCloakService from './services/KeyCloakService'; import HttpService from './services/HttpService'; const root = ReactDOM.createRoot(document.getElementById('root')); @@ -14,5 +14,5 @@ const renderApp = () => root.render( ); -UserService.initKeycloak(renderApp); +KeyCloakService.initKeycloak(renderApp); HttpService.configure(); \ No newline at end of file diff --git a/src/pages/Challanges/Challenges.js b/src/pages/Challanges/Challenges.js index d01889b..0b843ce 100644 --- a/src/pages/Challanges/Challenges.js +++ b/src/pages/Challanges/Challenges.js @@ -12,7 +12,6 @@ import cupIco from '../../assets/cup_ico.svg'; import getChallenges from '../../api/getChallenges'; import {CALC_PAGES} from '../../utils/globals'; import Loading from '../../components/elements/Loading'; -import UserService from '../../services/UserService'; const Challenges = () => { const [pageNr, setPageNr] = React.useState(1); @@ -121,12 +120,6 @@ const Challenges = () => {

Challenges

- - Increase your machine learning skills by competing in our exciting challenges. diff --git a/src/services/HttpService.js b/src/services/HttpService.js index 0509812..805f3c8 100644 --- a/src/services/HttpService.js +++ b/src/services/HttpService.js @@ -1,5 +1,5 @@ import axios from 'axios'; -import UserService from './UserService'; +import KeyCloakService from './KeyCloakService'; const HttpMethods = { GET: 'GET', @@ -11,12 +11,12 @@ const _axios = axios.create(); const configure = () => { _axios.interceptors.request.use((config) => { - if (UserService.isLoggedIn()) { + if (KeyCloakService.isLoggedIn()) { const cb = () => { - config.headers.Authorization = `Bearer ${UserService.getToken()}`; + config.headers.Authorization = `Bearer ${KeyCloakService.getToken()}`; return Promise.resolve(config); }; - return UserService.updateToken(cb); + return KeyCloakService.updateToken(cb); } }); }; diff --git a/src/services/UserService.js b/src/services/KeyCloakService.js similarity index 95% rename from src/services/UserService.js rename to src/services/KeyCloakService.js index 05cc9e3..ba51318 100644 --- a/src/services/UserService.js +++ b/src/services/KeyCloakService.js @@ -40,7 +40,7 @@ const getUsername = () => _kc.tokenParsed?.preferred_username; const hasRole = (roles) => roles.some((role) => _kc.hasRealmRole(role)); -const UserService = { +const KeyCloakService = { initKeycloak, doLogin, doLogout, @@ -51,4 +51,4 @@ const UserService = { hasRole, }; -export default UserService; \ No newline at end of file +export default KeyCloakService; \ No newline at end of file