Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@okta/okta-react

Package Overview
Dependencies
Maintainers
1
Versions
57
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@okta/okta-react

React support for Okta

  • 6.9.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created

What is @okta/okta-react?

@okta/okta-react is a library that provides React components and utilities to integrate Okta authentication into your React applications. It simplifies the process of adding authentication and authorization to your app by leveraging Okta's identity management services.

What are @okta/okta-react's main functionalities?

Authentication

This code demonstrates how to set up basic authentication in a React application using @okta/okta-react. It includes a secure route that requires authentication and a callback route for handling the login process.

import React from 'react';
import { Security, LoginCallback, SecureRoute } from '@okta/okta-react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';

const App = () => (
  <Router>
    <Security issuer='https://{yourOktaDomain}/oauth2/default' clientId='{clientId}' redirectUri={window.location.origin + '/login/callback'}>
      <Switch>
        <Route path='/login/callback' component={LoginCallback} />
        <SecureRoute path='/protected' component={ProtectedComponent} />
        <Route path='/' component={HomeComponent} />
      </Switch>
    </Security>
  </Router>
);

export default App;

Custom Login Component

This code shows how to create a custom login component using @okta/okta-react. The component uses the `withOktaAuth` higher-order component to access Okta's authentication methods.

import React from 'react';
import { withOktaAuth } from '@okta/okta-react';

class Login extends React.Component {
  handleLogin = () => {
    this.props.oktaAuth.signInWithRedirect();
  };

  render() {
    return (
      <div>
        <button onClick={this.handleLogin}>Login</button>
      </div>
    );
  }
}

export default withOktaAuth(Login);

Accessing User Information

This code demonstrates how to access and display user information in a React component using @okta/okta-react. It uses the `useOktaAuth` hook to get the authenticated user's information.

import React, { useEffect, useState } from 'react';
import { useOktaAuth } from '@okta/okta-react';

const UserProfile = () => {
  const { authState, oktaAuth } = useOktaAuth();
  const [userInfo, setUserInfo] = useState(null);

  useEffect(() => {
    if (authState.isAuthenticated) {
      oktaAuth.getUser().then(info => setUserInfo(info));
    }
  }, [authState, oktaAuth]);

  if (!userInfo) {
    return <div>Loading...</div>;
  }

  return (
    <div>
      <h1>Welcome, {userInfo.name}</h1>
      <p>Email: {userInfo.email}</p>
    </div>
  );
};

export default UserProfile;

Other packages similar to @okta/okta-react

Keywords

FAQs

Package last updated on 30 May 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc