Skip to content

Instantly share code, notes, and snippets.

View Franpastoragusti's full-sized avatar

Francisco Pastor Agustí Franpastoragusti

View GitHub Profile
/* /src/app.jsx */
import React, {Component} from "react";
import { AuthProvider } from "./providers/authProvider";
import { BrowserRouter } from "react-router-dom";
import {Routes} from "./routes/routes";
export class App extends Component {
render() {
return (
<AuthProvider>
import * as React from "react";
import { Route, Switch } from "react-router-dom";
import { Callback } from "../components/auth/callback";
import { Logout } from "../components/auth/logout";
import { LogoutCallback } from "../components/auth/logoutCallback";
import { PrivateRoute } from "./privateRoute";
import { Register } from "../components/auth/register";
import { SilentRenew } from "../components/auth/silentRenew";
/* /src/components/auth/silentRenew.jsx */
import React from "react";
import { AuthConsumer } from "../../providers/authProvider";
export const SilentRenew = () => (
<AuthConsumer>
{({ signinSilentCallback }) => {
signinSilentCallback();
return <span>loading</span>;
}}
/* /src/components/auth/logoutCallback.jsx */
import React from "react";
import { AuthConsumer } from "../../providers/authProvider";
export const LogoutCallback = () => (
<AuthConsumer>
{({ signoutRedirectCallback }) => {
signoutRedirectCallback();
return <span>loading</span>;
/* /src/components/auth/logout.jsx */
import React from "react";
import { AuthConsumer } from "../../providers/authProvider";
export const Logout = () => (
<AuthConsumer>
{({ logout }) => {
logout();
return <span>loading</span>;
/* /src/components/auth/callback.jsx */
import React from "react";
import { AuthConsumer } from "../../providers/authProvider";
export const Callback = () => (
<AuthConsumer>
{({ signinRedirectCallback }) => {
signinRedirectCallback();
return <span>loading</span>;
}}
/* /src/routes/privateRoute.jsx */
import React from "react";
import { Route } from "react-router-dom";
import { AuthConsumer } from "../providers/authProvider";
export const PrivateRoute = ({ component, ...rest }) => {
const renderFn = (Component) => (props) => (
<AuthConsumer>
{({ isAuthenticated, signinRedirect }) => {
if (!!Component && isAuthenticated()) {
/* /src/providers/authProvider.jsx */
import React, {Component} from "react";
import AuthService from "../services/authService";
const AuthContext = React.createContext({
signinRedirectCallback: () => ({}),
logout: () => ({}),
signoutRedirectCallback: () => ({}),
isAuthenticated: () => ({}),
import { IDENTITY_CONFIG, METADATA_OIDC } from "../utils/authConst";
import { UserManager, WebStorageStateStore, Log } from "oidc-client";
export default class AuthService {
UserManager;
constructor() {
this.UserManager = new UserManager({
...IDENTITY_CONFIG,
userStore: new WebStorageStateStore({ store: window.sessionStorage }),
/* /src/utils/authConst.js */
export const IDENTITY_CONFIG = {
authority: process.env.REACT_APP_AUTH_URL, //(string): The URL of the OIDC provider.
client_id: process.env.REACT_APP_IDENTITY_CLIENT_ID, //(string): Your client application's identifier as registered with the OIDC provider.
redirect_uri: process.env.REACT_APP_REDIRECT_URL, //The URI of your client application to receive a response from the OIDC provider.
login: process.env.REACT_APP_AUTH_URL + "/login",
automaticSilentRenew: false, //(boolean, default: false): Flag to indicate if there should be an automatic attempt to renew the access token prior to its expiration.
loadUserInfo: false, //(boolean, default: true): Flag to control if additional identity data is loaded from the user info endpoint in order to populate the user's profile.
silent_redirect_uri: process.env.REACT_APP_SILENT_REDIRECT_URL, //(string): The URL for the page containing the code handling the silent renew.