diff options
| author | Wes Bos <wesbos@gmail.com> | 2018-02-08 10:41:16 -0500 |
|---|---|---|
| committer | Wes Bos <wesbos@gmail.com> | 2018-02-08 10:41:16 -0500 |
| commit | 19c9683e2126c68cb9d231293162c756d69c51fb (patch) | |
| tree | 4679ca2dee47740aa0bf6fe623513c4242e027e6 /backend/src | |
| parent | eabff7cd396d1f37ceb929e666f082ce57f07919 (diff) | |
WIP
Diffstat (limited to 'backend/src')
| -rw-r--r-- | backend/src/generated/prisma.graphql | 44 | ||||
| -rw-r--r-- | backend/src/resolvers/Mutation.js | 31 | ||||
| -rw-r--r-- | backend/src/resolvers/Query.js | 7 | ||||
| -rw-r--r-- | backend/src/schema.graphql | 5 | ||||
| -rw-r--r-- | backend/src/utils.js | 10 |
5 files changed, 67 insertions, 30 deletions
diff --git a/backend/src/generated/prisma.graphql b/backend/src/generated/prisma.graphql index e59a1c9..5e2d471 100644 --- a/backend/src/generated/prisma.graphql +++ b/backend/src/generated/prisma.graphql @@ -30,7 +30,7 @@ type User implements Node { posts(where: PostWhereInput, orderBy: PostOrderByInput, skip: Int, after: String, before: String, first: Int, last: Int): [Post!] website: String age: Int - dummy: String + resetToken: String } @@ -421,7 +421,7 @@ input UserCreateInput { name: String! website: String age: Int - dummy: String + resetToken: String posts: PostCreateManyWithoutAuthorInput } @@ -436,7 +436,7 @@ input UserCreateWithoutPostsInput { name: String! website: String age: Int - dummy: String + resetToken: String } type UserEdge { @@ -457,8 +457,8 @@ enum UserOrderByInput { website_DESC age_ASC age_DESC - dummy_ASC - dummy_DESC + resetToken_ASC + resetToken_DESC updatedAt_ASC updatedAt_DESC createdAt_ASC @@ -472,7 +472,7 @@ type UserPreviousValues { name: String! website: String age: Int - dummy: String + resetToken: String } type UserSubscriptionPayload { @@ -498,7 +498,7 @@ input UserUpdateInput { name: String website: String age: Int - dummy: String + resetToken: String posts: PostUpdateManyWithoutAuthorInput } @@ -517,7 +517,7 @@ input UserUpdateWithoutPostsDataInput { name: String website: String age: Int - dummy: String + resetToken: String } input UserUpdateWithoutPostsInput { @@ -612,20 +612,20 @@ input UserWhereInput { age_lte: Int age_gt: Int age_gte: Int - dummy: String - dummy_not: String - dummy_in: [String!] - dummy_not_in: [String!] - dummy_lt: String - dummy_lte: String - dummy_gt: String - dummy_gte: String - dummy_contains: String - dummy_not_contains: String - dummy_starts_with: String - dummy_not_starts_with: String - dummy_ends_with: String - dummy_not_ends_with: String + resetToken: String + resetToken_not: String + resetToken_in: [String!] + resetToken_not_in: [String!] + resetToken_lt: String + resetToken_lte: String + resetToken_gt: String + resetToken_gte: String + resetToken_contains: String + resetToken_not_contains: String + resetToken_starts_with: String + resetToken_not_starts_with: String + resetToken_ends_with: String + resetToken_not_ends_with: String posts_every: PostWhereInput posts_some: PostWhereInput posts_none: PostWhereInput diff --git a/backend/src/resolvers/Mutation.js b/backend/src/resolvers/Mutation.js index fbae6fe..95be561 100644 --- a/backend/src/resolvers/Mutation.js +++ b/backend/src/resolvers/Mutation.js @@ -2,6 +2,8 @@ const bcrypt = require('bcryptjs'); const jwt = require('jsonwebtoken'); const { forwardTo } = require('prisma-binding'); const { getUserId, Context } = require('../utils'); +const { randomBytes } = require('crypto'); +const { promisify } = require('util'); const mutations = { // Signup Mutations @@ -17,7 +19,7 @@ const mutations = { }; }, - async login(parent, { email, password }, ctx, info) { + async signin(parent, { email, password }, ctx, info) { const user = await ctx.db.query.user({ where: { email } }); if (!user) { throw new Error(`No such user found for email: ${email}`); @@ -40,8 +42,7 @@ const mutations = { }, async deleteItem(parent, args, ctx, info) { - console.log('gonna delete one'); - console.log(args); + // TODO - handle auth for deleting an item return ctx.db.mutation.deleteItem( { where: { @@ -53,7 +54,6 @@ const mutations = { }, async updateItem(parent, args, ctx, info) { - console.log('Updating an item'); const updates = { ...args }; delete updates.id; return ctx.db.mutation.updateItem({ @@ -123,9 +123,30 @@ const mutations = { text: args.text, }, }); - console.log(updatedPost); return updatedPost; }, + + // Send password request + async requestReset(parent, args, ctx, info) { + // 1. find if there is a user with that email + const user = await ctx.db.query.user({ where: { email: args.email } }); + + if (!user) { + throw new Error(`No user with the email ${args.email}`); + } + console.log(user); + // 2. Set a reset token, and a reset date + // const resetToken = await promisify(randomBytes)(20); + // const resetTokenExpiry = Date.now() + 3600000; // 1 hour from now + // console.log({ resetToken, resetTokenExpiry }); + // const res = await ctx.db.mutation.updateUser({ + // where: { email: args.email }, + // data: { resetToken, resetTokenExpiry }, + // }); + + // console.log(res); + // 3. Send them their token via email + }, }; module.exports = mutations; diff --git a/backend/src/resolvers/Query.js b/backend/src/resolvers/Query.js index a2eb749..e6ea299 100644 --- a/backend/src/resolvers/Query.js +++ b/backend/src/resolvers/Query.js @@ -1,4 +1,4 @@ -const { getUserId, Context } = require('../utils'); +const { getUserId, Context, checkForUserId } = require('../utils'); const { forwardTo } = require('prisma-binding'); const Query = { @@ -31,6 +31,11 @@ const Query = { }, me(parent, args, ctx, info) { + const Authorization = ctx.request.get('Authorization'); + if (!Authorization || Authorization === 'null') { + console.log('Authorization is null'); + return null; + } const id = getUserId(ctx); return ctx.db.query.user({ where: { id } }, info); }, diff --git a/backend/src/schema.graphql b/backend/src/schema.graphql index 993236e..8a2fa23 100644 --- a/backend/src/schema.graphql +++ b/backend/src/schema.graphql @@ -26,7 +26,8 @@ type Query { type Mutation { signup(email: String!, password: String!, name: String!): AuthPayload! - login(email: String!, password: String!): AuthPayload! + signin(email: String!, password: String!): AuthPayload! + requestReset(email: String!): User createDraft(title: String!, text: String!): Post! publish(id: ID!): Post! deletePost(id: ID!): Post! @@ -41,7 +42,6 @@ type AuthPayload { user: User! } -# THe user definition overrides type User { id: ID! email: String! @@ -49,4 +49,5 @@ type User { name: String! posts: [Post!]! age: Int + resetToken: String } diff --git a/backend/src/utils.js b/backend/src/utils.js index 35587db..642c04a 100644 --- a/backend/src/utils.js +++ b/backend/src/utils.js @@ -11,6 +11,15 @@ function getUserId(ctx) { throw new AuthError(); } +function checkForUserId(ctx) { + const Authorization = ctx.request.get('Authorization'); + if (Authorization) { + const token = Authorization.replace('Bearer ', ''); + const { userId } = jwt.verify(token, process.env.APP_SECRET); + return userId; + } +} + class AuthError extends Error { constructor() { super('Not authorized'); @@ -20,4 +29,5 @@ class AuthError extends Error { module.exports = { getUserId, AuthError, + checkForUserId, }; |
