1.9.31 • Published 11 months ago

auth0-client-sk v1.9.31

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

11 months ago

1.9.22

11 months ago

1.9.21

11 months ago

1.9.19

11 months ago

1.9.18

11 months ago

1.9.17

11 months ago

1.9.16

11 months ago

1.9.15

11 months ago

1.9.14

11 months ago

1.9.13

11 months ago

1.9.12

11 months ago

1.9.11

11 months ago

1.7.4

11 months ago

1.9.1

11 months ago

1.9.0

11 months ago

1.9.31

11 months ago

1.8.2

11 months ago

1.8.1

11 months ago

1.8.0

11 months ago

1.9.9

11 months ago

1.9.8

11 months ago

1.9.7

11 months ago

1.9.6

11 months ago

1.9.5

11 months ago

1.9.4

11 months ago

1.9.3

11 months ago

1.9.2

11 months ago

1.7.3

11 months ago

1.7.41

11 months ago

1.8.11

11 months ago

1.8.12

11 months ago

1.8.13

11 months ago

1.8.14

11 months ago

1.8.15

11 months ago

1.8.16

11 months ago

1.8.17

11 months ago

1.8.18

11 months ago

1.8.19

11 months ago

1.7.2

11 months ago

1.7.11

11 months ago

1.7.1

11 months ago

1.7.0

11 months ago

1.6.9

11 months ago

1.6.8

11 months ago

1.6.7

11 months ago

1.6.6

11 months ago

1.6.5

11 months ago

1.6.4

11 months ago

1.6.3

11 months ago

1.6.2

11 months ago

1.6.1

11 months ago

1.6.0

11 months ago

1.5.9

11 months ago

1.5.8

11 months ago

1.5.7

11 months ago

1.5.6

11 months ago

1.5.5

11 months ago

1.5.4

11 months ago

1.5.2

11 months ago

1.5.1

11 months ago

1.5.0

11 months ago

1.4.6

11 months ago

1.4.5

11 months ago

1.4.4

11 months ago

1.4.3

11 months ago

1.4.2

11 months ago

1.4.1

11 months ago

1.4.0

11 months ago

1.3.4

11 months ago

1.3.34

11 months ago

1.3.33

11 months ago

1.3.32

11 months ago

1.3.31

11 months ago

1.3.3

11 months ago

1.3.24

11 months ago

1.3.23

11 months ago

1.3.22

11 months ago

1.3.21

11 months ago

1.3.2

11 months ago

1.3.1

11 months ago

1.3.0

11 months ago

1.2.9

11 months ago

1.2.8

11 months ago

1.2.7

11 months ago

1.2.61

11 months ago

1.2.6

11 months ago

1.2.51

11 months ago

1.2.5

11 months ago

1.2.4

11 months ago

1.2.3

11 months ago

1.2.2

11 months ago

1.2.0

11 months ago

1.1.9

11 months ago

1.1.8

11 months ago

1.1.7

11 months ago

1.1.6

11 months ago

1.1.5

11 months ago

1.1.44

11 months ago

1.1.43

11 months ago

1.1.42

11 months ago

1.1.41

11 months ago

1.1.4

11 months ago

1.1.3

11 months ago

1.1.2

11 months ago

1.1.1

11 months ago

1.1.0

11 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.2

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago