Skip to content

Commit

Permalink
feat/example (#1)
Browse files Browse the repository at this point in the history
* feat: added new example

* docs: added new sections in readme
  • Loading branch information
fedeya authored Apr 25, 2021
1 parent ea07673 commit 228939d
Show file tree
Hide file tree
Showing 33 changed files with 11,820 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
dist
dist
.env
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,24 @@

- Saving users and account in Sanity
- Retrieving of full linked provider information for a user
- Cache User
- Stale While Revalidate

### Database sessions

Database sessions are not implemented, this adapter relies on usage of JSON Web Tokens for stateless session management.

### Recommendations

you must make the dataset private otherwise the data of your users will be public for everyone

```sh
sanity dataset visibility set <datasetName> private
```

## Requirements

- Sanity Token for Read+Write

## Installation

### yarn
Expand All @@ -38,6 +50,8 @@ npm i next-auth-sanity

## Usage

[Full Example](https://github.com/Fedeya/next-auth-sanity/tree/main/examples/full-example)

```ts
import NextAuth, { NextAuthOptions } from 'next-auth';
import Providers from 'next-auth/providers';
Expand Down
5 changes: 5 additions & 0 deletions examples/full-example/.env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SANITY_DATASET=""
SANITY_PROJECT_ID=""
SANITY_API_TOKEN=""
GITHUB_CLIENT_ID=""
GITHUB_CLIENT_SECRET=""
34 changes: 34 additions & 0 deletions examples/full-example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env.local
.env.development.local
.env.test.local
.env.production.local

# vercel
.vercel
34 changes: 34 additions & 0 deletions examples/full-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).

## Getting Started

First, run the development server:

```bash
npm run dev
# or
yarn dev
```

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.

You can start editing the page by modifying `pages/index.js`. The page auto-updates as you edit the file.

[API routes](https://nextjs.org/docs/api-routes/introduction) can be accessed on [http://localhost:3000/api/hello](http://localhost:3000/api/hello). This endpoint can be edited in `pages/api/hello.js`.

The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/api-routes/introduction) instead of React pages.

## Learn More

To learn more about Next.js, take a look at the following resources:

- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.

You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!

## Deploy on Vercel

The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.

Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
4 changes: 4 additions & 0 deletions examples/full-example/lerna.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"packages": ["studio"],
"version": "0.0.0"
}
2 changes: 2 additions & 0 deletions examples/full-example/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// <reference types="next" />
/// <reference types="next/types/global" />
23 changes: 23 additions & 0 deletions examples/full-example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "full-example",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"@sanity/client": "^2.8.0",
"next": "10.1.3",
"next-auth": "^3.17.2",
"react": "17.0.2",
"react-dom": "17.0.2"
},
"devDependencies": {
"@types/node": "^14.14.41",
"@types/react": "^17.0.3",
"lerna": "^3.22.1",
"typescript": "^4.2.4"
}
}
Binary file added examples/full-example/public/favicon.ico
Binary file not shown.
4 changes: 4 additions & 0 deletions examples/full-example/public/vercel.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions examples/full-example/src/libs/sanity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import createClient from '@sanity/client';

export const client = createClient({
projectId: process.env.SANITY_PROJECT_ID,
dataset: process.env.SANITY_DATASET,
token: process.env.SANITY_API_TOKEN,
useCdn: process.env.NODE_ENV === 'production'
});
11 changes: 11 additions & 0 deletions examples/full-example/src/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { FC } from 'react';
import { AppProps } from 'next/app';
import { Provider } from 'next-auth/client';

const MyApp: FC<AppProps> = ({ Component, pageProps }) => (
<Provider session={pageProps.session}>
<Component {...pageProps} />
</Provider>
);

export default MyApp;
21 changes: 21 additions & 0 deletions examples/full-example/src/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import NextAuth, { NextAuthOptions } from 'next-auth';
import Providers from 'next-auth/providers';
import { NextApiRequest, NextApiResponse } from 'next';
import { SanityAdapter } from 'next-auth-sanity';
import { client } from '../../../libs/sanity';

const options: NextAuthOptions = {
providers: [
Providers.GitHub({
clientId: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET
})
],
session: {
jwt: true
},
adapter: SanityAdapter({ client })
};

export default (req: NextApiRequest, res: NextApiResponse) =>
NextAuth(req, res, options);
21 changes: 21 additions & 0 deletions examples/full-example/src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { FC } from 'react';
import { useSession, signIn, signOut } from 'next-auth/client';

const Home: FC = () => {
const [session, loading] = useSession();

if (loading) return <p>Loading...</p>;

if (session) {
return (
<div>
<p>User: {session.user.name}</p>
<button onClick={() => signOut({ redirect: false })}>Sign Out</button>
</div>
);
}

return <button onClick={() => signIn('github')}>Sign In</button>;
};

export default Home;
9 changes: 9 additions & 0 deletions examples/full-example/studio/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Sanity Clean Content Studio

Congratulations, you have now installed the Sanity Content Studio, an open source real-time content editing environment connected to the Sanity backend.

Now you can do the following things:

- [Read “getting started” in the docs](https://www.sanity.io/docs/introduction/getting-started?utm_source=readme)
- [Join the community Slack](https://slack.sanity.io/?utm_source=readme)
- [Extend and build plugins](https://www.sanity.io/docs/content-studio/extending?utm_source=readme)
7 changes: 7 additions & 0 deletions examples/full-example/studio/config/.checksums
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"#": "Used by Sanity to keep track of configuration file checksums, do not delete or modify!",
"@sanity/default-layout": "bb034f391ba508a6ca8cd971967cbedeb131c4d19b17b28a0895f32db5d568ea",
"@sanity/default-login": "6fb6d3800aa71346e1b84d95bbcaa287879456f2922372bb0294e30b968cd37f",
"@sanity/form-builder": "b38478227ba5e22c91981da4b53436df22e48ff25238a55a973ed620be5068aa",
"@sanity/data-aspects": "d199e2c199b3e26cd28b68dc84d7fc01c9186bf5089580f2e2446994d36b3cb6"
}
3 changes: 3 additions & 0 deletions examples/full-example/studio/config/@sanity/data-aspects.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"listOptions": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"toolSwitcher": {
"order": [],
"hidden": []
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"providers": {
"mode": "append",
"redirectOnSingle": false,
"entries": []
}
}
5 changes: 5 additions & 0 deletions examples/full-example/studio/config/@sanity/form-builder.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"images": {
"directUploads": true
}
}
29 changes: 29 additions & 0 deletions examples/full-example/studio/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "nextauthsanity",
"private": true,
"version": "1.0.0",
"description": "",
"main": "package.json",
"author": "Federico Minaya <[email protected]>",
"license": "UNLICENSED",
"scripts": {
"start": "sanity start",
"build": "sanity build"
},
"keywords": [
"sanity"
],
"dependencies": {
"@sanity/base": "^2.8.1",
"@sanity/components": "^2.2.6",
"@sanity/core": "^2.8.0",
"@sanity/default-layout": "^2.8.1",
"@sanity/default-login": "^2.8.0",
"@sanity/desk-tool": "^2.8.1",
"@sanity/vision": "^2.8.0",
"prop-types": "^15.7",
"react": "^17.0",
"react-dom": "^17.0"
},
"devDependencies": {}
}
1 change: 1 addition & 0 deletions examples/full-example/studio/plugins/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
User-specific packages can be placed here
28 changes: 28 additions & 0 deletions examples/full-example/studio/sanity.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"root": true,
"project": {
"name": "next-auth-sanity"
},
"api": {
"projectId": "placeholder",
"dataset": "placeholder"
},
"plugins": [
"@sanity/base",
"@sanity/components",
"@sanity/default-layout",
"@sanity/default-login",
"@sanity/desk-tool"
],
"env": {
"development": {
"plugins": ["@sanity/vision"]
}
},
"parts": [
{
"name": "part:@sanity/base/schema",
"path": "./schemas/schema"
}
]
}
39 changes: 39 additions & 0 deletions examples/full-example/studio/schemas/account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Document } from './schemaTypes';

export default <Document>{
name: 'account',
title: 'Account',
type: 'document',
fields: [
{
name: 'providerType',
type: 'string'
},
{
name: 'providerId',
type: 'string'
},
{
name: 'providerAccountId',
type: 'string'
},
{
name: 'refreshToken',
type: 'string'
},
{
name: 'accessToken',
type: 'string'
},
{
name: 'accessTokenExpires',
type: 'string'
},
{
name: 'user',
title: 'User',
type: 'reference',
to: { type: 'user' }
}
]
};
20 changes: 20 additions & 0 deletions examples/full-example/studio/schemas/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// First, we must import the schema creator
import createSchema from 'part:@sanity/base/schema-creator';

// Then import schema types from any plugins that might expose them
import schemaTypes from 'all:part:@sanity/base/schema-type';
import user from './user';
import account from './account';

// Then we give our schema to the builder and provide the result to Sanity
export default createSchema({
// We name our schema
name: 'default',
// Then proceed to concatenate our document type
// to the ones provided by any plugins that are installed
types: schemaTypes.concat([
/* Your types here! */
user,
account
])
});
Loading

0 comments on commit 228939d

Please sign in to comment.