|
| 1 | +# Magic Example |
| 2 | + |
| 3 | +This example shows how to use [Magic](https://magic.link) with Next.js. The example features cookie-based, passwordless authentication with email-based magic links. |
| 4 | + |
| 5 | +The example shows how to do a login and logout; and to get the user info using a hook with [SWR](https://swr.now.sh). |
| 6 | + |
| 7 | +A DB is not included. But you can add any DB you like!. |
| 8 | + |
| 9 | +The login cookie is `httpOnly`, meaning it can only be accessed by the API, and it's encrypted using [@hapi/iron](https://hapi.dev/family/iron) for more security. |
| 10 | + |
| 11 | +## Deploy your own |
| 12 | + |
| 13 | +Once you have access to [the environment variables you'll need](#configuration), deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example): |
| 14 | + |
| 15 | +[](https://vercel.com/import/git?c=1&s=https://github.com/vercel/next.js/tree/canary/examples/with-magic&env=NEXT_PUBLIC_MAGIC_PUBLISHABLE_KEY,MAGIC_SECRET_KEY,TOKEN_SECRET&envDescription=Required%20to%20connect%20the%20app%20with%20Magic&envLink=https://github.com/vercel/next.js/tree/canary/examples/with-magic%23configuration) |
| 16 | + |
| 17 | +## How to use |
| 18 | + |
| 19 | +Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example: |
| 20 | + |
| 21 | +```bash |
| 22 | +npx create-next-app --example with-magic with-magic-app |
| 23 | +# or |
| 24 | +yarn create next-app --example with-magic with-magic-app |
| 25 | +``` |
| 26 | + |
| 27 | +## Configuration |
| 28 | + |
| 29 | +Login to the [Magic Dashboard](https://dashboard.magic.link/) and get the keys of your application |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +Next, copy the `.env.local.example` file in this directory to .env.local (which will be ignored by Git): |
| 34 | + |
| 35 | +```bash |
| 36 | +cp .env.local.example .env.local |
| 37 | +``` |
| 38 | + |
| 39 | +Then set each variable on `.env.local`: |
| 40 | + |
| 41 | +- `NEXT_PUBLIC_MAGIC_PUBLISHABLE_KEY` should look like `pk_test_abc` or `pk_live_ABC` |
| 42 | +- `MAGIC_SECRET_KEY` should look like `sk_test_ABC` or `sk_live_ABC` |
| 43 | +- `TOKEN_SECRET` should be a string with at least 32 characters |
| 44 | + |
| 45 | +Now, run Next.js in development mode |
| 46 | + |
| 47 | +```bash |
| 48 | +npm run dev |
| 49 | +# or |
| 50 | +yarn dev |
| 51 | +``` |
| 52 | + |
| 53 | +Your app should be up and running on [http://localhost:3000](http://localhost:3000)! If it doesn't work, post on [GitHub discussions](https://github.com/vercel/next.js/discussions). |
| 54 | + |
| 55 | +## Deploy on Vercel |
| 56 | + |
| 57 | +You can deploy this app to the cloud with [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)). |
| 58 | + |
| 59 | +#### Deploy Your Local Project |
| 60 | + |
| 61 | +To deploy your local project to Vercel, push it to GitHub/GitLab/Bitbucket and [import to Vercel](https://vercel.com/import/git?utm_source=github&utm_medium=readme&utm_campaign=next-example). |
| 62 | + |
| 63 | +**Important**: When you import your project on Vercel, make sure to click on **Environment Variables** and set them to match your `.env.local` file. |
| 64 | + |
| 65 | +#### Deploy from Our Template |
| 66 | + |
| 67 | +Alternatively, you can deploy using our template by clicking on the Deploy button below. |
| 68 | + |
| 69 | +[](https://vercel.com/import/git?c=1&s=https://github.com/vercel/next.js/tree/canary/examples/with-magic&env=NEXT_PUBLIC_MAGIC_PUBLISHABLE_KEY,MAGIC_SECRET_KEY,TOKEN_SECRET&envDescription=Required%20to%20connect%20the%20app%20with%20Magic&envLink=https://github.com/vercel/next.js/tree/canary/examples/with-magic%23configuration) |
0 commit comments