Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
318 additions
and
9 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
var mongoose = require("mongoose"); | ||
|
||
const UserSchema = new mongoose.Schema({ | ||
name: String, | ||
password: String, | ||
email: String, | ||
active: Boolean | ||
}); | ||
|
||
module.exports = mongoose.model("User", UserSchema); |
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,17 @@ | ||
const graphql = require("graphql"); | ||
const globalIdField = require("graphql-relay").globalIdField; | ||
|
||
const { GraphQLObjectType, GraphQLString } = graphql; | ||
|
||
const UserType = new GraphQLObjectType({ | ||
name: "User", | ||
fields: () => ({ | ||
id: globalIdField("users"), | ||
_id: { type: GraphQLString }, | ||
name: { type: GraphQLString }, | ||
email: { type: GraphQLString }, | ||
active: { type: GraphQLString } | ||
}) | ||
}); | ||
|
||
module.exports = UserType; |
51 changes: 51 additions & 0 deletions
51
packages/server/src/modules/event/mutation/UserLoginWithMutation.js
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,51 @@ | ||
import { GraphQLString, GraphQLNonNull } from "graphql"; | ||
import { mutationWithClientMutationId } from "graphql-relay"; | ||
|
||
import { generateToken } from "../../../auth"; | ||
|
||
import UserModel from "../UserModel"; | ||
|
||
export default mutationWithClientMutationId({ | ||
name: "UserLoginWithEmail", | ||
inputFields: { | ||
email: { | ||
type: new GraphQLNonNull(GraphQLString) | ||
}, | ||
password: { | ||
type: new GraphQLNonNull(GraphQLString) | ||
} | ||
}, | ||
mutateAndGetPayload: async ({ email, password }) => { | ||
const user = await UserModel.findOne({ email: email.toLowerCase() }); | ||
|
||
const defaultErrorMessage = "Invalid password"; | ||
|
||
if (!user) { | ||
return { | ||
error: defaultErrorMessage | ||
}; | ||
} | ||
|
||
const correctPassword = user.authenticate(password); | ||
|
||
if (!correctPassword) { | ||
return { | ||
error: defaultErrorMessage | ||
}; | ||
} | ||
|
||
return { | ||
token: generateToken(user) | ||
}; | ||
}, | ||
outputFields: { | ||
token: { | ||
type: GraphQLString, | ||
resolve: ({ token }) => token | ||
}, | ||
error: { | ||
type: GraphQLString, | ||
resolve: ({ error }) => error | ||
} | ||
} | ||
}); |
56 changes: 56 additions & 0 deletions
56
packages/server/src/modules/event/mutation/UserRegisterWIthMutation.js
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,56 @@ | ||
import { GraphQLString, GraphQLNonNull } from "graphql"; | ||
import { mutationWithClientMutationId } from "graphql-relay"; | ||
|
||
import { generateToken } from "../../../auth"; | ||
// import pubSub, { EVENTS } from '../../../pubSub'; | ||
|
||
import UserModel from "../UserModel"; | ||
|
||
export default mutationWithClientMutationId({ | ||
name: "UserRegisterWithEmail", | ||
inputFields: { | ||
name: { | ||
type: new GraphQLNonNull(GraphQLString) | ||
}, | ||
email: { | ||
type: new GraphQLNonNull(GraphQLString) | ||
}, | ||
password: { | ||
type: new GraphQLNonNull(GraphQLString) | ||
} | ||
}, | ||
mutateAndGetPayload: async ({ name, email, password }) => { | ||
let user = await UserModel.findOne({ email: email.toLowerCase() }); | ||
|
||
if (user) { | ||
return { | ||
error: "Email already in use" | ||
}; | ||
} | ||
|
||
user = new UserModel({ | ||
name, | ||
email, | ||
password | ||
}); | ||
|
||
await user.save(); | ||
|
||
// Subscription not working | ||
// await pubSub.publish(EVENTS.USER.ADDED, { UserAdded: { user } }); | ||
|
||
return { | ||
token: generateToken(user) | ||
}; | ||
}, | ||
outputFields: { | ||
token: { | ||
type: GraphQLString, | ||
resolve: ({ token }) => token | ||
}, | ||
error: { | ||
type: GraphQLString, | ||
resolve: ({ error }) => error | ||
} | ||
} | ||
}); |
Oops, something went wrong.