Toggle navigation
Toggle navigation
This project
Loading...
Sign in
공태현
/
healthcare-with-webcam
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Graphs
Network
Create a new issue
Commits
Issue Boards
Authored by
공태현
2022-06-04 01:15:37 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
02533ad85090e5bba76f12178587b0443b9f2e8f
02533ad8
1 parent
f615ba68
Implement API for user register, login and logout
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
122 additions
and
2 deletions
.gitignore
mongodb.js → model/user.js
package-lock.json
package.json
server.js
.gitignore
0 → 100644
View file @
02533ad
package-lock.json
/node_modules/
\ No newline at end of file
mo
ngodb
.js
→
mo
del/user
.js
View file @
02533ad
const
{
json
}
=
require
(
'express/lib/response'
);
const
mongoose
=
require
(
'mongoose'
);
const
userSchema
=
new
mongoose
.
Schema
({
name
:
{
type
:
String
,
required
:
true
,
unique
:
true
,
},
password
:
{
type
:
String
,
required
:
true
,
trim
:
true
},
total_squart
:
{
type
:
Number
,
default
:
0
},
today_squart
:
{
type
:
Number
,
default
:
0
},
});
userSchema
.
methods
.
passwordCheck
=
function
(
password
,
cb
)
{
if
(
password
===
this
.
password
)
cb
(
null
,
isMatch
);
}
const
User
=
mongoose
.
model
(
'squartuser'
,
userSchema
)
module
.
exports
=
{
User
};
module
.
exports
=
mongoose
.
model
(
'squartuser'
,
userSchema
)
...
...
package-lock.json
View file @
02533ad
This diff is collapsed. Click to expand it.
package.json
View file @
02533ad
...
...
@@ -15,6 +15,8 @@
"license"
:
"ISC"
,
"dependencies"
:
{
"express"
:
"^4.18.1"
,
"mongoose"
:
"^6.3.4"
"express-session"
:
"^1.17.3"
,
"mongoose"
:
"^6.3.4"
,
"mongoose-session"
:
"0.0.4"
}
}
...
...
server.js
View file @
02533ad
const
express
=
require
(
'express'
);
const
app
=
express
();
const
port
=
3000
app
.
get
(
'/'
,
(
req
,
res
)
=>
{
res
.
send
(
"Hello World"
)
})
...
...
@@ -9,3 +14,102 @@ app.get('/', (req,res) => {
app
.
listen
(
port
,
()
=>
{
console
.
log
(
`Listening on
${
port
}
port`
);
})
// model/user.js
const
{
User
}
=
require
(
'./model/User'
);
const
mongoose
=
require
(
'mongoose'
);
// db 연결을 위한 키 값 , 보안을 위해 최종 마스터 브런치에는 포함하지 않을 예정.
mongoose
.
connect
(
'mongodb+srv://kongtae:ksas9825!%40@squartusers.e2ddc.mongodb.net/?retryWrites=true&w=majority'
)
.
then
(()
=>
console
.
log
(
'MongoDB connect!'
))
.
catch
(
err
=>
console
.
log
(
err
))
// 유저 등록 및 로그인 API
// 로그인 세션 : 로그인 정보 유지.
const
express_session
=
require
(
'express-session'
)
app
.
use
(
express_session
({
secret
:
"@secret@number"
,
// 암호화 키
resave
:
false
,
saveUninitialized
:
false
,
store
:
require
(
'mongoose-session'
)(
mongoose
),
cookie
:
{
maxAge
:
6
*
24
}
}))
// 등록 .
app
.
use
(
express
.
json
())
app
.
post
(
'/api/users/register'
,
(
req
,
res
)
=>
{
const
new_user
=
new
User
(
req
.
body
);
new_user
.
save
((
err
,
userInfo
)
=>
{
if
(
err
)
return
res
.
json
({
successs
:
false
,
err
})
return
res
.
status
(
200
).
json
({
success
:
true
})
})
})
// 로그인 .
app
.
post
(
'/api/users/login'
,
(
req
,
res
)
=>
{
User
.
findOne
({
name
:
req
.
body
.
name
},
(
err
,
user
)
=>
{
if
(
!
user
)
{
return
res
.
json
({
loginSuccess
:
false
,
message
:
"이름이 일치하는 사용자가 없습니다 !"
})
}
else
if
(
req
.
body
.
password
===
user
.
password
)
{
req
.
session
.
user
=
{
user_name
:
req
.
body
.
name
,
user_password
:
req
.
body
.
password
,
}
console
.
log
(
req
.
session
.
user
)
return
res
.
json
({
loginSuccess
:
true
,
})
}
else
{
return
res
.
json
({
loginSuccess
:
false
,
message
:
"비밀번호가 일치하지 않습니다 !"
})
}
})
})
// 로그아웃
app
.
get
(
'/api/users/logout'
,
(
req
,
res
)
=>
{
var
session
=
req
.
session
if
(
session
.
user
)
{
req
.
session
.
destroy
(
err
=>
{
if
(
err
)
{
console
.
log
(
err
)
return
res
.
json
({
logoutSuccess
:
false
})
}
else
{
console
.
log
(
'로그아웃 완료'
)
return
res
.
json
({
logoutSuccess
:
true
})
}
})
// res.redirect('/');
}
else
{
console
.
log
(
'로그인이 되어있지 않습니다.'
)
return
res
.
json
({
logoutSuccess
:
true
,
})
}
})
// 세션 저장 확인
app
.
get
(
'/api/session'
,
(
req
,
res
)
=>
{
console
.
log
(
req
.
session
.
user
)
return
res
.
json
({
session
:
req
.
session
})
})
...
...
Please
register
or
login
to post a comment