Progress with authentication tolen

This commit is contained in:
Riccardo
2021-01-05 13:58:26 +01:00
parent 4d021d6043
commit 436eb4410c
8 changed files with 138 additions and 34 deletions

21
server/src/models/user.js Normal file
View File

@@ -0,0 +1,21 @@
import mongoose from 'mongoose';
const Schema = mongoose.Schema;
const UserSchema = new Schema({
username: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
deleted: {
type: Boolean,
required: false
}
});
export default mongoose.model('user', UserSchema);

View File

@@ -1,7 +1,11 @@
// import Appointment from '../../client/src/components/Appointment.js';
import Product from './models/product.js';
import Appointment from './models/appointment.js';
import User from './models/user.js'
// import { createAppointment } from './resolvers/Mutation.js';
import bcrypt from 'bcryptjs';
import jwt from 'jsonwebtoken';
import dotenv from 'dotenv';
export const resolvers = {
Query: {
@@ -11,8 +15,58 @@ export const resolvers = {
async allProducts() {
return await Product.find();
},
async allUsers() {
return await User.find();
},
},
Mutation: {
async signup(root, {
input
}) {
console.log(input.password);
input.password = await bcrypt.hash(input.password, 10);
const user = await User.create(input);
const token = jwt.sign({ userId: user.id }, process.env.APP_SECRET);
return {
token,
user
};
},
async login(root, {
email, password
}) {
const user = await User.findOne({
email: email
});
if (!user) {
throw new Error('No such user found');
}
const pwd = await bcrypt.hash(password, 10);
console.log(pwd);
console.log(user.password);
const valid = await bcrypt.compare(
password,
user.password
);
if (!valid) {
throw new Error('Invalid password');
}
const token = jwt.sign({ userId: user.id }, APP_SECRET);
return {
token,
user
};
},
async createAppointment(root, {
input
}) {

View File

@@ -8,6 +8,7 @@ type Query {
): Feed!
allProducts: [Product]
allAppointments: [Appointment]
allUsers: [User]
users: [User!]!
}
@@ -39,9 +40,7 @@ type Mutation {
_id: ID!
) : Product
signup(
email: String!
password: String!
name: String!
input: UserInput
): AuthPayload
login(
email: String!,
@@ -57,19 +56,26 @@ type Subscription {
newFollow: Follow
}
#User Schemas
type User {
_id: ID!
username: String!
email: String!
password: String!
# appointments: [Appointment!]!
}
input UserInput{
username: String!
email: String!
password: String!
# appointments: [Appointment!]!
}
type AuthPayload {
token: String
user: User
}
type User {
_id: ID!
name: String!
email: String!
appointments: [Appointment!]!
}
# Appointment model
# Appointment schemas
type Appointment {
_id: ID!
title: String!
@@ -91,8 +97,13 @@ input AppointmentInput {
timeStart: Time!
timeEnd: Time!
}
input AppointmentOrderByInput {
description: Sort
url: Sort
createdAt: Sort
}
# Product model
# Product schemas
type Product {
_id: ID!
title: String!
@@ -103,23 +114,17 @@ input ProductInput {
qty: Int
}
# Follow schemas
type Follow {
_id: ID!
appointment: Appointment!
user: User!
}
input AppointmentOrderByInput {
description: Sort
url: Sort
createdAt: Sort
}
# General-purpose schemas
enum Sort {
asc
desc
}
scalar DateTime
scalar Time