1.9.31 • Published 6 months ago

auth0-client-sk v1.9.31

Weekly downloads
-
License
ISC
Repository
-
Last release
6 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

6 months ago

1.9.22

6 months ago

1.9.21

6 months ago

1.9.19

6 months ago

1.9.18

6 months ago

1.9.17

6 months ago

1.9.16

6 months ago

1.9.15

6 months ago

1.9.14

6 months ago

1.9.13

6 months ago

1.9.12

6 months ago

1.9.11

6 months ago

1.7.4

6 months ago

1.9.1

6 months ago

1.9.0

6 months ago

1.9.31

6 months ago

1.8.2

6 months ago

1.8.1

6 months ago

1.8.0

6 months ago

1.9.9

6 months ago

1.9.8

6 months ago

1.9.7

6 months ago

1.9.6

6 months ago

1.9.5

6 months ago

1.9.4

6 months ago

1.9.3

6 months ago

1.9.2

6 months ago

1.7.3

6 months ago

1.7.41

6 months ago

1.8.11

6 months ago

1.8.12

6 months ago

1.8.13

6 months ago

1.8.14

6 months ago

1.8.15

6 months ago

1.8.16

6 months ago

1.8.17

6 months ago

1.8.18

6 months ago

1.8.19

6 months ago

1.7.2

6 months ago

1.7.11

6 months ago

1.7.1

6 months ago

1.7.0

6 months ago

1.6.9

6 months ago

1.6.8

6 months ago

1.6.7

6 months ago

1.6.6

6 months ago

1.6.5

6 months ago

1.6.4

6 months ago

1.6.3

6 months ago

1.6.2

6 months ago

1.6.1

6 months ago

1.6.0

6 months ago

1.5.9

6 months ago

1.5.8

6 months ago

1.5.7

6 months ago

1.5.6

6 months ago

1.5.5

6 months ago

1.5.4

6 months ago

1.5.2

6 months ago

1.5.1

6 months ago

1.5.0

6 months ago

1.4.6

6 months ago

1.4.5

6 months ago

1.4.4

6 months ago

1.4.3

6 months ago

1.4.2

6 months ago

1.4.1

6 months ago

1.4.0

6 months ago

1.3.4

6 months ago

1.3.34

6 months ago

1.3.33

6 months ago

1.3.32

6 months ago

1.3.31

6 months ago

1.3.3

6 months ago

1.3.24

6 months ago

1.3.23

6 months ago

1.3.22

6 months ago

1.3.21

6 months ago

1.3.2

6 months ago

1.3.1

6 months ago

1.3.0

6 months ago

1.2.9

6 months ago

1.2.8

6 months ago

1.2.7

6 months ago

1.2.61

6 months ago

1.2.6

6 months ago

1.2.51

6 months ago

1.2.5

6 months ago

1.2.4

6 months ago

1.2.3

6 months ago

1.2.2

6 months ago

1.2.0

6 months ago

1.1.9

6 months ago

1.1.8

6 months ago

1.1.7

6 months ago

1.1.6

6 months ago

1.1.5

6 months ago

1.1.44

6 months ago

1.1.43

6 months ago

1.1.42

6 months ago

1.1.41

6 months ago

1.1.4

6 months ago

1.1.3

6 months ago

1.1.2

6 months ago

1.1.1

6 months ago

1.1.0

6 months ago

1.0.4

6 months ago

1.0.3

6 months ago

1.0.2

6 months ago

1.0.1

6 months ago

1.0.0

6 months ago