공태현

Implement API for user register, login and logout

1 +package-lock.json
2 +/node_modules/
...\ No newline at end of file ...\ No newline at end of file
1 +const { json } = require('express/lib/response');
1 const mongoose = require('mongoose'); 2 const mongoose = require('mongoose');
2 3
3 const userSchema = new mongoose.Schema({ 4 const userSchema = new mongoose.Schema({
4 name : {type : String, required : true, unique : true, }, 5 name : {type : String, required : true, unique : true, },
5 password : {type : String, required : true, trim : true}, 6 password : {type : String, required : true, trim : true},
7 + total_squart : {type : Number, default : 0},
8 + today_squart : {type : Number, default : 0},
9 +
6 }); 10 });
7 11
12 +userSchema.methods.passwordCheck = function(password, cb) {
13 + if (password === this.password)
14 + cb(null, isMatch);
15 +}
16 +
17 +
18 +
19 +const User = mongoose.model('squartuser', userSchema )
20 +module.exports = {User};
8 21
9 -module.exports = mongoose.model('squartuser', userSchema )
10 22
......
This diff is collapsed. Click to expand it.
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
15 "license": "ISC", 15 "license": "ISC",
16 "dependencies": { 16 "dependencies": {
17 "express": "^4.18.1", 17 "express": "^4.18.1",
18 - "mongoose": "^6.3.4" 18 + "express-session": "^1.17.3",
19 + "mongoose": "^6.3.4",
20 + "mongoose-session": "0.0.4"
19 } 21 }
20 } 22 }
......
1 const express = require('express'); 1 const express = require('express');
2 +
3 +
2 const app = express(); 4 const app = express();
5 +
6 +
3 const port = 3000 7 const port = 3000
4 8
9 +
5 app.get('/', (req,res) => { 10 app.get('/', (req,res) => {
6 res.send("Hello World") 11 res.send("Hello World")
7 }) 12 })
...@@ -9,3 +14,102 @@ app.get('/', (req,res) => { ...@@ -9,3 +14,102 @@ app.get('/', (req,res) => {
9 app.listen(port, () => { 14 app.listen(port, () => {
10 console.log(`Listening on ${port} port`); 15 console.log(`Listening on ${port} port`);
11 }) 16 })
17 +
18 +// model/user.js
19 +const { User } = require('./model/User');
20 +const mongoose = require('mongoose');
21 +// db 연결을 위한 키 값 , 보안을 위해 최종 마스터 브런치에는 포함하지 않을 예정.
22 +mongoose.connect('mongodb+srv://kongtae:ksas9825!%40@squartusers.e2ddc.mongodb.net/?retryWrites=true&w=majority')
23 +.then(() => console.log('MongoDB connect!'))
24 +.catch(err => console.log(err))
25 +
26 +// 유저 등록 및 로그인 API
27 +
28 +// 로그인 세션 : 로그인 정보 유지.
29 +const express_session = require('express-session')
30 +app.use(express_session({
31 + secret : "@secret@number", // 암호화 키
32 + resave : false,
33 + saveUninitialized : false,
34 + store:require('mongoose-session')(mongoose),
35 + cookie : {maxAge : 6*24}
36 +}))
37 +
38 +
39 +// 등록 .
40 +app.use(express.json())
41 +app.post('/api/users/register', (req,res) => {
42 + const new_user = new User(req.body);
43 + new_user.save((err, userInfo) => {
44 + if (err) return res.json({ successs : false, err})
45 + return res.status(200).json({
46 + success : true
47 + })
48 + })
49 +})
50 +
51 +// 로그인 .
52 +app.post('/api/users/login', (req ,res) => {
53 + User.findOne({name : req.body.name}, (err, user) => {
54 + if (!user) {
55 + return res.json({
56 + loginSuccess: false,
57 + message : "이름이 일치하는 사용자가 없습니다 !"
58 + })
59 + }
60 + else if (req.body.password === user.password) {
61 + req.session.user = {
62 + user_name : req.body.name,
63 + user_password : req.body.password,
64 + }
65 + console.log(req.session.user)
66 + return res.json({
67 + loginSuccess : true,
68 + })
69 + }
70 + else {
71 + return res.json({
72 + loginSuccess : false,
73 + message : "비밀번호가 일치하지 않습니다 !"
74 + })
75 + }
76 + })
77 +})
78 +
79 +// 로그아웃
80 +app.get('/api/users/logout', (req,res) => {
81 + var session = req.session
82 + if (session.user)
83 + {
84 + req.session.destroy(err => {
85 + if (err) {
86 + console.log(err)
87 + return res.json({
88 + logoutSuccess : false
89 + })
90 + }
91 + else
92 + {
93 + console.log('로그아웃 완료')
94 + return res.json({
95 + logoutSuccess : true
96 + })
97 + }
98 + })
99 + // res.redirect('/');
100 + }
101 + else
102 + {
103 + console.log('로그인이 되어있지 않습니다.')
104 + return res.json({
105 + logoutSuccess : true,
106 + })
107 + }
108 +
109 +})
110 +
111 +// 세션 저장 확인
112 +app.get('/api/session', (req,res) => {
113 + console.log(req.session.user)
114 + return res.json({session :req.session})
115 +})
......