npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@sensenet/authentication-oidc-react

v2.3.1

Published

React components and service for oidc authentication with sensenet

Downloads

15

Readme

@sensenet/authentication-oidc-react

@sensenet/authentication-oidc-react is a react component library that uses React context api for authenticating with oidc client.

NPM version NPM downloads License: GPL v2

Install

# Yarn
yarn add @sensenet/authentication-oidc-react

# NPM
npm install @sensenet/authentication-oidc-react

Usage

There are 2 components and 1 hook that you can use. AuthenticationProvider, OidcSecure, useOidcAuthentication.

AuthenticationProvider

For AuthenticationProvider to work properly you must pass a configuration object. To work with sensenet you must include sensenet as a scope and add the repository url as snrepo to extraQueryParams. See the example below for proper configuration.

Props

| name | type | required | description | | ------------------------- | ------------------------------------------- | :------: | -------------------------------------------------------------------------------------------------------------------------------------------------- | | history | History from 'history' | ✔ | history object from react-router or history package. Needed for navigation. | | configuration | UserManagerSettings from 'oidc-client' | ✔ | configuration object for oidc-client. These properties are required: client_id, redirect_uri, response_type, scope, authority, silent_redirect_uri | | children | () => ReactNode | ✔ | You can only use AuthenticationProvider as a wrapper. | | customEvents | CustomEvents | | You can subscribe to oidc events like userLoaded. Be aware that this object should not change during rerender. Eg.: you should wrap it in useMemo. | | authenticating | () => ReactNode | | Component shown when OidcSecure is used and login is required. | | notAuthenticated | () => ReactNode | | Component shown on route /authentication/not-authenticated | | notAuthorized | ({ login }) => ReactNode | | Component shown on route /authentication/not-authorized | | sessionLost | ElementType<{ onAuthenticate: () => void }> | | Component shown on route /authentication/session-lost | | callbackComponentOverride | () => ReactNode | | Component shown when login is redirecting back. |

Example

import { AuthenticationProvider, useOidcAuthentication, UserManagerSettings } from '@sensenet/authentication-oidc-react'
import { Repository } from '@sensenet/client-core'
import { RepositoryContext } from '@sensenet/hooks-react'
import React, { PropsWithChildren } from 'react'
import { BrowserRouter, useHistory } from 'react-router-dom'

export const repositoryUrl = 'https://my-service.sensenet.com/'

export const configuration: UserManagerSettings = {
  client_id: 'spa',
  automaticSilentRenew: true,
  redirect_uri: 'http://localhost:3000/authentication/callback',
  response_type: 'code',
  post_logout_redirect_uri: 'http://localhost:3000/',
  scope: 'openid profile sensenet',
  authority: 'https://is.my-service.sensenet.com/',
  silent_redirect_uri: 'http://localhost:3000/authentication/silent_callback',
  extraQueryParams: { snrepo: repositoryUrl },
}

export function AppProviders({ children }: PropsWithChildren<{}>) {
  return (
    <BrowserRouter>
      <AuthProvider>
        <RepositoryProvider>{children}</RepositoryProvider>
      </AuthProvider>
    </BrowserRouter>
  )
}

export const AuthProvider = ({ children }: PropsWithChildren<{}>) => {
  const history = useHistory()

  return (
    <AuthenticationProvider configuration={configuration} history={history}>
      {children}
    </AuthenticationProvider>
  )
}

export const RepositoryProvider = ({ children }: PropsWithChildren<{}>) => {
  const { oidcUser } = useOidcAuthentication()

  if (!oidcUser) {
    return <LoginForm />
  }

  return (
    <RepositoryContext.Provider value={new Repository({ repositoryUrl, token: oidcUser.access_token })}>
      {children}
    </RepositoryContext.Provider>
  )
}

OidcSecure

This component can be used to secure routes that needs authentication. This must be inside a router and the AuthenticationProvider of course .

Props

| name | type | required | description | | -------- | ---------------------- | :------: | --------------------------------------------------------------------------- | | history | History from 'history' | ✔ | history object from react-router or history package. Needed for navigation. | | children | ReactNode | ✔ | You can only use OidcSecure as a wrapper. |

Example

import React from 'react'
import { Switch, Route, useHistory } from 'react-router-dom'

const Routes = () => (
  <Switch>
    <Route path="/admin" component={Admin} />
  </Switch>
)

const Admin = ({ oidcUser }) => {
  const history = useHistory()

  return (
    <OidcSecure history={history}>
      <h1>Admin</h1>
      <p>Protected Admin</p>
    </OidcSecure>
  )
}

useOidcAuthentication

Custom hook for AuthenticationContext value. This hook can only be used inside the AuthenticationProvider.

Example

import React from 'react'
import { useOidcAuthentication } from '@sensenet/authentication-oidc-react'

const NavBar = ({ oidcUser }) => {
  const { login, logout, oidcUser, error, isLoading } = useOidcAuthentication()

  if (isLoading) {
    return <p>Loading...</p>
  }

  if (error) {
    return <p>Error: {error}</p>
  }

  return (
    <nav>
      <ul>
        <li onClick={login}>login</li>
        <li onClick={logout}>logout</li>
      </ul>
    </nav>
  )
}