Skip to content

Commit

Permalink
next 14 changes (#37)
Browse files Browse the repository at this point in the history
* feat: make available signUpHandler in route handlers and ege runtime

* docs: add route handler
  • Loading branch information
fedeya authored May 25, 2023
1 parent 4d0920b commit ce3bf81
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 136 deletions.
40 changes: 12 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export default {

### Setup

`API Route`
`API Route` (with pages)

```ts
// pages/api/sanity/signUp.ts
Expand All @@ -216,6 +216,16 @@ import { client } from 'your/sanity/client';
export default signUpHandler(client);
```

`Route Handler` (with app directory)

```ts
// app/api/sanity/signUp/route.ts
import { signUpHandler } from 'next-auth-sanity';
import { client } from 'your/sanity/client';

export const POST = signUpHandler(client);
```

`Client`

```ts
Expand Down Expand Up @@ -255,35 +265,9 @@ SanityCredentials(client, 'profile');
signUpHandler(client, 'profile');
```

## App Directory

With next@canary and appDir, you may experience the following error;

```js
./node_modules/@mapbox/node-pre-gyp/lib/util/nw-pre-gyp/index.html
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> <!doctype html>
| <html>
| <head>

```

This issue is caused by using argon2 which depends on node. To workaround this for the time being, add the next line to your `next.config.js`.

```ts
// next.config.js
module.exports = {
experimental: {
appDir: true,
serverComponentsExternalPackages: ['argon2'],
}
}
```

## Author

👤 **Fedeya <elfedeminaya@gmail.com>**
👤 **Fedeya <hello@fedeminaya.com>**

- Website: [fedeminaya.com](https://fedeminaya.com)
- Twitter: [@fedeminaya](https://twitter.com/fedeminaya)
Expand Down
7 changes: 1 addition & 6 deletions examples/full-example/next.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
/** @type {import('next').NextConfig} */
module.exports = {
experimental: {
appDir: true,
serverComponentsExternalPackages: ['argon2']
}
};
module.exports = {};
2 changes: 1 addition & 1 deletion examples/full-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"dependencies": {
"@sanity/client": "^5.2.2",
"next": "^13.2.4-canary.2",
"next": "^13.4.3",
"next-auth": "^4.20.1",
"react": "^18.2.0",
"react-dom": "^18.2.0"
Expand Down
7 changes: 7 additions & 0 deletions examples/full-example/src/app/api/sanity/signUp/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

import { signUpHandler } from 'next-auth-sanity';
import { client } from '@/lib/sanity';

const handler = signUpHandler(client);

export { handler as POST };
179 changes: 87 additions & 92 deletions examples/full-example/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -797,75 +797,55 @@
call-me-maybe "^1.0.1"
glob-to-regexp "^0.3.0"

"@next/[email protected]":
version "13.2.4-canary.2"
resolved "https://registry.yarnpkg.com/@next/env/-/env-13.2.4-canary.2.tgz#9270116e0a33ffcd472588e6d0f891ff2c12ebe2"
integrity sha512-RtiIL/Mae5Xq5TRso9Q/0/Z86RvDQy3HBt+v9yKuIvIMDuggUZHm9+LzcPppqolaWM2Nv8tWHE1/UCvtCb+SDg==

"@next/[email protected]":
version "13.2.4-canary.2"
resolved "https://registry.yarnpkg.com/@next/swc-android-arm-eabi/-/swc-android-arm-eabi-13.2.4-canary.2.tgz#9d225ac0ac8bb460f7121ba37911a67058c32b25"
integrity sha512-o3GB8gMOVjLh4Gy4+dMTqaKzrXdUM9vnZ0ZmyBwqe+x5m7jg4QhJ1lfMzD7ACghubeAxNgv6xlrXs5ngNw4ppw==

"@next/[email protected]":
version "13.2.4-canary.2"
resolved "https://registry.yarnpkg.com/@next/swc-android-arm64/-/swc-android-arm64-13.2.4-canary.2.tgz#5904e1b1cfa1c943916fbecf6e81d59acd31cc7d"
integrity sha512-58Kayvi/plf8dp6wvQWe7bW4+Cd3NtNpz4kQKVOHSh1URScWclI7OK6Ouo9mh0EZ0z1b9CJB/Bxc2KCdB/gZ7w==

"@next/[email protected]":
version "13.2.4-canary.2"
resolved "https://registry.yarnpkg.com/@next/swc-darwin-arm64/-/swc-darwin-arm64-13.2.4-canary.2.tgz#41eaa71fce1126622418250ff40aae104d109b0d"
integrity sha512-D6UEC1epUeM3UvB513u8RhqAUrV9h1YPffu/LxU/RNxqi5G6BRa9ZbrlWND2Mgm49uExoINiu9spf51ed/sSCw==

"@next/[email protected]":
version "13.2.4-canary.2"
resolved "https://registry.yarnpkg.com/@next/swc-darwin-x64/-/swc-darwin-x64-13.2.4-canary.2.tgz#1726fdae5c1995cdb6f539e2dd56ea0b189613aa"
integrity sha512-vr53fFSIQeEUA/S9yH93MuqSBm6HTC+Tf3keR85nnAB0lTtVLrmZQOg+9TNgT0fewJhLDxgQ0Ivttd0rHy/moQ==

"@next/[email protected]":
version "13.2.4-canary.2"
resolved "https://registry.yarnpkg.com/@next/swc-freebsd-x64/-/swc-freebsd-x64-13.2.4-canary.2.tgz#de18e431ac0057699fad09b91b615302bf8a2e2d"
integrity sha512-WjVBeO2y9DjhDKAZWaC4eikHCHGwVgV4gik+6kXpiw8evWaoqH+JXc0SgqdOhP5r4TGwb3+wpf4F/fLYv2EHpA==

"@next/[email protected]":
version "13.2.4-canary.2"
resolved "https://registry.yarnpkg.com/@next/swc-linux-arm-gnueabihf/-/swc-linux-arm-gnueabihf-13.2.4-canary.2.tgz#f7dddb0e2e1d7991700f1448818daecb781a76f4"
integrity sha512-uh6llx+L0wKN/uIV76tLIvEG7x4TGA5EuAMJkjMVHarZzeAuKxs9cWqLbmxWFhuxlPd9gahVf3Fgdpepco2JSQ==

"@next/[email protected]":
version "13.2.4-canary.2"
resolved "https://registry.yarnpkg.com/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-13.2.4-canary.2.tgz#4f44ce1029c8df3558d0a087b102d4a9bc3c566c"
integrity sha512-RaZbEhHcJzbbN9u1gclPXGSJmiAKiTXJswmQQz+9eknEx+BiRxKLExuZ69ljXL/1xJfZDHDLNNpf/nnmvYxACA==

"@next/[email protected]":
version "13.2.4-canary.2"
resolved "https://registry.yarnpkg.com/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-13.2.4-canary.2.tgz#ce6582534f2ca3a8ceb81bb7699442e9b00fe1a8"
integrity sha512-ZCzYr/XnY+I2uSI0svqNaSH8E4G9fTMeWl1/UwhmhWV6Y0Javfm5NTzpkxOLdGSRkAelTU7U59NYkIhWIg76rg==

"@next/[email protected]":
version "13.2.4-canary.2"
resolved "https://registry.yarnpkg.com/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-13.2.4-canary.2.tgz#28c7871450499bbe7fdb166db99a25e3d36d51e6"
integrity sha512-KkEruFMkH2WCM0B1C9A1AC++LjIGmdLmVGt2Jfqy4bIQ30HopOm8EfwcEVfZRKTCwuL6dU3FUVQFiPE1ag2F7w==

"@next/[email protected]":
version "13.2.4-canary.2"
resolved "https://registry.yarnpkg.com/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-13.2.4-canary.2.tgz#2bab94609af8c8f26ac2248b029d3c4414c9d90c"
integrity sha512-MWp74FRKIN2nhujBg3doP9qZVEF72jf0oBGjEnzylm46pvOB87aOFO7zuTohxAHRjt48FI1BvL6jPjI7A9QgdA==

"@next/[email protected]":
version "13.2.4-canary.2"
resolved "https://registry.yarnpkg.com/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-13.2.4-canary.2.tgz#a1caca5d2dc1b6d03cdbc11ff52ff938a0267e87"
integrity sha512-C0VzGbgNoen9O8AAuW8Rqh6Hyc7Sl3Y3I3NZV1kZp6/v+u+/dUmlSy7DDP1sgE8RwmpJQ4l3kCh6qF5NndETig==

"@next/[email protected]":
version "13.2.4-canary.2"
resolved "https://registry.yarnpkg.com/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-13.2.4-canary.2.tgz#7371aeedf606c722407ec33ce591956ab49d5378"
integrity sha512-ymbyus4yaJDTpBiTVZz4gexutyuAjokmCjido6yDOxg21MbYq61ifMWCpB19JK4hVHR8pQqNYl9BhzN7ta0+Eg==

"@next/[email protected]":
version "13.2.4-canary.2"
resolved "https://registry.yarnpkg.com/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-13.2.4-canary.2.tgz#47792f0ac1e3ddef1a8cd7daa1039d009eb8ec5e"
integrity sha512-Cl5bQDnkYEyoudhh8w2pzDRHxqnaGSRVJL0ZZYLTnc37hv/HMHlfgATR5TM9A6ATkjK+7Mi5F6x6taKzepAu/Q==
"@next/[email protected]":
version "13.4.3"
resolved "https://registry.yarnpkg.com/@next/env/-/env-13.4.3.tgz#cb00bdd43a0619a79a52c9336df8a0aa84f8f4bf"
integrity sha512-pa1ErjyFensznttAk3EIv77vFbfSYT6cLzVRK5jx4uiRuCQo+m2wCFAREaHKIy63dlgvOyMlzh6R8Inu8H3KrQ==

"@next/[email protected]":
version "13.4.3"
resolved "https://registry.yarnpkg.com/@next/swc-darwin-arm64/-/swc-darwin-arm64-13.4.3.tgz#2d6c99dd5afbcce37e4ba0f64196317a1259034d"
integrity sha512-yx18udH/ZmR4Bw4M6lIIPE3JxsAZwo04iaucEfA2GMt1unXr2iodHUX/LAKNyi6xoLP2ghi0E+Xi1f4Qb8f1LQ==

"@next/[email protected]":
version "13.4.3"
resolved "https://registry.yarnpkg.com/@next/swc-darwin-x64/-/swc-darwin-x64-13.4.3.tgz#162b15fb8a54d9f64e69c898ebeb55b7dac9bddd"
integrity sha512-Mi8xJWh2IOjryAM1mx18vwmal9eokJ2njY4nDh04scy37F0LEGJ/diL6JL6kTXi0UfUCGbMsOItf7vpReNiD2A==

"@next/[email protected]":
version "13.4.3"
resolved "https://registry.yarnpkg.com/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-13.4.3.tgz#aee57422f11183d6a2e4a2e8aa23b9285873e18f"
integrity sha512-aBvtry4bxJ1xwKZ/LVPeBGBwWVwxa4bTnNkRRw6YffJnn/f4Tv4EGDPaVeYHZGQVA56wsGbtA6nZMuWs/EIk4Q==

"@next/[email protected]":
version "13.4.3"
resolved "https://registry.yarnpkg.com/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-13.4.3.tgz#c10b6aaaa47b341c6c9ea15f8b0ddb37e255d035"
integrity sha512-krT+2G3kEsEUvZoYte3/2IscscDraYPc2B+fDJFipPktJmrv088Pei/RjrhWm5TMIy5URYjZUoDZdh5k940Dyw==

"@next/[email protected]":
version "13.4.3"
resolved "https://registry.yarnpkg.com/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-13.4.3.tgz#3f85bc5591c6a0d4908404f7e88e3c04f4462039"
integrity sha512-AMdFX6EKJjC0G/CM6hJvkY8wUjCcbdj3Qg7uAQJ7PVejRWaVt0sDTMavbRfgMchx8h8KsAudUCtdFkG9hlEClw==

"@next/[email protected]":
version "13.4.3"
resolved "https://registry.yarnpkg.com/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-13.4.3.tgz#f4535adc2374a86bc8e43af149b551567df065de"
integrity sha512-jySgSXE48shaLtcQbiFO9ajE9mqz7pcAVLnVLvRIlUHyQYR/WyZdK8ehLs65Mz6j9cLrJM+YdmdJPyV4WDaz2g==

"@next/[email protected]":
version "13.4.3"
resolved "https://registry.yarnpkg.com/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-13.4.3.tgz#e76106d85391c308c5ed70cda2bca2c582d65536"
integrity sha512-5DxHo8uYcaADiE9pHrg8o28VMt/1kR8voDehmfs9AqS0qSClxAAl+CchjdboUvbCjdNWL1MISCvEfKY2InJ3JA==

"@next/[email protected]":
version "13.4.3"
resolved "https://registry.yarnpkg.com/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-13.4.3.tgz#8eb5d9dd71ed7a971671291605ad64ad522fb3bc"
integrity sha512-LaqkF3d+GXRA5X6zrUjQUrXm2MN/3E2arXBtn5C7avBCNYfm9G3Xc646AmmmpN3DJZVaMYliMyCIQCMDEzk80w==

"@next/[email protected]":
version "13.4.3"
resolved "https://registry.yarnpkg.com/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-13.4.3.tgz#c7b2b1b9e158fd7749f8209e68ee8e43a997eb4c"
integrity sha512-jglUk/x7ZWeOJWlVoKyIAkHLTI+qEkOriOOV+3hr1GyiywzcqfI7TpFSiwC7kk1scOiH7NTFKp8mA3XPNO9bDw==

"@nodelib/fs.stat@^1.1.2":
version "1.1.3"
Expand Down Expand Up @@ -1006,10 +986,10 @@
event-source-polyfill "1.0.25"
eventsource "^2.0.2"

"@swc/helpers@0.4.14":
version "0.4.14"
resolved "https://registry.yarnpkg.com/@swc/helpers/-/helpers-0.4.14.tgz#1352ac6d95e3617ccb7c1498ff019654f1e12a74"
integrity sha512-4C7nX/dvpzB7za4Ql9K81xK3HPxCpHMgwTZVyf+9JQ6VUbn9jjZVN7/Nkdz/Ugzs2CSjqnL/UPXroiVBVHUWUw==
"@swc/helpers@0.5.1":
version "0.5.1"
resolved "https://registry.yarnpkg.com/@swc/helpers/-/helpers-0.5.1.tgz#e9031491aa3f26bfcc974a67f48bd456c8a5357a"
integrity sha512-sJ902EfIzn1Fa+qYmjdQqh8tPsoxyBz+8yBKC2HKUxyezKJFwPGOn7pv4WY6QuQW//ySQi5lJjA/ZT9sNWWNTg==
dependencies:
tslib "^2.4.0"

Expand Down Expand Up @@ -1353,6 +1333,13 @@ builtins@^1.0.3:
resolved "https://registry.yarnpkg.com/builtins/-/builtins-1.0.3.tgz#cb94faeb61c8696451db36534e1422f94f0aee88"
integrity sha1-y5T662HIaWRR2zZTThQi+U8K7og=

[email protected]:
version "1.6.0"
resolved "https://registry.yarnpkg.com/busboy/-/busboy-1.6.0.tgz#966ea36a9502e43cdb9146962523b92f531f6893"
integrity sha512-8SFQbg/0hQ9xy3UNTB0YEnsNBbWfhf7RtnzpL7TkBiTBRfrQ9Fxcnz7VJsleJpyp6rVLvXiuORqjlHi5q+PYuA==
dependencies:
streamsearch "^1.1.0"

byline@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/byline/-/byline-5.0.0.tgz#741c5216468eadc457b03410118ad77de8c1ddb1"
Expand Down Expand Up @@ -3694,30 +3681,28 @@ next-auth@^4.20.1:
preact-render-to-string "^5.1.19"
uuid "^8.3.2"

next@^13.2.4-canary.2:
version "13.2.4-canary.2"
resolved "https://registry.yarnpkg.com/next/-/next-13.2.4-canary.2.tgz#49ac82cba646b2d00bd5e08924b44cfa20dffe9f"
integrity sha512-MOL3AessZFXWT2TDQTybi/D5EBDk11jo7T0WnbabceGP7kNuKqh5RlshJZyTdYxeTWhhHdfsSYBtqRelM2Xnng==
next@^13.4.3:
version "13.4.3"
resolved "https://registry.yarnpkg.com/next/-/next-13.4.3.tgz#7f417dec9fa2731d8c1d1819a1c7d0919ad6fc75"
integrity sha512-FV3pBrAAnAIfOclTvncw9dDohyeuEEXPe5KNcva91anT/rdycWbgtu3IjUj4n5yHnWK8YEPo0vrUecHmnmUNbA==
dependencies:
"@next/env" "13.2.4-canary.2"
"@swc/helpers" "0.4.14"
"@next/env" "13.4.3"
"@swc/helpers" "0.5.1"
busboy "1.6.0"
caniuse-lite "^1.0.30001406"
postcss "8.4.14"
styled-jsx "5.1.1"
zod "3.21.4"
optionalDependencies:
"@next/swc-android-arm-eabi" "13.2.4-canary.2"
"@next/swc-android-arm64" "13.2.4-canary.2"
"@next/swc-darwin-arm64" "13.2.4-canary.2"
"@next/swc-darwin-x64" "13.2.4-canary.2"
"@next/swc-freebsd-x64" "13.2.4-canary.2"
"@next/swc-linux-arm-gnueabihf" "13.2.4-canary.2"
"@next/swc-linux-arm64-gnu" "13.2.4-canary.2"
"@next/swc-linux-arm64-musl" "13.2.4-canary.2"
"@next/swc-linux-x64-gnu" "13.2.4-canary.2"
"@next/swc-linux-x64-musl" "13.2.4-canary.2"
"@next/swc-win32-arm64-msvc" "13.2.4-canary.2"
"@next/swc-win32-ia32-msvc" "13.2.4-canary.2"
"@next/swc-win32-x64-msvc" "13.2.4-canary.2"
"@next/swc-darwin-arm64" "13.4.3"
"@next/swc-darwin-x64" "13.4.3"
"@next/swc-linux-arm64-gnu" "13.4.3"
"@next/swc-linux-arm64-musl" "13.4.3"
"@next/swc-linux-x64-gnu" "13.4.3"
"@next/swc-linux-x64-musl" "13.4.3"
"@next/swc-win32-arm64-msvc" "13.4.3"
"@next/swc-win32-ia32-msvc" "13.4.3"
"@next/swc-win32-x64-msvc" "13.4.3"

nice-try@^1.0.4:
version "1.0.5"
Expand Down Expand Up @@ -5015,6 +5000,11 @@ stream-shift@^1.0.0:
resolved "https://registry.yarnpkg.com/stream-shift/-/stream-shift-1.0.1.tgz#d7088281559ab2778424279b0877da3c392d5a3d"
integrity sha512-AiisoFqQ0vbGcZgQPY1cdP2I76glaVA/RauYR4G4thNFgkTqr90yXTo4LYX60Jl+sIlPNHHdGSwo01AvbKUSVQ==

streamsearch@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/streamsearch/-/streamsearch-1.1.0.tgz#404dd1e2247ca94af554e841a8ef0eaa238da764"
integrity sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==

strict-uri-encode@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/strict-uri-encode/-/strict-uri-encode-2.0.0.tgz#b9c7330c7042862f6b142dc274bbcc5866ce3546"
Expand Down Expand Up @@ -5661,3 +5651,8 @@ yargs@^14.2.2:
which-module "^2.0.0"
y18n "^4.0.0"
yargs-parser "^15.0.1"

[email protected]:
version "3.21.4"
resolved "https://registry.yarnpkg.com/zod/-/zod-3.21.4.tgz#10882231d992519f0a10b5dd58a38c9dabbb64db"
integrity sha512-m46AKbrzKVzOzs/DZgVnG5H55N1sv1M8qZU3A8RIKbs3mrACDNeIOeilDymVb2HdmP8uwshOCF4uJ8uM9rCqJw==
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
],
"scripts": {
"build": "tsc",
"dev": "tsc -w",
"clean": "rm -rf index.js index.d.ts queries.js queries.ts queries.d.ts adapter.js adapter.ts adapter.d.ts client.js client.ts client.d.ts schemas.js schemas.ts schemas.d.ts credentials.js credentials.ts credentials.d.ts",
"prepublishOnly": "yarn build",
"postpublish": "yarn clean"
Expand Down
12 changes: 4 additions & 8 deletions src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,13 @@ export function SanityAdapter(
return {
async createUser(profile) {
const { emailVerified: tempEmailVerified, ...tempProfile } = profile;
const {
_id,
emailVerified,
...user
} = await client.create({
const { _id, emailVerified, ...user } = await client.create({
_id: `user.${uuid()}`,
_type: options.schemas.user,
emailVerified:
tempEmailVerified === null ? undefined : tempEmailVerified,
...tempProfile,
});
...tempProfile
});

return {
id: _id,
Expand Down Expand Up @@ -95,7 +91,7 @@ export function SanityAdapter(
.set({
emailVerified:
tempEmailVerified === null ? undefined : tempEmailVerified,
...tempUser,
...tempUser
})
.commit<typeof user>();

Expand Down
15 changes: 14 additions & 1 deletion src/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import argon2 from 'argon2';
export const signUpHandler =
(client: SanityClient, userSchema: string = 'user') =>
async (req: any, res: any) => {
const { email, password, name, image, ...userData } = req.body;
const isEdge = req instanceof Request;

const body = isEdge ? await req.json() : req.body;

const { email, password, name, image, ...userData } = body;

const user = await client.fetch(getUserByEmailQuery, {
userSchema,
Expand All @@ -30,6 +34,15 @@ export const signUpHandler =
...userData
});

if (isEdge) {
return new Response(JSON.stringify(newUser), {
headers: {
'Content-Type': 'application/json'
},
status: 200
});
}

res.json({
id: newUser._id,
...newUser
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,6 @@
"skipLibCheck": true /* Skip type checking of declaration files. */,
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
},
"include": ["./src/**/*"],
"exclude": ["./examples", "./dist"]
}

0 comments on commit ce3bf81

Please sign in to comment.