Skip to content

Commit

Permalink
feat: support for next auth v4 (#8)
Browse files Browse the repository at this point in the history
* feat: support for next auth v4

* fix dataset

* feat: add docs and some fixes for credentials

* npm ignore
  • Loading branch information
fedeya authored Apr 16, 2022
1 parent 74659dc commit f3ce44e
Show file tree
Hide file tree
Showing 17 changed files with 398 additions and 1,257 deletions.
15 changes: 15 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.*.swp
._*
.DS_Store
.git
.gitignore
.hg
.npmignore
.npmrc
.lock-wscript
.svn
.wafpickle-*
config.gypi
CVS
npm-debug.log
/examples
41 changes: 30 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,26 @@ npm i next-auth-sanity

```ts
import NextAuth, { NextAuthOptions } from 'next-auth';
import Providers from 'next-auth/providers';
import GitHub from 'next-auth/providers/github';
import { NextApiRequest, NextApiResponse } from 'next';
import { SanityAdapter, SanityCredentials } from 'next-auth-sanity';
import { client } from 'your/sanity/client';

const options: NextAuthOptions = {
providers: [
Providers.GitHub({
GitHub({
clientId: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET
}),
SanityCredentials(client) // only if you use sign in with credentials
],
session: {
jwt: true
strategy: 'jwt'
},
adapter: SanityAdapter(client)
};

export default (req: NextApiRequest, res: NextApiResponse) =>
NextAuth(req, res, options);
export default NextAuth(options);
```

### Sanity Schemas
Expand All @@ -83,11 +82,11 @@ you can install this package in your studio project and use the schemas like thi
import createSchema from 'part:@sanity/base/schema-creator';

import schemaTypes from 'all:part:@sanity/base/schema-type';
import { user, account, verificationRequest } from 'next-auth-sanity/schemas';
import { user, account, verificationToken } from 'next-auth-sanity/schemas';

export default createSchema({
name: 'default',
types: schemaTypes.concat([user, account, verificationRequest])
types: schemaTypes.concat([user, account, verificationToken])
});
```

Expand Down Expand Up @@ -169,11 +168,11 @@ export default {
```

```ts
// verification-request - only if you use email provider
// verification-token - only if you use email provider

export default {
name: 'verification-request',
title: 'Verification Request',
name: 'verification-token',
title: 'Verification Token',
type: 'document',
fields: [
{
Expand Down Expand Up @@ -221,13 +220,33 @@ const user = await signUp({
name
});

await signIn('credentials', {
await signIn('sanity-login', {
redirect: false,
email,
password
});
```

## Custom Schemas
if you want to use another schema or upgrade from previous version you can change the default schema used in the library, to do so you can pass a second argument to all methods with config
```ts
SanityAdapter(client, {
schemas: {
verificationToken: 'verification-request',
account: 'account',
user: 'profile'
}
})


// the second argument is the name of the user schema
// default: 'user'

SanityCredentials(client, 'profile');

signUpHandler(client, 'profile');
```

## Author

👤 **Fedeya <[email protected]>**
Expand Down
2 changes: 1 addition & 1 deletion examples/full-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"dependencies": {
"@sanity/client": "^2.8.0",
"next": "10.1.3",
"next-auth": "^3.27.1",
"next-auth": "^4.3.2",
"react": "17.0.2",
"react-dom": "17.0.2"
},
Expand Down
6 changes: 3 additions & 3 deletions examples/full-example/src/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { FC } from 'react';
import { AppProps } from 'next/app';
import { Provider } from 'next-auth/client';
import { SessionProvider } from 'next-auth/react';

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

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

const options: NextAuthOptions = {
providers: [
Providers.GitHub({
GitHub({
clientId: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET
}),
SanityCredentials(client)
],
session: {
jwt: true
strategy: 'jwt'
},
adapter: SanityAdapter(client)
secret: 'any-secret-word',
adapter: SanityAdapter(client, {
schemas: {
verificationToken: 'verificationToken',
account: 'account',
user: 'user'
}
})
};

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

const Credentials: FC = () => {
const [email, setEmail] = useState('');
const [session, loading] = useSession();
const { data, status } = useSession();
const [password, setPassword] = useState('');
const [name, setName] = useState('');

Expand All @@ -17,29 +17,32 @@ const Credentials: FC = () => {
name
});

await signIn('credentials', {
await signIn('sanity-login', {
redirect: false,
email,
password
email
});

console.log(user);
};

const handleSubmitSignIn = async (e: React.FormEvent) => {
e.preventDefault();
await signIn('credentials', {

await signIn('sanity-login', {
redirect: false,
email,
password
});
};

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

return (
<div>
<p>User: {session?.user.name}</p>
{data && (
<div>
<p>User: {data?.user.name}</p>
<button onClick={() => signOut({ redirect: false })}>Sign Out</button>
</div>
)}
<h1>Sign Up</h1>
<form onSubmit={handleSubmit}>
<input
Expand Down
10 changes: 5 additions & 5 deletions examples/full-example/src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { FC } from 'react';
import { useSession, signIn, signOut } from 'next-auth/client';
import { useSession, signIn, signOut } from 'next-auth/react';

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

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

if (session) {
if (data) {
return (
<div>
<p>User: {session.user.name}</p>
<p>User: {data.user.name}</p>
<button onClick={() => signOut({ redirect: false })}>Sign Out</button>
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion examples/full-example/studio/sanity.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
},
"api": {
"projectId": "placeholder",
"dataset": "placeholder"
"dataset": "production"
},
"plugins": [
"@sanity/base",
Expand Down
4 changes: 2 additions & 2 deletions examples/full-example/studio/schemas/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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, account, verificationRequest } from '../../../../dist/schemas';
import { user, account, verificationToken } from '../../../../dist/schemas';

// Then we give our schema to the builder and provide the result to Sanity
export default createSchema({
Expand All @@ -15,6 +15,6 @@ export default createSchema({
/* Your types here! */
user,
account,
verificationRequest
verificationToken
])
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Document } from './schemaTypes';

export default <Document>{
name: 'verification-request',
title: 'Verification Request',
name: 'verification-token',
title: 'Verification Token',
type: 'document',
fields: [
{
Expand Down
Loading

0 comments on commit f3ce44e

Please sign in to comment.