-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: sign in with credentials * 1.2.0
- Loading branch information
Showing
15 changed files
with
716 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
dist | ||
.env | ||
.env | ||
.next |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ | |
</a> | ||
</p> | ||
|
||
> NextAuth Adapter for Sanity | ||
> NextAuth Adapter and Provider for Sanity | ||
## Overview | ||
|
||
|
@@ -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 | ||
|
||
|
@@ -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 | ||
|
@@ -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 | ||
} | ||
] | ||
}; | ||
|
@@ -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]>** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.