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
const token = authHeader && authHeader.split(' ')[1]; | |
if (token == null) return res.sendStatus(401); | |
// Verify JWT Token Signature | |
let decodedToken = jwt.decode(token, { complete: true }); | |
let kid = decodedToken.header.kid; | |
let clientId = decodedToken.payload.client_id; | |
// Checks if client id is expected id | |
if (clientId !== VALID_CLIENT_ID) { |
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
app.use(cors()); | |
app.use('/adminInfo', async (req, res) => { | |
// Code Here | |
}); | |
app.listen(8080, () => console.log('API is running on http://localhost:8080/adminInfo')); |
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
const express = require('express'); | |
const cors = require('cors'); | |
const app = express(); | |
const jwt = require('jsonwebtoken'); | |
const jwksClient = require('jwks-rsa'); | |
const VALID_CLIENT_ID = 'YOUR CLIENT ID'; |
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
return ( | |
<div className="wrapper"> | |
<h1>Application</h1> | |
<button onClick={() => { | |
if (isLoggingIn) { | |
handleCancelLogin(); | |
return; | |
} | |
if (isLoggedIn) { | |
handleLogout(); |
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
auth.on("closed", () => { | |
setIsLoggingIn(false); | |
}); | |
auth.on("authenticated", result => { | |
setIsLoggedIn(true); | |
updateUserTokens(); | |
}); | |
auth.on("logged_out", () => { |
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
function updateUserTokens() { | |
const accessToken = auth.getAccessToken(); | |
const accessTokenJwt = auth.getDecodedAccessToken(); | |
const idTokenJwt = auth.getDecodedIDToken(); | |
setUser({ | |
accessToken: accessToken, | |
accessTokenJwt: accessTokenJwt, | |
idTokenJwt: idTokenJwt | |
}); | |
} |
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
useEffect(() => { | |
if (isLoggedIn) { | |
updateUserTokens(); | |
navigate('/protected'); | |
} | |
}, [isLoggedIn, navigate]); |
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
const [ user, setUser ] = useState(null); | |
const [ isLoggingIn, setIsLoggingIn ] = useState(false); | |
const [ isLoggedIn, setIsLoggedIn ] = useState(auth.isAuthenticated()); | |
const navigate = useNavigate(); | |
const handleLogin = () => { | |
setIsLoggingIn(true); | |
auth.showLogin(); | |
}; | |
const handleLogout = () => auth.logout(); | |
const handleCancelLogin = () => auth.close(); |
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
import './App.css'; | |
import React, { useState, useEffect } from 'react'; | |
import { Route, Routes, useNavigate } from 'react-router-dom'; | |
import ProtectedPage from './components/ProtectedPage/ProtectedPage'; | |
import PublicPage from './components/PublicPage/PublicPage'; | |
import ProtectedRoute from './components/ProtectedRoute/ProtectedRoute'; | |
import * as onz from 'onz-auth'; |
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
import React from 'react'; | |
import ReactDOM from 'react-dom/client'; | |
import './index.css'; | |
import App from './App'; | |
import reportWebVitals from './reportWebVitals'; | |
import { BrowserRouter } from 'react-router-dom'; | |
const root = ReactDOM.createRoot(document.getElementById('root')); | |
root.render( | |
<React.StrictMode> |
NewerOlder