Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop/xxeol seminar2 #32

Open
wants to merge 2 commits into
base: develop/xxeol
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# soptConnector
# soptConnector

# pull request test
11 changes: 11 additions & 0 deletions back/nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"watch": [
"src",
".env"
],
"ext": "js,ts,json",
"ignore": [
"src/**/*.spec.ts"
],
"exec": "ts-node --transpile-only ./src/index.ts"
}
4 changes: 3 additions & 1 deletion back/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"main": "src/index.js",
"scripts": {
"dev": "ts-node src",
"build": "tsc && node dist"
"build": "tsc && node dist",
"test": "nodemon"
},
"dependencies": {
"dotenv": "^8.2.0",
Expand All @@ -18,6 +19,7 @@
"@types/express": "^4.17.11",
"@types/mongoose": "^5.10.4",
"@types/node": "^14.14.37",
"nodemon": "^2.0.7",
"ts-node": "^9.1.1",
"typescript": "^4.2.3"
},
Expand Down
87 changes: 87 additions & 0 deletions back/src/api/users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import express from "express";
import gravatar from "gravatar";
import jwt from "jsonwebtoken";
import bcrypt from "bcryptjs";
import config from "../config";
import { check, validationResult } from "express-validator";

const router = express.Router();

import User from "../models/User";

/**
* @route Post api/users
* @desc Register User
* @access Public
*/
router.post(
"/",
[
check("name", "Name is required").not().isEmpty(),
check("email", "Please include a valid email").isEmail(),
check(
"password",
"Please enter a password with 6 or more characters"
).isLength({ min: 6 }),
],
async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}

const { name, email, password } = req.body;

try {
// See if user exists
let user = await User.findOne({ email });

if (user) {
res.status(400).json({
errors: [{ msg: "User already exists" }],
});
}

// Get users gravatar
const avatar = gravatar.url(email, {
s: "200",
r: "pq",
d: "mm",
});

user = new User({
name,
email,
avatar,
password,
});

// Encrpyt password
const salt = await bcrypt.genSalt(10);
user.password = await bcrypt.hash(password, salt);

await user.save();

// Return jsonwebtoken
const payload = {
user: {
id: user.id,
},
};
jwt.sign(
payload,
config.jwtSecret,
{ expiresIn: 36000 },
(err, token) => {
if (err) throw err;
res.json({ token });
}
);
} catch (err) {
console.error(err.message);
res.status(500).send("Server Error");
}
}
);

module.exports = router;
8 changes: 8 additions & 0 deletions back/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import connectDB from "./Logger/db";
// Connect Database
connectDB();


app.use(express.json());

// Define Routes
app.use("/api/users", require("./api/users"));



// error handler
app.use(function (err, req, res, next) {
Expand All @@ -30,3 +36,5 @@ app
console.error(err);
process.exit(1);
});


14 changes: 14 additions & 0 deletions back/src/interfaces/IUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export interface IUser {
id: string;
name: string;
email: string;
password: string;
avatar: string;
date: Date;
}

export interface IUserInputDTO {
name: string;
email: string;
password: string;
}
27 changes: 27 additions & 0 deletions back/src/models/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import mongoose from "mongoose";
import { IUser } from "../interfaces/IUser";

const UserSchema = new mongoose.Schema({
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
unique: true,
},
password: {
type: String,
required: true,
},
avatar: {
type: String,
},
date: {
type: Date,
default: Date.now,
},
});

export default mongoose.model<IUser & mongoose.Document>("User", UserSchema);
18 changes: 18 additions & 0 deletions node_modules/.yarn-integrity

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions node_modules/@types/bcryptjs/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions node_modules/@types/bcryptjs/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

123 changes: 123 additions & 0 deletions node_modules/@types/bcryptjs/index.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading