登録するログイン
ドキュメント価格設定
登録するログイン
  • はじめる
  • 導入
  • クイックスタート
  • SDK
  • React
  • TypeScript
  • Next.js
  • Express
  • NestJS
  • Python
  • APIリファレンス
  • サポートとリソース
  • よくある質問
  • 接触

useAuth

useAuthフックは、ユーザーオブジェクトや読み込みステータスなど、現在の認証状態へのアクセスを提供します。


基本的な使い方

import { useAuth } from 'authsafe-react';

function UserProfile() {
  const { user, isAuthenticated, isLoading } = useAuth();

  if (isLoading) return <div>Loading...</div>;
  if (!isAuthenticated) return <div>Please log in</div>;

  return (
    <div>
      <h2>Welcome, {user?.name}!</h2>
      <p>Email: {user?.email}</p>
    </div>
  );
}

戻り値

財産タイプ説明
userUser | null現在のユーザーオブジェクト(認証されていない場合はnull)
isAuthenticatedbooleanユーザーが現在認証されているかどうか
isLoadingboolean認証状態が読み込まれているかどうか
errorError | null認証エラーが発生した場合

ユーザーオブジェクトプロパティ

財産タイプ説明
substring固有のユーザー識別子
namestringユーザーの表示名
emailstringユーザーのメールアドレス
picturestring | undefinedユーザーのプロフィール写真へのURL
email_verifiedboolean | undefinedメールアドレスが認証されているかどうか

ルートの保護

認証ステータスに基づいてコンテンツを条件付きでレンダリングするには、フックを使用します。

import { useAuth } from 'authsafe-react';
import { Navigate } from 'react-router-dom';

function ProtectedRoute({ children }) {
  const { isAuthenticated, isLoading } = useAuth();

  if (isLoading) return <div>Loading...</div>;
  if (!isAuthenticated) return <Navigate to="/login" />;

  return children;
}

型定義

export interface User {
  sub: string;
  name?: string;
  email?: string;
  picture?: string;
  email_verified?: boolean;
  [key: string]: any;
}

export interface IUseAuth {
  user: User | null;
  isAuthenticated: boolean;
  isLoading: boolean;
  error: Error | null;
}

ベストプラクティス
  • 認証に依存するUIをレンダリングする前に、必ずisLoadingを確認してください。
  • isAuthenticated を使用して、保護されたコンテンツを条件付きでレンダリングします。
  • エラー状態を適切に処理し、フォールバックUIを使用する
  • オプションチェーン(user?.email)を使用して、ユーザープロパティに安全にアクセスできます。
  • ルートガードと組み合わせることで完全な保護を実現します

AuthSafe

Product

HighlightFeatureIntegrationPricingFAQ

Company

AboutBlogContact

Developer

DashboardDocumentation

Legal

Terms & ConditionsPrivacyComplianceShippingCancellationAI

© 2026 AuthSafe. All rights reserved.

私たちはあなたのプライバシーを尊重します

このウェブサイトでは、お客様の体験を向上させるために匿名の分析Cookieを使用しています。個人情報の保存や共有は一切行いません。分析トラッキングはいつでも許可または拒否できます。プライバシーポリシーをご覧ください。

匿名の分析Cookieを使用しています。個人情報は保存されません。プライバシーポリシーをご覧ください。