Commit ddb977b9 by qiuzhi99

register && login

1 parent 44e25903
module.exports = {
MONGODB: "mongodb://test:A12345678@ds119820.mlab.com:19820/merng",
SECRET_KEY: "some very secret key"
};
...@@ -4,5 +4,8 @@ const usersResolvers = require("./users"); ...@@ -4,5 +4,8 @@ const usersResolvers = require("./users");
module.exports = { module.exports = {
Query: { Query: {
...postsResolvers.Query ...postsResolvers.Query
},
Mutation: {
...usersResolvers.Mutation
} }
}; };
const bcrypt = require("bcryptjs");
const User = require("../../models/User");
const jwt = require("jsonwebtoken");
const { SECRET_KEY } = require("../../config");
const { UserInputError } = require("apollo-server");
const {
validateRegisterInput,
validateLoginInput
} = require("../../util/validators");
function generateToken(user) {
return jwt.sign(
{
id: user.id,
email: user.email,
username: user.username
},
SECRET_KEY,
{ expiresIn: "1h" }
);
}
module.exports = {
Mutation: {
async login(_, { username, password }) {
const { errors, valid } = validateLoginInput(username, password);
if (!valid) {
throw new UserInputError("Errors", { errors });
}
const user = await User.findOne({ username });
if (!user) {
errors.general = "User not found";
throw new UserInputError("User not found", { errors });
}
const match = await bcrypt.compare(password, user.password);
if (!match) {
errors.general = "Wrong credentials";
throw new UserInputError("Wrong credentials", { errors });
}
const token = generateToken(user);
return {
...user._doc,
id: user._id,
token
};
},
async register(
_,
{
registerInput: { username, email, password, confirmPassword }
}
) {
// TODO: Validate user data
const { errors, valid } = validateRegisterInput(
username,
email,
password,
confirmPassword
);
if (!valid) {
throw new UserInputError("Errors", { errors });
}
// TODO: Make sure user doesnt already exist
const user = await User.findOne({ username });
if (user) {
throw new UserInputError("Username is taken", {
errors: {
username: "The username is taken"
}
});
}
// TODO: hash password and create an auth token
password = await bcrypt.hash(password, 12);
const newUser = new User({
email,
username,
password,
createdAt: new Date().toISOString()
});
const res = await newUser.save();
const token = generateToken(res);
return {
...res._doc,
id: res._id,
token
};
}
}
};
...@@ -29,5 +29,6 @@ module.exports = gql` ...@@ -29,5 +29,6 @@ module.exports = gql`
type Mutation { type Mutation {
register(registerInput: RegisterInput): User! register(registerInput: RegisterInput): User!
login(username: String!, password: String!): User!
} }
`; `;
...@@ -2,7 +2,7 @@ const { ApolloServer } = require("apollo-server"); ...@@ -2,7 +2,7 @@ const { ApolloServer } = require("apollo-server");
const mongoose = require("mongoose"); const mongoose = require("mongoose");
const Post = require("./models/Post"); const { MONGODB } = require("./config");
const typeDefs = require("./graphql/typeDefs"); const typeDefs = require("./graphql/typeDefs");
const resolvers = require("./graphql/resolvers"); const resolvers = require("./graphql/resolvers");
...@@ -13,7 +13,7 @@ const server = new ApolloServer({ ...@@ -13,7 +13,7 @@ const server = new ApolloServer({
}); });
mongoose mongoose
.connect("mongodb://test:A12345678@ds119820.mlab.com:19820/merng", { .connect(MONGODB, {
useNewUrlParser: true useNewUrlParser: true
}) })
.then(() => { .then(() => {
......
...@@ -8,7 +8,9 @@ ...@@ -8,7 +8,9 @@
}, },
"dependencies": { "dependencies": {
"apollo-server": "^2.9.3", "apollo-server": "^2.9.3",
"bcryptjs": "^2.4.3",
"graphql": "^14.5.4", "graphql": "^14.5.4",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.6.13" "mongoose": "^5.6.13"
}, },
"devDependencies": { "devDependencies": {
......
module.exports.validateRegisterInput = (
username,
email,
password,
confirmPassword
) => {
const errors = {};
if (username.trim() === "") {
errors.username = "Username must not be empty";
}
if (email.trim() === "") {
errors.email = "Email must not be empty";
} else {
const regEx = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
if (!email.match(regEx)) {
errors.email = "Email must be a valid email address";
}
}
if (password.trim() === "") {
errors.password = "Password must not be empty";
} else if (password !== confirmPassword) {
errors.confirmPassword = "Passwords must match";
}
return {
errors,
valid: Object.keys(errors).length < 1
};
};
module.exports.validateLoginInput = (username, password) => {
const errors = {};
if (username.trim() === "") {
errors.username = "Username must not be empty";
}
if (password.trim() === "") {
errors.password = "Password must not be empty";
}
return {
errors,
valid: Object.keys(errors).length < 1
};
};
...@@ -460,6 +460,10 @@ base@^0.11.1: ...@@ -460,6 +460,10 @@ base@^0.11.1:
mixin-deep "^1.2.0" mixin-deep "^1.2.0"
pascalcase "^0.1.1" pascalcase "^0.1.1"
bcryptjs@^2.4.3:
version "2.4.3"
resolved "https://registry.npm.taobao.org/bcryptjs/download/bcryptjs-2.4.3.tgz#9ab5627b93e60621ff7cdac5da9733027df1d0cb"
binary-extensions@^1.0.0: binary-extensions@^1.0.0:
version "1.13.1" version "1.13.1"
resolved "https://registry.npm.taobao.org/binary-extensions/download/binary-extensions-1.13.1.tgz#598afe54755b2868a5330d2aff9d4ebb53209b65" resolved "https://registry.npm.taobao.org/binary-extensions/download/binary-extensions-1.13.1.tgz#598afe54755b2868a5330d2aff9d4ebb53209b65"
...@@ -521,6 +525,10 @@ bson@^1.1.1, bson@~1.1.1: ...@@ -521,6 +525,10 @@ bson@^1.1.1, bson@~1.1.1:
version "1.1.1" version "1.1.1"
resolved "https://registry.npm.taobao.org/bson/download/bson-1.1.1.tgz#4330f5e99104c4e751e7351859e2d408279f2f13" resolved "https://registry.npm.taobao.org/bson/download/bson-1.1.1.tgz#4330f5e99104c4e751e7351859e2d408279f2f13"
buffer-equal-constant-time@1.0.1:
version "1.0.1"
resolved "https://registry.npm.taobao.org/buffer-equal-constant-time/download/buffer-equal-constant-time-1.0.1.tgz#f8e71132f7ffe6e01a5c9697a4c6f3e48d5cc819"
busboy@^0.3.1: busboy@^0.3.1:
version "0.3.1" version "0.3.1"
resolved "https://registry.npm.taobao.org/busboy/download/busboy-0.3.1.tgz#170899274c5bf38aae27d5c62b71268cd585fd1b" resolved "https://registry.npm.taobao.org/busboy/download/busboy-0.3.1.tgz#170899274c5bf38aae27d5c62b71268cd585fd1b"
...@@ -786,6 +794,12 @@ duplexer3@^0.1.4: ...@@ -786,6 +794,12 @@ duplexer3@^0.1.4:
version "0.1.4" version "0.1.4"
resolved "https://registry.npm.taobao.org/duplexer3/download/duplexer3-0.1.4.tgz#ee01dd1cac0ed3cbc7fdbea37dc0a8f1ce002ce2" resolved "https://registry.npm.taobao.org/duplexer3/download/duplexer3-0.1.4.tgz#ee01dd1cac0ed3cbc7fdbea37dc0a8f1ce002ce2"
ecdsa-sig-formatter@1.0.11:
version "1.0.11"
resolved "https://registry.npm.taobao.org/ecdsa-sig-formatter/download/ecdsa-sig-formatter-1.0.11.tgz#ae0f0fa2d85045ef14a817daa3ce9acd0489e5bf"
dependencies:
safe-buffer "^5.0.1"
ee-first@1.1.1: ee-first@1.1.1:
version "1.1.1" version "1.1.1"
resolved "https://registry.npm.taobao.org/ee-first/download/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d" resolved "https://registry.npm.taobao.org/ee-first/download/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"
...@@ -1389,6 +1403,36 @@ iterall@^1.1.3, iterall@^1.2.1, iterall@^1.2.2: ...@@ -1389,6 +1403,36 @@ iterall@^1.1.3, iterall@^1.2.1, iterall@^1.2.2:
version "1.2.2" version "1.2.2"
resolved "https://registry.npm.taobao.org/iterall/download/iterall-1.2.2.tgz#92d70deb8028e0c39ff3164fdbf4d8b088130cd7" resolved "https://registry.npm.taobao.org/iterall/download/iterall-1.2.2.tgz#92d70deb8028e0c39ff3164fdbf4d8b088130cd7"
jsonwebtoken@^8.5.1:
version "8.5.1"
resolved "https://registry.npm.taobao.org/jsonwebtoken/download/jsonwebtoken-8.5.1.tgz#00e71e0b8df54c2121a1f26137df2280673bcc0d"
dependencies:
jws "^3.2.2"
lodash.includes "^4.3.0"
lodash.isboolean "^3.0.3"
lodash.isinteger "^4.0.4"
lodash.isnumber "^3.0.3"
lodash.isplainobject "^4.0.6"
lodash.isstring "^4.0.1"
lodash.once "^4.0.0"
ms "^2.1.1"
semver "^5.6.0"
jwa@^1.4.1:
version "1.4.1"
resolved "https://registry.npm.taobao.org/jwa/download/jwa-1.4.1.tgz#743c32985cb9e98655530d53641b66c8645b039a"
dependencies:
buffer-equal-constant-time "1.0.1"
ecdsa-sig-formatter "1.0.11"
safe-buffer "^5.0.1"
jws@^3.2.2:
version "3.2.2"
resolved "https://registry.npm.taobao.org/jws/download/jws-3.2.2.tgz#001099f3639468c9414000e99995fa52fb478304"
dependencies:
jwa "^1.4.1"
safe-buffer "^5.0.1"
kareem@2.3.1: kareem@2.3.1:
version "2.3.1" version "2.3.1"
resolved "https://registry.npm.taobao.org/kareem/download/kareem-2.3.1.tgz#def12d9c941017fabfb00f873af95e9c99e1be87" resolved "https://registry.npm.taobao.org/kareem/download/kareem-2.3.1.tgz#def12d9c941017fabfb00f873af95e9c99e1be87"
...@@ -1419,6 +1463,34 @@ latest-version@^3.0.0: ...@@ -1419,6 +1463,34 @@ latest-version@^3.0.0:
dependencies: dependencies:
package-json "^4.0.0" package-json "^4.0.0"
lodash.includes@^4.3.0:
version "4.3.0"
resolved "https://registry.npm.taobao.org/lodash.includes/download/lodash.includes-4.3.0.tgz#60bb98a87cb923c68ca1e51325483314849f553f"
lodash.isboolean@^3.0.3:
version "3.0.3"
resolved "https://registry.npm.taobao.org/lodash.isboolean/download/lodash.isboolean-3.0.3.tgz#6c2e171db2a257cd96802fd43b01b20d5f5870f6"
lodash.isinteger@^4.0.4:
version "4.0.4"
resolved "https://registry.npm.taobao.org/lodash.isinteger/download/lodash.isinteger-4.0.4.tgz#619c0af3d03f8b04c31f5882840b77b11cd68343"
lodash.isnumber@^3.0.3:
version "3.0.3"
resolved "https://registry.npm.taobao.org/lodash.isnumber/download/lodash.isnumber-3.0.3.tgz#3ce76810c5928d03352301ac287317f11c0b1ffc"
lodash.isplainobject@^4.0.6:
version "4.0.6"
resolved "https://registry.npm.taobao.org/lodash.isplainobject/download/lodash.isplainobject-4.0.6.tgz#7c526a52d89b45c45cc690b88163be0497f550cb"
lodash.isstring@^4.0.1:
version "4.0.1"
resolved "https://registry.npm.taobao.org/lodash.isstring/download/lodash.isstring-4.0.1.tgz#d527dfb5456eca7cc9bb95d5daeaf88ba54a5451"
lodash.once@^4.0.0:
version "4.1.1"
resolved "https://registry.npm.taobao.org/lodash.once/download/lodash.once-4.1.1.tgz#0dd3971213c7c56df880977d504c88fb471a97ac"
lodash.sortby@^4.7.0: lodash.sortby@^4.7.0:
version "4.7.0" version "4.7.0"
resolved "https://registry.npm.taobao.org/lodash.sortby/download/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438" resolved "https://registry.npm.taobao.org/lodash.sortby/download/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"
...@@ -2041,7 +2113,7 @@ semver-diff@^2.0.0: ...@@ -2041,7 +2113,7 @@ semver-diff@^2.0.0:
dependencies: dependencies:
semver "^5.0.3" semver "^5.0.3"
semver@^5.0.3, semver@^5.1.0, semver@^5.3.0, semver@^5.5.0: semver@^5.0.3, semver@^5.1.0, semver@^5.3.0, semver@^5.5.0, semver@^5.6.0:
version "5.7.1" version "5.7.1"
resolved "https://registry.npm.taobao.org/semver/download/semver-5.7.1.tgz#a954f931aeba508d307bbf069eff0c01c96116f7" resolved "https://registry.npm.taobao.org/semver/download/semver-5.7.1.tgz#a954f931aeba508d307bbf069eff0c01c96116f7"
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!