1.9.31 • Published 5 months ago

auth0-client-sk v1.9.31

Weekly downloads
-
License
ISC
Repository
-
Last release
5 months ago

Introduction

Auth0 Client SK authentication library for Next.js applications.

Installation

Install the package using npm:

npm install auth0-client-sk@latest

Configuration

Environment Variables

Create a .env file in your project root with the following configuration:

# Auth0 Configuration
CLIENT_ID=your_auth0_client_id
CLIENT_SECRET=your_auth0_client_secret
REDIRECT_URI=http://localhost:3000/api/auth/callback/github
JWT_SECRET=your_secure_jwt_secret

# Public Configuration
NEXT_PUBLIC_REDIRECT_URI=http://localhost:3000/api/auth/callback/github

# Post-Authentication Routes
POST_AUTH_URL=http://localhost:3000/protected 
POST_LOGOUT_URL=http://localhost:3000

Required Environment Variables Explained

VariableDescription
CLIENT_IDYour Auth0 application client ID
CLIENT_SECRETYour Auth0 application client secret
REDIRECT_URIThe callback URL after authentication
JWT_SECRETSecret key for JWT token encryption
NEXT_PUBLIC_REDIRECT_URIPublicly accessible redirect URI
POST_AUTH_URLURL to redirect after successful authentication
POST_LOGOUT_URLURL to redirect after logout

Implementation

API Route Handler

Create a catch-all API route for authentication:

// app/api/auth/[...auth]/route.ts
import { handler } from "auth0-client-sk/Integrations";

export const GET = handler;

Authentication Components

Login Button

Implement the login functionality in your components:

import { signin } from "auth0-client-sk/Auth";

const LoginButton = () => (
  <button
    onClick={signin}
    className="your-button-styles"
  >
    Login
  </button>
);

Session Management

Client-Side Session

Use the useClientSession hook in client components:

"use client"
import { useClientSession } from 'auth0-client-sk/Integrations'

const ProfilePage = () => {
  const { isAuthenticated, isPending, user } = useClientSession();

  if (isPending) return <div>Loading...</div>;

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

  return (
    <div>
      <h1>Welcome, {user?.name}</h1>
      {/* Your protected content */}
    </div>
  );
};

Server-Side Session

Access session data in server components:

import { getUserSession } from 'auth0-client-sk/Integrations'

const ServerComponent = async () => {
  const session = await getUserSession();

  if (!session) {
    return <div>Not authenticated</div>;
  }

  return (
    <div>
      <h1>Server-side protected content</h1>
      <p>User ID: {session.user.sub}</p>
    </div>
  );
};

Session Properties

The session object provides the following properties:

PropertyTypeDescription
isAuthenticatedbooleanAuthentication status
isPendingbooleanLoading state indicator
userobjectUser profile information

User Object Properties

User : {
    id: string;
    email: string | null;
    name: string;
    avatar: string;
    username: string;
    provider: string;
}
1.9.23

5 months ago

1.9.22

5 months ago

1.9.21

5 months ago

1.9.19

5 months ago

1.9.18

5 months ago

1.9.17

5 months ago

1.9.16

5 months ago

1.9.15

5 months ago

1.9.14

5 months ago

1.9.13

5 months ago

1.9.12

5 months ago

1.9.11

5 months ago

1.7.4

5 months ago

1.9.1

5 months ago

1.9.0

5 months ago

1.9.31

5 months ago

1.8.2

5 months ago

1.8.1

5 months ago

1.8.0

5 months ago

1.9.9

5 months ago

1.9.8

5 months ago

1.9.7

5 months ago

1.9.6

5 months ago

1.9.5

5 months ago

1.9.4

5 months ago

1.9.3

5 months ago

1.9.2

5 months ago

1.7.3

5 months ago

1.7.41

5 months ago

1.8.11

5 months ago

1.8.12

5 months ago

1.8.13

5 months ago

1.8.14

5 months ago

1.8.15

5 months ago

1.8.16

5 months ago

1.8.17

5 months ago

1.8.18

5 months ago

1.8.19

5 months ago

1.7.2

5 months ago

1.7.11

5 months ago

1.7.1

5 months ago

1.7.0

5 months ago

1.6.9

5 months ago

1.6.8

5 months ago

1.6.7

5 months ago

1.6.6

5 months ago

1.6.5

5 months ago

1.6.4

5 months ago

1.6.3

5 months ago

1.6.2

5 months ago

1.6.1

5 months ago

1.6.0

5 months ago

1.5.9

5 months ago

1.5.8

5 months ago

1.5.7

5 months ago

1.5.6

5 months ago

1.5.5

5 months ago

1.5.4

5 months ago

1.5.2

5 months ago

1.5.1

5 months ago

1.5.0

5 months ago

1.4.6

5 months ago

1.4.5

5 months ago

1.4.4

5 months ago

1.4.3

5 months ago

1.4.2

5 months ago

1.4.1

5 months ago

1.4.0

5 months ago

1.3.4

5 months ago

1.3.34

5 months ago

1.3.33

5 months ago

1.3.32

5 months ago

1.3.31

5 months ago

1.3.3

5 months ago

1.3.24

5 months ago

1.3.23

5 months ago

1.3.22

5 months ago

1.3.21

5 months ago

1.3.2

5 months ago

1.3.1

5 months ago

1.3.0

5 months ago

1.2.9

5 months ago

1.2.8

5 months ago

1.2.7

5 months ago

1.2.61

5 months ago

1.2.6

5 months ago

1.2.51

5 months ago

1.2.5

5 months ago

1.2.4

5 months ago

1.2.3

5 months ago

1.2.2

5 months ago

1.2.0

5 months ago

1.1.9

5 months ago

1.1.8

5 months ago

1.1.7

5 months ago

1.1.6

5 months ago

1.1.5

5 months ago

1.1.44

5 months ago

1.1.43

5 months ago

1.1.42

5 months ago

1.1.41

5 months ago

1.1.4

5 months ago

1.1.3

5 months ago

1.1.2

5 months ago

1.1.1

5 months ago

1.1.0

5 months ago

1.0.4

5 months ago

1.0.3

5 months ago

1.0.2

5 months ago

1.0.1

5 months ago

1.0.0

5 months ago