Skip to content

Commit

Permalink
feat: sign in with credentials (#2)
Browse files Browse the repository at this point in the history
* feat: sign in with credentials

* 1.2.0
  • Loading branch information
fedeya authored Apr 25, 2021
1 parent 946e15a commit ebb075c
Show file tree
Hide file tree
Showing 15 changed files with 716 additions and 192 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
dist
.env
.env
.next
57 changes: 52 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</a>
</p>

> NextAuth Adapter for Sanity
> NextAuth Adapter and Provider for Sanity
## Overview

Expand All @@ -17,6 +17,8 @@
- Saving users and account in Sanity
- Retrieving of full linked provider information for a user
- Stale While Revalidate
- Auth with Credentials
- Hash Credentials Passwords with Argon2

### Database sessions

Expand Down Expand Up @@ -56,13 +58,16 @@ npm i next-auth-sanity
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 '/your/sanity/client';
import { SanityAdapter, SanityCredentials } from 'next-auth-sanity';
import { client } from 'your/sanity/client';

const options: NextAuthOptions = {
providers: [
clientId: process.env.GITHUB_CLIENT_ID!,
clientSecret: process.env.GITHUB_CLIENT_SECRET!,
Providers.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
Expand Down Expand Up @@ -98,6 +103,12 @@ export default {
name: 'image',
title: 'Image',
type: 'url'
},
{
// this is only if you use credentials provider
name: 'password',
type: 'string',
hidden: true
}
]
};
Expand Down Expand Up @@ -145,6 +156,42 @@ export default {
};
```

### Sign Up and Sign In With Credentials

### Setup

`API Route`

```ts
// pages/api/sanity/signUp.ts

import { signUpHandler } from 'next-auth-sanity';
import { NextApiRequest, NextApiResponse } from 'next';
import { client } from 'your/sanity/client';

export default (req: NextApiRequest, res: NextApiResponse) =>
signUpHandler({ req, res, client });
```

`Client`

```ts
import { signUp } from 'next-auth-sanity/client';
import { signIn } from 'next-auth/client';

const user = await signUp({
email,
password,
name
});

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

## Author

👤 **Fedeya <[email protected]>**
Expand Down
5 changes: 3 additions & 2 deletions examples/full-example/src/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import NextAuth, { NextAuthOptions } from 'next-auth';
import Providers from 'next-auth/providers';
import { NextApiRequest, NextApiResponse } from 'next';
import { SanityAdapter } from 'next-auth-sanity';
import { SanityAdapter, SanityCredentials } from '../../../../../../dist';
import { client } from '../../../libs/sanity';

const options: NextAuthOptions = {
providers: [
Providers.GitHub({
clientId: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET
})
}),
SanityCredentials({ client })
],
session: {
jwt: true
Expand Down
6 changes: 6 additions & 0 deletions examples/full-example/src/pages/api/sanity/signUp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { signUpHandler } from '../../../../../../dist';
import { NextApiRequest, NextApiResponse } from 'next';
import { client } from '../../../libs/sanity';

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

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

const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();

const user = await signUp({
email,
password,
name
});

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

console.log(user);
};

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

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

return (
<div>
<p>User: {session?.user.name}</p>
<h1>Sign Up</h1>
<form onSubmit={handleSubmit}>
<input
type="email"
placeholder="Email"
value={email}
onChange={e => setEmail(e.target.value)}
/>
<input
type="password"
placeholder="Password"
value={password}
onChange={e => setPassword(e.target.value)}
/>
<input
type="name"
placeholder="Name"
value={name}
onChange={e => setName(e.target.value)}
/>
<button type="submit">Create Account</button>
</form>

<h1>Sign In</h1>
<form onSubmit={handleSubmitSignIn}>
<input
type="email"
value={email}
placeholder="Email"
onChange={e => setEmail(e.target.value)}
/>
<input
type="password"
placeholder="Password"
value={password}
onChange={e => setPassword(e.target.value)}
/>
<button type="submit">Sign In</button>
</form>
</div>
);
};

export default Credentials;
5 changes: 5 additions & 0 deletions examples/full-example/studio/schemas/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export default <Document>{
name: 'image',
title: 'Image',
type: 'url'
},
{
name: 'password',
type: 'string',
hidden: true
}
]
};
3 changes: 3 additions & 0 deletions examples/full-example/studio/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
// Note: This config is only used to help editors like VS Code understand/resolve
// parts, the actual transpilation is done by babel. Any compiler configuration in
// here will be ignored.
"compilerOptions": {
"outDir": "dist"
},
"include": ["./node_modules/@sanity/base/types/**/*.ts", "./**/*.ts", "./**/*.tsx"]
}
3 changes: 0 additions & 3 deletions examples/full-example/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"paths": {
"next-auth-sanity": ["../../src"]
}
},
"include": [
"next-env.d.ts",
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "next-auth-sanity",
"description": "NextAuth Adapter for Sanity",
"version": "1.1.1",
"version": "1.2.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"repository": {
Expand All @@ -28,6 +28,8 @@
},
"dependencies": {
"@sanity/client": "^2.8.0",
"argon2": "^0.27.2",
"axios": "^0.21.1",
"groq": "^2.2.6",
"lru-cache": "^6.0.0"
},
Expand Down
Loading

0 comments on commit ebb075c

Please sign in to comment.