Skip to content

Commit ef0d7f3

Browse files
Merge remote-tracking branch 'origin/features/google_auth'
2 parents f5001c0 + 8c72731 commit ef0d7f3

File tree

12 files changed

+374
-16
lines changed

12 files changed

+374
-16
lines changed

.env.example

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
1-
DB_HOST =
2-
SECRET_KEY =
1+
DB_HOST =
2+
SECRET_KEY =
3+
4+
PORT =
5+
BASE_URL =
6+
FRONTEND_URL =
7+
GOOGLE_CLIENT_ID =
8+
GOOGLE_CLIENT_SECRET =

app.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
const express = require ("express");
1+
const express = require("express");
2+
3+
const path = require("path");
24

35
const logger = require("morgan");
46
const cors = require("cors");
57
const swaggerUi = require('swagger-ui-express');
68
const swaggerDocument = require('./swagger.json');
79

8-
require('dotenv').config();
10+
// require('dotenv').config();
11+
require("dotenv").config({ path: path.join(__dirname, "./.env") });
912

1013
const authRouter = require('./routes/api/auth');
1114
const booksRouter = require("./routes/api/books");
@@ -19,9 +22,16 @@ app.use(logger(formatsLogger));
1922
app.use(cors());
2023
app.use(express.json());
2124

25+
app.use(express.static("public"))
26+
2227
app.use('/api/users', authRouter);
28+
app.use('/link', (req, res) => {
29+
res.sendFile(path.join(__dirname, "./public/link.html"));
30+
});
2331
app.use("/api/books", booksRouter);
2432
app.use("/api/training", statsRouter);
33+
// app.use("/api/stats", statsRouter);
34+
2535
app.use('/', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
2636

2737
app.use((req, res) => {
@@ -33,4 +43,4 @@ app.use((req, res) => {
3343
res.status(status).json({ message });
3444
});
3545

36-
module.exports = app;
46+
module.exports = app;

controllers/auth/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ const signup = require('./signup');
22
const login = require('./login');
33
const logout = require('./logout');
44
const currentUser = require('./currentUser');
5+
const signupEndLogin = require('./signupEndLogin')
56

67
module.exports = {
78
signup,
89
login,
910
logout,
1011
currentUser,
12+
signupEndLogin,
1113
}

controllers/auth/login.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ const { User } = require('../../models/user');
77
const { SECRET_KEY } = process.env;
88

99
const login = async (req, res) => {
10-
const { email, password, } = req.body;
10+
let { email, password, } = req.body;
11+
email = email.toLowerCase();
1112
const user = await User.findOne({ email });
1213
if (!user) {
1314
throw RequestError(401, "Email not found");
@@ -16,16 +17,16 @@ const login = async (req, res) => {
1617
if (!comparePassword) {
1718
throw RequestError(401, "Password wrong");
1819
}
19-
// if(!user.verify) {
20-
// throw RequestError(403, "Email not verify");
21-
// }
20+
2221
const peyload = {
2322
id: user._id
2423
}
2524
const token = jwt.sign(peyload, SECRET_KEY, { expiresIn: "24h" });
2625
await User.findByIdAndUpdate(user._id, { token });
26+
2727
res.json({
2828
token,
29+
name: user.name,
2930
});
3031
};
3132

controllers/auth/signup.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ const { User } = require('../../models/user');
44
const { RequestError } = require('../../helpers/index');
55

66
const signup = async (req, res) => {
7-
const { name, email, password } = req.body;
7+
let { name, email, password } = req.body;
8+
email = email.toLowerCase();
89
const user = await User.findOne({ email });
910
if (user) {
1011
throw RequestError(409, "Email already exist");
1112
}
1213
const hashPassword = await bcrypt.hash(password, 10);
14+
1315
const result = await User.create({ name, email, password: hashPassword });
1416
res.status(201).json({
1517
name: result.name,

controllers/auth/signupEndLogin.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const bcrypt = require('bcryptjs');
2+
const jwt = require('jsonwebtoken');
3+
4+
const { User } = require('../../models/user');
5+
const { RequestError } = require('../../helpers/index');
6+
const { SECRET_KEY } = process.env;
7+
8+
const signupEndLogin = async (req, res) => {
9+
let { name, email, password } = req.body;
10+
// SINGUP *********************************************
11+
email = email.toLowerCase();
12+
let user = await User.findOne({ email });
13+
if (user) {
14+
throw RequestError(409, "Email already exist");
15+
}
16+
const hashPassword = await bcrypt.hash(password, 10);
17+
18+
const result = await User.create({ name, email, password: hashPassword });
19+
20+
// LOGIN *********************************************
21+
user = await User.findOne({ email });
22+
const peyload = {
23+
id: user._id
24+
}
25+
const token = jwt.sign(peyload, SECRET_KEY, { expiresIn: "24h" });
26+
await User.findByIdAndUpdate(user._id, { token });
27+
28+
res.status(201).json({
29+
name: result.name,
30+
email: result.email,
31+
token,
32+
});
33+
}
34+
35+
module.exports = signupEndLogin;

models/user.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ const Joi = require('joi');
33

44
const { handleSchemaValidationErrors } = require("../helpers");
55

6-
const emailRegexp = /^([a-z0-9_-]+\.)*[a-z0-9_-]+@[a-z0-9_-]+(\.[a-z0-9_-]+)*\.[a-z]{2,6}$/;
7-
// const emailRegexp = /^[\w.]+@[\w]+.[\w]+$/;
6+
// const emailRegexp = /^([a-z0-9_-]+\.)*[a-z0-9_-]+@[a-z0-9_-]+(\.[a-z0-9_-]+)*\.[a-z]{2,6}$/;
7+
const emailRegexp = /^[\w.]+@[\w]+.[\w]+$/;
88

99
const userShema = new Schema({
1010
name: {
@@ -13,6 +13,8 @@ const userShema = new Schema({
1313
},
1414
email: {
1515
type: String,
16+
minlength: 6,
17+
maxLength: 63,
1618
required: [true, 'Email is required'],
1719
match: emailRegexp,
1820
unique: true,
@@ -34,13 +36,13 @@ userShema.post("save", handleSchemaValidationErrors);
3436
// ** Joi schemas ***************************************
3537
const singupSchema = Joi.object({
3638
name: Joi.string().required(),
37-
email: Joi.string().pattern(emailRegexp).required(),
38-
password: Joi.string().min(6).required(),
39+
email: Joi.string().pattern(emailRegexp).min(6).required(),
40+
password: Joi.string().min(6).max(6).required(),
3941
repeat_password: Joi.string().required().valid(Joi.ref('password')),
4042
})
4143

4244
const loginSchema = Joi.object({
43-
email: Joi.string().pattern(emailRegexp).required(),
45+
email: Joi.string().min(6).max(6).pattern(emailRegexp).required(),
4446
password: Joi.string().min(6).required(),
4547
})
4648

0 commit comments

Comments
 (0)