Skip to content

Commit

Permalink
Update docs (#269)
Browse files Browse the repository at this point in the history
  • Loading branch information
Widcket committed Feb 2, 2021
1 parent a2932dc commit 58d6897
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 28 deletions.
23 changes: 12 additions & 11 deletions EXAMPLES.md
@@ -1,10 +1,10 @@
# Examples

- [Basic Setup](#basic-setup)
- [Customise handlers behaviour](#customise-handlers-behaviour)
- [Customize handlers behavior](#customize-handlers-behavior)
- [Use custom auth urls](#use-custom-auth-urls)
- [Protecting a Server Side Rendered (SSR) Page](#protecting-a-server-side-rendered-ssr-page)
- [Protecting a Client Side Rendered (CSR) Page](#protecting-a-client-side-rendered-csr-page)
- [Protecting a Server-Side Rendered (SSR) Page](#protecting-a-server-side-rendered-ssr-page)
- [Protecting a Client-Side Rendered (CSR) Page](#protecting-a-client-side-rendered-csr-page)
- [Protect an API Route](#protect-an-api-route)
- [Access an External API from an API Route](#access-an-external-api-from-an-api-route)
- [Create your own instance of the SDK](#create-your-own-instance-of-the-sdk)
Expand Down Expand Up @@ -41,7 +41,7 @@ import React from 'react';
import { UserProvider } from '@auth0/nextjs-auth0';

export default function App({ Component, pageProps }) {
// You can optionally pass the `user` prop from pages that require server side
// You can optionally pass the `user` prop from pages that require server-side
// rendering to prepopulate the `useUser` hook.
const { user } = pageProps;

Expand Down Expand Up @@ -78,7 +78,7 @@ export default () => {

Have a look at the `basic-example` app [./examples/basic-example](./examples/basic-example).

## Customise handlers behaviour
## Customize handlers behavior

Pass custom parameters to the auth handlers or add your own logging and error handling.

Expand Down Expand Up @@ -132,9 +132,9 @@ export default async function login(req, res) {
export default () => <a href="/api/custom-login">Login</a>;
```

> Note: you will need to specify this custom login URL when calling `withPageAuthRequired` both the [front end version](https://auth0.github.io/nextjs-auth0/interfaces/frontend_with_page_auth_required.withpageauthrequiredoptions.html#loginurl) and [server side version](https://auth0.github.io/nextjs-auth0/modules/helpers_with_page_auth_required.html#withpageauthrequiredoptions)
> Note: If you customize the login url you will need to set the environment variable `NEXT_PUBLIC_AUTH0_LOGIN` to this custom value for `withPageAuthRequired` to work correctly. And if you customize the profile url, you will need to set the `NEXT_PUBLIC_AUTH0_PROFILE` environment variable to this custom value for the `useUser` hook to work properly.
## Protecting a Server Side Rendered (SSR) Page
## Protecting a Server-Side Rendered (SSR) Page

Requests to `/pages/profile` without a valid session cookie will be redirected to the login page.

Expand All @@ -153,7 +153,7 @@ export const getServerSideProps = withPageAuthRequired();

See a running example of a [SSR protected page](./examples/kitchen-sink-example/pages/profile-ssr.tsx) in the kitchen-sink example app.

## Protecting a Client Side Rendered (CSR) Page
## Protecting a Client-Side Rendered (CSR) Page

Requests to `/pages/profile` without a valid session cookie will be redirected to the login page.

Expand All @@ -177,7 +177,8 @@ Requests to `/pages/api/protected` without a valid session cookie will fail with
import { withApiAuthRequired } from '@auth0/nextjs-auth0';

export default withApiAuthRequired(async function myApiRoute(req, res) {
res.json({ protected: 'My Secret' });
const { user } = getSession(req, res);
res.json({ protected: 'My Secret', id: user.sub });
});
```

Expand Down Expand Up @@ -272,7 +273,7 @@ import {
However, there are various reasons why you might want to create and manage an instance of the SDK yourself:

- You may want to create your own instance for testing
- You may not want to use environment variables for configuration of secrets (eg using CredStash or AWS's Key Management Service)
- You may not want to use environment variables for the configuration of secrets (eg using CredStash or AWS's Key Management Service)

In this case you can use the [initAuth0](https://auth0.github.io/nextjs-auth0/modules/instance.html) method to create an instance, eg:

Expand Down Expand Up @@ -300,7 +301,7 @@ export default auth0.handleAuth();
```

> Note: You should not use the instance methods in combination with the named exports,
> otherwise you will be creating multiple instances of the sdk, eg:
> otherwise you will be creating multiple instances of the SDK, eg:
```js
// DON'T Mix instance methods and named exports
Expand Down
12 changes: 6 additions & 6 deletions README.md
Expand Up @@ -168,16 +168,16 @@ directly in the browser and use them to access external APIs directly.
You should be aware of the security implications of both, but if you are:

- Using [Static HTML Export](https://nextjs.org/docs/advanced-features/static-html-export)
- You do not need to access user data during Server Side Rendering
- You do not need to access user data during server-side rendering
- You want to get the Access Token and call external API's directly from the frontend rather than using Next.js API Routes as a proxy to call external APIs

Then [auth0-react](https://github.com/auth0/auth0-react) may be more suitable for your needs.

### Testing

By default the SDK creates and manages a singleton instance to run for the lifetime of the application. When
testing your application you may need to reset this instance, so it's state does not leak between tests.
If you're using Jest, we recommend using `jest.resetModules()` after each test. Alternatively you can look at
By default, the SDK creates and manages a singleton instance to run for the lifetime of the application. When
testing your application you may need to reset this instance, so its state does not leak between tests.
If you're using Jest, we recommend using `jest.resetModules()` after each test. Alternatively, you can look at
[creating your own instance of the SDK](./EXAMPLES.md#create-your-own-instance-of-the-sdk) so it can be recreated between tests.

## Contributing
Expand All @@ -193,15 +193,15 @@ Run NPM install first to install the dependencies of this project:
npm install
```

In order to build a release you can run the following commands and the output will be stored in the `dist` folder:
In order to build a release, you can run the following commands and the output will be stored in the `dist` folder:

```sh
npm run clean
npm run lint
npm run build
```

Additionally you can also run tests:
Additionally, you can also run tests:

```sh
npm run build:test # Build the Next.js test app
Expand Down
2 changes: 1 addition & 1 deletion V1_MIGRATION_GUIDE.md
Expand Up @@ -112,7 +112,7 @@ See the [getSession docs](https://auth0.github.io/nextjs-auth0/modules/session_g

### getAccessToken

`tokenCache` has been removed in favour of a single `getAccessToken` method.
`tokenCache` has been removed in favor of a single `getAccessToken` method.

### Before

Expand Down
2 changes: 1 addition & 1 deletion cypress/integration/smoke.test.ts
Expand Up @@ -26,7 +26,7 @@ describe('smoke tests', () => {
cy.get('[data-testid=profile]').contains(EMAIL);
});

it('should protect a server-side-rendered page', () => {
it('should protect a server-side rendered page', () => {
cy.visit('/profile-ssr');
cy.url().should('eq', `${Cypress.config().baseUrl}/profile-ssr`);
cy.get('[data-testid=profile]').contains(EMAIL);
Expand Down
14 changes: 7 additions & 7 deletions src/config.ts
Expand Up @@ -386,6 +386,13 @@ const bool = (param?: any, defaultValue?: boolean): boolean | undefined => {
*/
const num = (param?: string): number | undefined => (param === undefined || param === '' ? undefined : +param);

/**
* @ignore
*/
export const getLoginUrl = (): string => {
return process.env.NEXT_PUBLIC_AUTH0_LOGIN || '/api/auth/login';
};

/**
* @ignore
*/
Expand Down Expand Up @@ -477,10 +484,3 @@ export const getConfig = (params?: ConfigParameters): { baseConfig: BaseConfig;

return { baseConfig, nextConfig };
};

/**
* @ignore
*/
export const getLoginUrl = (): string => {
return process.env.NEXT_PUBLIC_AUTH0_LOGIN || '/api/auth/login';
};
2 changes: 1 addition & 1 deletion src/handlers/auth.ts
Expand Up @@ -5,7 +5,7 @@ import { HandleProfile } from './profile';
import { NextApiHandler, NextApiRequest, NextApiResponse } from 'next';

/**
* If you want to add some custom behaviour to the default auth handlers, you can pass in custom handlers for
* If you want to add some custom behavior to the default auth handlers, you can pass in custom handlers for
* `login`, `logout`, `callback` and `profile` eg
*
* ```js
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/callback.ts
Expand Up @@ -64,7 +64,7 @@ export type AfterCallback = (
) => Promise<Session> | Session;

/**
* Options to customise the callback handler.
* Options to customize the callback handler.
*
* @category Server
*/
Expand Down

0 comments on commit 58d6897

Please sign in to comment.