New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

react-oauth-login

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-oauth-login

A lightweight React library for OAuth 2.0 authentication and token management

latest
Source
npmnpm
Version
1.0.0
Version published
Weekly downloads
7
Maintainers
1
Weekly downloads
 
Created
Source

react-oauth-login

react-oauth-loginは、ReactでOAuth 2.0の認証を簡単に実装するためのライブラリです。useOAuthフックとOAuthコンポーネントを提供し、アクセストークンの取得や検証をシンプルに行うことができます。

特徴

  • OAuth 2.0認証の簡易化
  • アクセストークンのローカルストレージ管理
  • Reactフック (useOAuth) とコンポーネント (OAuth) のサポート
  • トークンの有効期限管理と自動リフレッシュ

インストール

npm install react-oauth-login

使用方法

フック: useOAuth

useOAuthは、OAuth 2.0認証を処理するためのカスタムフックです。

パラメータ

  • endpoint: 認証エンドポイントURL(例: GoogleのOAuthエンドポイント)
  • params: 認証時に必要なクエリパラメータ

戻り値

  • token: アクセストークン
  • verifyToken: トークンの有効期限を確認する関数

使用例

import { useOAuth } from 'react-oauth-login'

const App = () => {
  const { token, verifyToken } = useOAuth({
    endpoint: 'https://accounts.google.com/o/oauth2/v2/auth',
    params: {
      client_id: 'YOUR_CLIENT_ID',
      redirect_uri: 'YOUR_REDIRECT_URI',
      response_type: 'token',
      scope: 'YOUR_REQUESTED_SCOPE',
    },
  })

  if (!verifyToken()) {
    return <p>Authenticating...</p>
  }

  return <p>Authenticated! Token: {token}</p>
}

コンポーネント: OAuth

OAuthコンポーネントは、useOAuthをラップして、Reactのコンポーネント構造で使いやすくしたものです。

Props

  • endpoint: 認証エンドポイントURL
  • params: 認証時に必要なクエリパラメータ
  • children: トークン情報と検証関数を受け取る子コンポーネント

使用例

import { OAuth } from 'react-oauth-login'

const App = () => {
  return (
    <OAuth
      endpoint="https://accounts.google.com/o/oauth2/v2/auth"
      params={{
        client_id: 'YOUR_CLIENT_ID',
        redirect_uri: 'YOUR_REDIRECT_URI',
        response_type: 'token',
        scope: 'YOUR_REQUESTED_SCOPE',
      }}
    >
      {({ token, verifyToken }) => (
        verifyToken() ? <p>Token: {token}</p> : <p>Authenticating...</p>
      )}
    </OAuth>
  )
}

応用例: Firebase Cloud Messaging (FCM)

以下は、取得したアクセストークンを使用してFirebase Cloud MessagingのAPIを呼び出す例です。

import { OAuth } from 'react-oauth-login'

export const App = () => {
  const sendHandler = async (e, token) => {
    e.preventDefault()

    const form = new FormData(e.target)

    try {
      const res = await fetch('https://fcm.googleapis.com/v1/projects/YOUR_PROJECT_ID/messages:send', {
        method: 'POST',
        headers: {
          Authorization: `Bearer ${token}`,
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          message: {
            token: form.get('destination'),
            notification: {
              title: form.get('title'),
              body: form.get('body'),
            },
          },
        }),
      })

      const data = await res.json()
      console.log(data)
    } catch (err) {
      console.error(err)
    }
  }

  return (
    <OAuth
      endpoint="https://accounts.google.com/o/oauth2/v2/auth"
      params={{
        client_id: 'YOUR_CLIENT_ID',
        redirect_uri: 'YOUR_REDIRECT_URI',
        response_type: 'token',
        scope: 'https://www.googleapis.com/auth/firebase.messaging',
      }}
    >
      {({ token }) => (
        <form onSubmit={(e) => sendHandler(e, token)}>
          <label>User Device Token:
            <input name="destination" required />
          </label>

          <label>Title:
            <input name="title" required />
          </label>

          <label>Body:
            <input name="body" required />
          </label>

          <button type="submit">Send Notification</button>
        </form>
      )}
    </OAuth>
  )
}

作者

@shinosaki

ライセンス

MIT License

Keywords

react

FAQs

Package last updated on 28 Dec 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