Toggle navigation
Toggle navigation
This project
Loading...
Sign in
정민우
/
vps_service
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
정민우
2021-05-13 15:19:30 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
f542616fb5f32c6f4019716fdaccbdd0ab63a47f
f542616f
1 parent
9711314c
[New] User Upsert 구현
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
179 additions
and
5 deletions
backend/.env.example
backend/app.js
backend/controllers/authController.js
backend/controllers/userController.js
backend/routes/authRouter.js
backend/routes/userRouter.js
backend/.env.example
View file @
f542616
...
...
@@ -5,3 +5,6 @@ MYSQL_DATABASE=root
MYSQL_HOST=127.0.0.1
MYSQL_PORT=3306
CORS_ALLOWED_URL=
INITIAL_ADMIN_ID=
INITIAL_ADMIN_PW=
JWT_KEY=
...
...
backend/app.js
View file @
f542616
...
...
@@ -5,6 +5,7 @@ var corsConfig = require('./config/cors')
const
indexRouter
=
require
(
'./routes/index'
)
const
authRouter
=
require
(
'./routes/authRouter'
)
const
userRouter
=
require
(
'./routes/userRouter'
)
let
app
=
express
()
app
.
use
(
cors
(
corsConfig
))
...
...
@@ -15,6 +16,7 @@ app.use(cookieParser())
app
.
use
(
'/'
,
indexRouter
)
app
.
use
(
'/'
,
authRouter
)
app
.
use
(
'/'
,
userRouter
)
app
.
use
(
function
(
req
,
res
)
{
res
.
status
(
400
)
...
...
backend/controllers/authController.js
View file @
f542616
...
...
@@ -83,10 +83,6 @@ exports.userInfo = async (req, res) => {
}
}
exports
.
adminTest
=
async
(
req
,
res
)
=>
{
return
sendResponse
(
res
,
"Just Test"
,
200
,
"Test OK"
)
}
exports
.
isAdmin
=
async
(
userId
)
=>
{
const
user
=
await
User
.
findByPk
(
userId
)
return
(
user
&&
await
user
.
isAdmin
)
...
...
backend/controllers/userController.js
View file @
f542616
const
bcrypt
=
require
(
'bcrypt'
)
const
{
sequelize
,
Sequelize
,
User
,
Group
}
=
require
(
'../models'
)
const
{
sendResponse
,
sendError
}
=
require
(
'../utils/response'
)
const
{
logging
}
=
require
(
'../utils/log'
)
const
{
checkRequiredExist
,
setValues
}
=
require
(
'../utils/validation'
)
const
Op
=
Sequelize
.
Op
exports
.
userRegister
=
async
(
req
,
res
)
=>
{
try
{
let
registerData
=
setValues
(
req
.
body
,
[
'userId'
,
'password'
,
'name'
,
'phone'
,
'email'
])
if
(
typeof
registerData
.
password
!==
'undefined'
&&
registerData
.
password
!==
null
)
{
registerData
.
password
=
await
bcrypt
.
hash
(
registerData
.
password
,
10
)
}
if
(
typeof
registerData
.
userId
!==
'undefined'
&&
registerData
.
userId
!==
null
)
{
let
userIdDuplicated
=
await
User
.
count
({
where
:
{
userId
:
registerData
.
userId
},
})
if
(
userIdDuplicated
>
0
)
{
logging
(
'user'
,
'error'
,
{
code
:
400
,
message
:
'duplicatedUserId'
},
req
)
return
sendError
(
res
,
400
,
'duplicatedUserId'
)
}
}
let
user
=
await
User
.
create
(
registerData
)
logging
(
'user'
,
'register'
,
user
,
req
)
return
sendResponse
(
res
,
user
,
200
)
}
catch
(
error
)
{
logging
(
'user'
,
'error'
,
{
code
:
500
,
message
:
error
.
message
},
req
)
return
sendError
(
res
,
500
,
error
.
message
)
}
}
exports
.
userUpdate
=
async
(
req
,
res
)
=>
{
const
id
=
req
.
decoded
.
id
if
(
!
id
)
{
return
sendError
(
res
,
401
,
'InvalidToken'
)
}
try
{
let
updateData
=
setValues
(
req
.
body
,
[
'password'
,
'name'
,
'phone'
,
'email'
])
if
(
typeof
updateData
.
password
!==
'undefined'
&&
updateData
.
password
!==
null
)
{
updateData
.
password
=
await
bcrypt
.
hash
(
updateData
.
password
,
10
)
}
let
logdata
=
{
}
let
user
=
await
User
.
findByPk
(
id
)
if
(
!
user
)
{
logging
(
'user'
,
'error'
,
{
code
:
404
,
message
:
'NoUserFound'
},
req
)
return
sendError
(
res
,
404
,
'NoUserFound'
)
}
logdata
.
before
=
{
user
:
user
}
await
user
.
update
(
updateData
)
logdata
.
after
=
{
user
:
user
}
logging
(
'user'
,
'update'
,
user
,
req
)
return
sendResponse
(
res
,
user
,
200
)
}
catch
(
error
)
{
logging
(
'user'
,
'error'
,
{
code
:
500
,
message
:
error
.
message
},
req
)
return
sendError
(
res
,
500
,
error
.
message
)
}
}
exports
.
adminUserList
=
async
(
req
,
res
)
=>
{
try
{
let
users
=
await
User
.
findAndCountAll
({
where
:
{
id
:
{
[
Op
.
ne
]:
1
}
},
order
:
[
[
'createdAt'
,
'desc'
]
]
})
const
result
=
{
count
:
users
.
count
,
data
:
users
.
rows
}
return
sendResponse
(
res
,
result
,
200
)
}
catch
(
error
)
{
logging
(
'user'
,
'error'
,
{
code
:
500
,
message
:
error
.
message
},
req
)
return
sendError
(
res
,
500
,
error
.
message
)
}
}
exports
.
adminUserInfo
=
async
(
req
,
res
)
=>
{
const
requiredKey
=
[
'id'
]
const
required
=
checkRequiredExist
(
req
.
body
,
requiredKey
)
if
(
required
)
{
logging
(
'user'
,
'error'
,
{
code
:
400
,
message
:
`missingKey:
${
required
}
`
},
req
)
return
sendError
(
res
,
400
,
`missingKey:
${
required
}
`
)
}
const
userId
=
req
.
body
.
id
try
{
const
user
=
await
User
.
findByPk
(
userId
)
if
(
!
user
)
{
logging
(
'user'
,
'error'
,
{
code
:
404
,
message
:
'NoUserFound'
},
req
)
return
sendError
(
res
,
404
,
'NoUserFound'
)
}
return
sendResponse
(
res
,
user
,
200
)
}
catch
(
error
)
{
return
sendError
(
res
,
500
,
error
.
message
)
}
}
exports
.
adminUserUpsert
=
async
(
req
,
res
)
=>
{
const
id
=
req
.
body
.
id
try
{
let
updatedData
=
setValues
(
req
.
body
,
[
'userId'
,
'password'
,
'name'
,
'phone'
,
'email'
,
'isAdmin'
])
if
(
typeof
updatedData
.
password
!==
'undefined'
&&
updatedData
.
password
!==
null
)
{
updatedData
.
password
=
await
bcrypt
.
hash
(
updatedData
.
password
,
10
)
}
if
(
typeof
updatedData
.
userId
!==
'undefined'
&&
updatedData
.
userId
!==
null
)
{
let
userIdDuplicated
=
await
User
.
count
({
where
:
{
id
:
{
[
Op
.
ne
]:
id
},
userId
:
updatedData
.
userId
},
})
if
(
userIdDuplicated
>
0
)
{
logging
(
'user'
,
'error'
,
{
code
:
400
,
message
:
'duplicatedUserId'
},
req
)
return
sendError
(
res
,
400
,
'duplicatedUserId'
)
}
}
let
logdata
=
{
}
let
user
=
null
if
(
id
)
{
user
=
await
User
.
findByPk
(
id
)
if
(
!
user
)
{
logging
(
'user'
,
'error'
,
{
code
:
404
,
message
:
'NoUserFound'
},
req
)
return
sendError
(
res
,
404
,
'NoUserFound'
)
}
logdata
.
before
=
{
user
:
user
}
await
user
.
update
(
updatedData
)
}
else
{
user
=
await
User
.
create
(
updatedData
)
logdata
.
before
=
null
}
logdata
.
after
=
{
user
:
user
}
logging
(
'user'
,
'update'
,
logdata
,
req
)
return
sendResponse
(
res
,
user
,
200
)
}
catch
(
error
)
{
logging
(
'user'
,
'error'
,
{
code
:
500
,
message
:
error
.
message
},
req
)
return
sendError
(
res
,
500
,
error
.
message
)
}
}
...
...
backend/routes/authRouter.js
View file @
f542616
...
...
@@ -6,6 +6,5 @@ const { guestOnly, memberOnly, adminOnly } = require ('../middlewares/auth')
router
.
post
(
'/login'
,
guestOnly
,
authController
.
login
)
router
.
post
(
'/user/info'
,
memberOnly
,
authController
.
userInfo
)
router
.
post
(
'/admin_test'
,
adminOnly
,
authController
.
adminTest
)
module
.
exports
=
router
...
...
backend/routes/userRouter.js
View file @
f542616
const
express
=
require
(
'express'
);
const
router
=
express
.
Router
();
const
{
guestOnly
,
memberOnly
,
adminOnly
}
=
require
(
'../middlewares/auth'
)
const
userController
=
require
(
'../controllers/userController'
)
router
.
post
(
'/user/register'
,
guestOnly
,
userController
.
userRegister
)
router
.
post
(
'/user/update'
,
memberOnly
,
userController
.
userUpdate
)
router
.
post
(
'/admin/user/list'
,
adminOnly
,
userController
.
adminUserList
)
router
.
post
(
'/admin/user/info'
,
adminOnly
,
userController
.
adminUserInfo
)
router
.
post
(
'/admin/user/upsert'
,
adminOnly
,
userController
.
adminUserUpsert
)
module
.
exports
=
router
...
...
Please
register
or
login
to post a comment