Skip to content

Commit

Permalink
fix(customize): fixed how get profile data and send the profile data (#…
Browse files Browse the repository at this point in the history
…11)

* fix(customize): fixed how get profile data and send the profile data

* chore(version): changed version to 1.4.4
  • Loading branch information
fedeya authored Aug 15, 2022
1 parent 2b5ce2e commit 3573575
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 52 deletions.
2 changes: 1 addition & 1 deletion examples/full-example/src/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import NextAuth, { NextAuthOptions } from 'next-auth';
import GitHub from 'next-auth/providers/github';
import { SanityAdapter, SanityCredentials } from '../../../../../../dist';
import { SanityAdapter, SanityCredentials } from 'next-auth-sanity';
import { client } from '../../../libs/sanity';

const options: NextAuthOptions = {
Expand Down
2 changes: 1 addition & 1 deletion examples/full-example/src/pages/api/sanity/signUp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { signUpHandler } from '../../../../../../dist';
import { signUpHandler } from 'next-auth-sanity';
import { client } from '../../../libs/sanity';

export default signUpHandler(client);
5 changes: 3 additions & 2 deletions examples/full-example/src/pages/credentials.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { FC, useState } from 'react';
import { useSession, signOut, signIn } from 'next-auth/react';
import { signUp } from '../../../../dist/client';
import { signUp } from 'next-auth-sanity/client';

const Credentials: FC = () => {
const [email, setEmail] = useState('');
Expand All @@ -19,7 +19,8 @@ const Credentials: FC = () => {

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

Expand Down
18 changes: 4 additions & 14 deletions examples/full-example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
Expand All @@ -16,14 +12,8 @@
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"jsx": "preserve"
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx"
],
"exclude": [
"node_modules"
]
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
2 changes: 1 addition & 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.4.3",
"version": "1.4.4",
"main": "index.js",
"types": "index.d.ts",
"repository": {
Expand Down
27 changes: 7 additions & 20 deletions src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,17 @@ export function SanityAdapter(
): Adapter {
return {
async createUser(profile) {
const user = await client.create({
const { _id, ...user } = await client.create({
_id: `user.${uuid()}`,
_type: options.schemas.user,
email: profile.email,
name: profile.name,
image: profile.image
...profile
});

return {
id: user._id,
id: _id,
emailVerified: null,
email: user.email,
name: user.name,
image: user.image
} as AdapterUser;
...user
};
},

async getUser(id) {
Expand Down Expand Up @@ -86,22 +82,13 @@ export function SanityAdapter(
async deleteSession() {},

async updateUser(user) {
const { id, name, email, image } = user;

const newUser = await client
.patch(id!)
.set({
name,
email,
image
})
.commit();
const newUser = await client.patch(user.id!).set(user).commit();

return {
id: newUser._id,
...newUser,
emailVerified: null
} as AdapterUser;
};
},

async getUserByEmail(email) {
Expand Down
4 changes: 2 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { User } from 'next-auth';

export interface SignUpPayload {
export type SignUpPayload = {
email: string;
password: string;
name?: string;
image?: string;
}
} & Record<string, any>;

export const signUp = async (payload: SignUpPayload): Promise<User> => {
const res = await fetch('/api/sanity/signUp', {
Expand Down
20 changes: 9 additions & 11 deletions src/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type CredentialsConfig = ReturnType<CredentialsProvider>;
export const signUpHandler =
(client: SanityClient, userSchema: string = 'user') =>
async (req: any, res: any) => {
const { email, password, name, image } = req.body;
const { email, password, name, image, ...userData } = req.body;

const user = await client.fetch(getUserByEmailQuery, {
userSchema,
Expand All @@ -22,19 +22,19 @@ export const signUpHandler =
return;
}

const newUser = await client.create({
const { password: _, ...newUser } = await client.create({
_id: `user.${uuid()}`,
_type: userSchema,
email,
password: await argon2.hash(password),
name,
image
image,
...userData
});

res.json({
email: newUser.email,
name: newUser.name,
image: newUser.image
id: newUser._id,
...newUser
});
};

Expand All @@ -57,7 +57,7 @@ export const SanityCredentials = (
}
},
async authorize(credentials) {
const user = await client.fetch(getUserByEmailQuery, {
const { _id, ...user } = await client.fetch(getUserByEmailQuery, {
userSchema,
email: credentials?.email
});
Expand All @@ -66,10 +66,8 @@ export const SanityCredentials = (

if (await argon2.verify(user.password, credentials?.password!)) {
return {
email: user.email,
name: user.name,
image: user.image,
id: user._id
id: _id,
...user
};
}

Expand Down

0 comments on commit 3573575

Please sign in to comment.