Giter Site home page Giter Site logo

sdu-oidc-jwt-client's Introduction

oidc-jwt-client

Fetch JWTs for API access from oidc-jwt-provider

Installation

npm install oidc-jwt-client --save

How to use

<OidcJwtProvider
  client={{ url: 'https://api-auth.acc.titan.awssdu.nl' }}
  shouldAttemptLogin={false}
  shouldMonitorAccessTokens={false}
>
  // Contents of your app
</OidcJwtProvider>

When you come back after authorization to your app it will have a token in the url like this ?token=. To replace this token we use window.history.replaceState() by default. If you would like to replace this behaviour you could send a custom removeTokenFromUrlFunction.

In NextJS you could create a helper function like this:

// removeTokenFromUrlFunction.ts
import Router from 'next/router';
import { stripTokenFromUrl } from 'oidc-jwt-client';

const removeTokenFromUrlFunction = (url: string) => {
  const urlWithoutToken = stripTokenFromUrl(url);
  Router.replace(urlWithoutToken, undefined, { shallow: true });
};

export { removeTokenFromUrlFunction };

And then use it like this:

// App.tsx
<OidcJwtProvider
  client={{ url: 'https://api-auth.ota.titan2.awssdu.nl' }}
  shouldAttemptLogin={false}
  shouldMonitorAccessTokens={false}
  removeTokenFromUrlFunction={removeTokenFromUrlFunction}
>
  // Contents of your app
</OidcJwtProvider>

Fetch an accessToken

Within the provider we make use of several hooks to use the functionality exposed within the context.

The accessToken is directly returned from the fetchAccessToken function when already present and valid. If not it will automatically fetch a new accessToken for you.

To get the accessToken you can do this:

const [token, setToken] = (useState < null) | (string > null);
const fetchAccessToken = useAuthAccessToken();

useEffect(() => {
  fetchAccessToken().then((token) => {
    setToken(token);
  });
}, [fetchAccessToken, setToken]);

Login and Logout functions

To login or logout a user manually you can make use of these two function exposed by the useAuthControls hook:

const { authorize, logout } = useAuthControls();

const onClickLogout = React.useCallback(() => {
  logout();
}, [logout]);

const onClickLogin = React.useCallback(() => {
  authorize();
}, [authorize]);

Check if authentication client has initialized

Checks when the loadInitialData function is done executing and will return true when finished. NB! This doesn't mean you're logged in, jsut that the authentication is done initializing.

const isInitialized = useAuthInitialized();
console.log('Auth is initialized: ', isInitialized);

Get User Info

To get the user info you can do this within the context of the provider:

const { value, loading } = useAuthUserInfo();
console.log('This is the userInfo: ', value);

Get the Claims

To get the claims you can do this within the context of the provider:

const { value, loading } = useAuthAccessClaims();
console.log('These are the claims: ', value);

Check if a user is logged in.

Checking if the user is logged in so that you can act on it.

const isLoggedIn = useAuthIsLoggedIn();
console.log('Is the user loggedin? ', isLoggedIn);

Check if the users session has expired

Checking if the users session has expired

const isSessionExpired = useAuthSessionExpired();
console.log('Is the users session expired? ', isSessionExpired);

sdu-oidc-jwt-client's People

Contributors

daphnesmit avatar sbesselsen avatar lennardwesterveld avatar tpdewolf avatar diederikgithub avatar

Stargazers

 avatar  avatar

Watchers

Edgar Post-Buijs avatar Carlo Bernardini avatar Erik Verheijden avatar James Cloos avatar  avatar  avatar Michel Vermeer avatar Wart avatar Nikola Zivkovic avatar

sdu-oidc-jwt-client's Issues

The automated release is failing 🚨

🚨 The automated release from the develop branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you can benefit from your bug fixes and new features again.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can fix this πŸ’ͺ.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the develop branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here are some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


Invalid npm token.

The npm token configured in the NPM_TOKEN environment variable must be a valid token allowing to publish to the registry https://registry.npmjs.org/.

If you are using Two Factor Authentication for your account, set its level to "Authorization only" in your account settings. semantic-release cannot publish with the default "
Authorization and writes" level.

Please make sure to set the NPM_TOKEN environment variable in your CI with the exact value of the npm token.


Good luck with your project ✨

Your semantic-release bot πŸ“¦πŸš€

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.