Skip to content

Commit

Permalink
Replace usages of auth0 dynamic route
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjmcgrath committed May 16, 2023
1 parent ab50f4c commit cee36e1
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 28 deletions.
15 changes: 8 additions & 7 deletions EXAMPLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ AUTH0_CLIENT_ID='CLIENT_ID'
AUTH0_CLIENT_SECRET='CLIENT_SECRET'
```

Create a [dynamic API route handler](https://nextjs.org/docs/api-routes/dynamic-api-routes) at `/pages/api/auth/[...auth0].js`.
Create a [dynamic API route handler](https://nextjs.org/docs/api-routes/dynamic-api-routes) at `/pages/api/auth/[auth0].js`.

```js
import { handleAuth } from '@auth0/nextjs-auth0';
Expand Down Expand Up @@ -126,7 +126,7 @@ export default initAuth0({
Now rather than using the named exports, you can use the instance methods directly.

```js
// pages/api/auth/[...auth0].js
// pages/api/auth/[auth0].js
import auth0 from '../../../utils/auth0';

// Use the instance method
Expand All @@ -141,7 +141,8 @@ export default auth0.handleAuth();
import auth0 from '../../../utils/auth0';
import { handleLogin } from '@auth0/nextjs-auth0';

export default auth0.handleAuth({ // <= instance method
export default auth0.handleAuth({
// <= instance method
async login(req, res) {
try {
// `auth0.handleAuth` and `handleLogin` will be using separate instances
Expand All @@ -159,7 +160,7 @@ export default auth0.handleAuth({ // <= instance method
Pass custom parameters to the auth handlers or add your own logging and error handling.

```js
// pages/api/auth/[...auth0].js
// pages/api/auth/[auth0].js
import { handleAuth, handleLogin } from '@auth0/nextjs-auth0';
import { myCustomLogger, myCustomErrorReporter } from '../utils';

Expand Down Expand Up @@ -192,7 +193,7 @@ export default handleAuth({

## Use custom auth urls

Instead of (or in addition to) creating `/pages/api/auth/[...auth0].js` to handle all requests, you can create them individually at different urls.
Instead of (or in addition to) creating `/pages/api/auth/[auth0].js` to handle all requests, you can create them individually at different urls.

Eg for login:

Expand Down Expand Up @@ -352,7 +353,7 @@ export default auth0.withMiddlewareAuthRequired(async function middleware(req) {
Get an access token by providing your API's audience and scopes. You can pass them directly to the `handlelogin` method, or use environment variables instead.

```js
// pages/api/auth/[...auth0].js
// pages/api/auth/[auth0].js
import { handleAuth, handleLogin } from '@auth0/nextjs-auth0';

export default handleAuth({
Expand Down Expand Up @@ -404,7 +405,7 @@ Pass a custom authorize parameter to the login handler in a custom route.
If you are using the [New Universal Login Experience](https://auth0.com/docs/universal-login/new-experience) you can pass the `screen_hint` parameter.

```js
// pages/api/auth/[...auth0].js
// pages/api/auth/[auth0].js
import { handleAuth, handleLogin } from '@auth0/nextjs-auth0';

export default handleAuth({
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ Go to your Next.js application and create a [catch-all, dynamic API route handle

- Create an `auth` directory under the `/pages/api/` directory.

- Create a `[...auth0].js` file under the newly created `auth` directory.
- Create a `[auth0].js` file under the newly created `auth` directory.

The path to your dynamic API route file would be `/pages/api/auth/[...auth0].js`. Populate that file as follows:
The path to your dynamic API route file would be `/pages/api/auth/[auth0].js`. Populate that file as follows:

```js
import { handleAuth } from '@auth0/nextjs-auth0';
Expand Down
4 changes: 2 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ export interface NextConfig extends Pick<BaseConfig, 'identityClaimFilter'> {
* {@link WithApiAuthRequired}, and {@link WithPageAuthRequired}).
*
* ```js
* // pages/api/auth/[...auth0].js
* // pages/api/auth/[auth0].js
* import { handleAuth } from '@auth0/nextjs-auth0';
*
* return handleAuth();
Expand Down Expand Up @@ -435,7 +435,7 @@ export interface NextConfig extends Pick<BaseConfig, 'identityClaimFilter'> {
* Then import it into your route handler:
*
* ```js
* // pages/api/auth/[...auth0].js
* // pages/api/auth/[auth0].js
* import auth0 from '../../../../utils/auth0';
*
* return auth0.handleAuth();
Expand Down
10 changes: 5 additions & 5 deletions src/handlers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { HandlerError } from '../utils/errors';
* `login`, `logout`, `callback`, and `profile`. For example:
*
* ```js
* // pages/api/auth/[...auth0].js
* // pages/api/auth/[auth0].js
* import { handleAuth, handleLogin } from '@auth0/nextjs-auth0';
* import { errorReporter, logger } from '../../../utils';
*
Expand All @@ -33,7 +33,7 @@ import { HandlerError } from '../utils/errors';
* Alternatively, you can customize the default handlers without overriding them. For example:
*
* ```js
* // pages/api/auth/[...auth0].js
* // pages/api/auth/[auth0].js
* import { handleAuth, handleLogin } from '@auth0/nextjs-auth0';
*
* export default handleAuth({
Expand All @@ -46,7 +46,7 @@ import { HandlerError } from '../utils/errors';
* You can also create new handlers by customizing the default ones. For example:
*
* ```js
* // pages/api/auth/[...auth0].js
* // pages/api/auth/[auth0].js
* import { handleAuth, handleLogin } from '@auth0/nextjs-auth0';
*
* export default handleAuth({
Expand All @@ -72,10 +72,10 @@ type ErrorHandlers = {
* The main way to use the server SDK.
*
* Simply set the environment variables per {@link ConfigParameters} then create the file
* `pages/api/auth/[...auth0].js`. For example:
* `pages/api/auth/[auth0].js`. For example:
*
* ```js
* // pages/api/auth/[...auth0].js
* // pages/api/auth/[auth0].js
* import { handleAuth } from '@auth0/nextjs-auth0';
*
* export default handleAuth();
Expand Down
10 changes: 5 additions & 5 deletions src/handlers/callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { CallbackHandlerError, HandlerErrorCause } from '../utils/errors';
* @example Validate additional claims
*
* ```js
* // pages/api/auth/[...auth0].js
* // pages/api/auth/[auth0].js
* import { handleAuth, handleCallback } from '@auth0/nextjs-auth0';
*
* const afterCallback = (req, res, session, state) => {
Expand All @@ -39,7 +39,7 @@ import { CallbackHandlerError, HandlerErrorCause } from '../utils/errors';
* @example Modify the session after login
*
* ```js
* // pages/api/auth/[...auth0].js
* // pages/api/auth/[auth0].js
* import { handleAuth, handleCallback } from '@auth0/nextjs-auth0';
*
* const afterCallback = (req, res, session, state) => {
Expand All @@ -62,7 +62,7 @@ import { CallbackHandlerError, HandlerErrorCause } from '../utils/errors';
* @example Redirect successful login based on claim
*
* ```js
* // pages/api/auth/[...auth0].js
* // pages/api/auth/[auth0].js
* import { handleAuth, handleCallback } from '@auth0/nextjs-auth0';
*
* const afterCallback = (req, res, session, state) => {
Expand Down Expand Up @@ -138,7 +138,7 @@ export type CallbackOptionsProvider = (req: NextApiRequest) => CallbackOptions;
* @example Pass an options object
*
* ```js
* // pages/api/auth/[...auth0].js
* // pages/api/auth/[auth0].js
* import { handleAuth, handleCallback } from '@auth0/nextjs-auth0';
*
* export default handleAuth({
Expand All @@ -149,7 +149,7 @@ export type CallbackOptionsProvider = (req: NextApiRequest) => CallbackOptions;
* @example Pass a function that receives the request and returns an options object
*
* ```js
* // pages/api/auth/[...auth0].js
* // pages/api/auth/[auth0].js
* import { handleAuth, handleCallback } from '@auth0/nextjs-auth0';
*
* export default handleAuth({
Expand Down
6 changes: 3 additions & 3 deletions src/handlers/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { HandlerErrorCause, LoginHandlerError } from '../utils/errors';
* Use this to store additional state for the user before they visit the identity provider to log in.
*
* ```js
* // pages/api/auth/[...auth0].js
* // pages/api/auth/[auth0].js
* import { handleAuth, handleLogin } from '@auth0/nextjs-auth0';
*
* const getLoginState = (req, loginOptions) => {
Expand Down Expand Up @@ -173,7 +173,7 @@ export type LoginOptionsProvider = (req: NextApiRequest) => LoginOptions;
* @example Pass an options object
*
* ```js
* // pages/api/auth/[...auth0].js
* // pages/api/auth/[auth0].js
* import { handleAuth, handleLogin } from '@auth0/nextjs-auth0';
*
* export default handleAuth({
Expand All @@ -186,7 +186,7 @@ export type LoginOptionsProvider = (req: NextApiRequest) => LoginOptions;
* @example Pass a function that receives the request and returns an options object
*
* ```js
* // pages/api/auth/[...auth0].js
* // pages/api/auth/[auth0].js
* import { handleAuth, handleLogin } from '@auth0/nextjs-auth0';
*
* export default handleAuth({
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/logout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export type LogoutOptionsProvider = (req: NextApiRequest) => LogoutOptions;
* @example Pass an options object
*
* ```js
* // pages/api/auth/[...auth0].js
* // pages/api/auth/[auth0].js
* import { handleAuth, handleLogout } from '@auth0/nextjs-auth0';
*
* export default handleAuth({
Expand All @@ -50,7 +50,7 @@ export type LogoutOptionsProvider = (req: NextApiRequest) => LogoutOptions;
* @example Pass a function that receives the request and returns an options object
*
* ```js
* // pages/api/auth/[...auth0].js
* // pages/api/auth/[auth0].js
* import { handleAuth, handleLogout } from '@auth0/nextjs-auth0';
*
* export default handleAuth({
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export type ProfileOptionsProvider = (req: NextApiRequest) => ProfileOptions;
* @example Pass an options object
*
* ```js
* // pages/api/auth/[...auth0].js
* // pages/api/auth/[auth0].js
* import { handleAuth, handleProfile } from '@auth0/nextjs-auth0';
*
* export default handleAuth({
Expand All @@ -55,7 +55,7 @@ export type ProfileOptionsProvider = (req: NextApiRequest) => ProfileOptions;
* @example Pass a function that receives the request and returns an options object
*
* ```js
* // pages/api/auth/[...auth0].js
* // pages/api/auth/[auth0].js
* import { handleAuth, handleProfile } from '@auth0/nextjs-auth0';
*
* export default handleAuth({
Expand Down

0 comments on commit cee36e1

Please sign in to comment.