Toggle navigation
Toggle navigation
This project
Loading...
Sign in
황선혁
/
weather_chatbot
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
Eric Whale
2022-05-24 12:10:35 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
07c49a4fd5b3c83b790e4baa9ffdde0074860767
07c49a4f
1 parent
6c619408
Add signin, signup functionality
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
73 additions
and
9 deletions
actions/userActions.js
middleware/authMiddleware.js
middleware/errorMiddleware.js
package-lock.json
package.json
routes/userRoutes.js
server.js
actions/userActions.js
View file @
07c49a4
const
jwt
=
require
(
"jsonwebtoken"
);
const
bcrypt
=
require
(
"bcryptjs"
);
// handles "exception" inside of async express routes
// (Used for mongoDB error in this project)
const
asyncHandler
=
require
(
"express-async-handler"
);
const
User
=
require
(
"../models/userModel"
);
// @desc Signup new user
// @route POST /api/users
// @access Public
const
signupUser
=
asyncHandler
(
async
(
req
,
res
)
=>
{
const
{
username
,
email
,
password
}
=
req
.
body
;
if
(
!
name
||
!
email
||
!
password
)
{
if
(
!
user
name
||
!
email
||
!
password
)
{
res
.
status
(
400
);
throw
new
Error
(
"Please fill in all fields"
);
}
// Check if user already exists
const
userExists
=
await
User
.
findOne
({
email
});
if
(
userExists
)
{
res
.
status
(
400
);
throw
new
Error
(
"User with the email already exists"
);
}
// Hash password (bcrypt)
const
salt
=
await
bcrypt
.
genSalt
(
10
);
const
hashedPassword
=
await
bcrypt
.
hash
(
password
,
salt
);
// Create/Build user
const
user
=
await
User
.
create
({
username
,
email
,
password
:
hashedPassword
,
});
// Send response
if
(
user
)
{
// 201: Resource successfully created
res
.
status
(
201
).
json
({
_id
:
user
.
id
,
username
:
user
.
username
,
email
:
user
.
email
,
// TODO: Add token!
});
}
else
{
res
.
status
(
400
);
throw
new
Error
(
"Invalid user data"
);
}
});
// @desc Login user
// @route POST /api/users/login
// @access Public
const
loginUser
=
asyncHandler
(
async
(
req
,
res
)
=>
{
const
{
email
,
password
}
=
req
.
body
;
// Check for the user email
// Send response
// Check email & password
const
userInDB
=
await
User
.
findOne
({
email
});
const
validPassword
=
await
bcrypt
.
compare
(
password
,
userInDB
.
password
);
if
(
userInDB
&&
validPassword
)
{
res
.
status
(
200
).
json
({
_id
:
userInDB
.
id
,
username
:
userInDB
.
username
,
email
:
userInDB
.
email
,
// TODO: Add Token!
});
}
else
{
res
.
status
(
400
);
throw
new
Error
(
"Invalid credentials"
);
}
});
// @desc Get user(only self)
// @route GET /api/users/self
// @access Private
const
getSelf
=
asyncHandler
(
async
(
req
,
res
)
=>
{});
module
.
exports
=
{
...
...
middleware/authMiddleware.js
0 → 100644
View file @
07c49a4
const
jwt
=
require
(
"jsonwebtoken"
);
const
authHandler
=
(
err
,
req
,
res
,
next
)
=>
{
next
();
};
module
.
exports
=
{
authHandler
};
middleware/errorMiddleware.js
View file @
07c49a4
...
...
@@ -4,7 +4,7 @@ const errorHandler = (err, req, res, next) => {
res
.
status
(
statusCode
);
res
.
json
({
message
:
err
.
message
,
// stack from mongoDB
(maybe...)
// stack from mongoDB
TODO: Check it!
stack
:
process
.
env
.
NODE_ENV
===
"production"
?
null
:
err
.
stack
,
});
};
...
...
package-lock.json
View file @
07c49a4
...
...
@@ -10,6 +10,7 @@
"license"
:
"MIT"
,
"dependencies"
:
{
"bcryptjs"
:
"^2.4.3"
,
"colors"
:
"^1.4.0"
,
"dotenv"
:
"^16.0.1"
,
"express"
:
"^4.18.1"
,
"express-async-handler"
:
"^1.2.0"
,
...
...
@@ -457,6 +458,14 @@
"integrity"
:
"sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA=="
,
"dev"
:
true
},
"node_modules/colors"
:
{
"version"
:
"1.4.0"
,
"resolved"
:
"https://registry.npmjs.org/colors/-/colors-1.4.0.tgz"
,
"integrity"
:
"sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA=="
,
"engines"
:
{
"node"
:
">=0.1.90"
}
},
"node_modules/concat-map"
:
{
"version"
:
"0.0.1"
,
"resolved"
:
"https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz"
,
...
...
@@ -2591,6 +2600,11 @@
"integrity"
:
"sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA=="
,
"dev"
:
true
},
"colors"
:
{
"version"
:
"1.4.0"
,
"resolved"
:
"https://registry.npmjs.org/colors/-/colors-1.4.0.tgz"
,
"integrity"
:
"sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA=="
},
"concat-map"
:
{
"version"
:
"0.0.1"
,
"resolved"
:
"https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz"
,
...
...
package.json
View file @
07c49a4
...
...
@@ -5,7 +5,7 @@
"main"
:
"server.js"
,
"scripts"
:
{
"start"
:
"node server.js"
,
"
dev
"
:
"nodemon server.js"
"
server
"
:
"nodemon server.js"
},
"repository"
:
{
"type"
:
"git"
,
...
...
@@ -15,6 +15,7 @@
"license"
:
"MIT"
,
"dependencies"
:
{
"bcryptjs"
:
"^2.4.3"
,
"colors"
:
"^1.4.0"
,
"dotenv"
:
"^16.0.1"
,
"express"
:
"^4.18.1"
,
"express-async-handler"
:
"^1.2.0"
,
...
...
routes/userRoutes.js
View file @
07c49a4
const
express
=
require
(
"express"
);
const
router
=
express
.
Router
();
const
{
signupUser
,
loginUser
}
=
require
(
"../actions/userActions"
);
const
{
signupUser
,
loginUser
,
getSelf
}
=
require
(
"../actions/userActions"
);
router
.
post
(
"/"
,
signupUser
);
router
.
post
(
"/login"
,
loginUser
);
router
.
get
(
"/self"
,
getSelf
);
module
.
exports
=
router
;
...
...
server.js
View file @
07c49a4
...
...
@@ -2,7 +2,7 @@ const express = require("express");
const
dotenv
=
require
(
"dotenv"
).
config
();
const
{
errorHandler
}
=
require
(
"./middleware/errorMiddleware"
);
const
connectDB
=
require
(
"./config/db"
);
const
port
=
process
.
env
.
PORT
||
8
000
;
const
port
=
process
.
env
.
PORT
||
6
000
;
connectDB
();
const
app
=
express
();
...
...
Please
register
or
login
to post a comment