반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- 리액트
- react
- 정보처리기사
- 스프링
- 자바의정석
- 코딩테스트
- 정보처리기사실기
- Java의정석
- 정보처리기사정리
- 자바스크립트 코딩테스트
- 리액트네이티브
- 국비코딩
- 정보처리기사요약
- php
- 국비IT
- 정보처리기사실기정리
- 평일코딩
- 오라클
- VUE
- CSS
- spring
- javascript
- 이안의평일코딩
- Oracle
- 정보처리기사실기요약
- 자스코테
- 자바스크립트
- ReactNative
- 타입스크립트
- typescript
Archives
- Today
- Total
이안의 평일코딩
[node.js] boiler plate 로그인 & 회원가입 본문
반응형
server
index.js
const express = require("express");
const app = express();
const path = require("path");
const cors = require('cors')
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const config = require("./config/key");
// const mongoose = require("mongoose");
// mongoose
// .connect(config.mongoURI, { useNewUrlParser: true })
// .then(() => console.log("DB connected"))
// .catch(err => console.error(err));
const mongoose = require("mongoose");
const connect = mongoose.connect(config.mongoURI,
{
useNewUrlParser: true, useUnifiedTopology: true,
useCreateIndex: true, useFindAndModify: false
})
.then(() => console.log('MongoDB Connected...'))
.catch(err => console.log(err));
app.use(cors())
//to not get any deprecation warning or error
//support parsing of application/x-www-form-urlencoded post data
app.use(bodyParser.urlencoded({ extended: true }));
//to get json data
// support parsing of application/json type post data
app.use(bodyParser.json());
app.use(cookieParser());
app.use('/api/users', require('./routes/users'));
//use this to show the image you have in node js server to client (react js)
//https://stackoverflow.com/questions/48914987/send-image-path-from-node-js-express-server-to-react-client
app.use('/uploads', express.static('uploads'));
// Serve static assets if in production
if (process.env.NODE_ENV === "production") {
// Set static folder
// All the javascript and css files will be read and served from this folder
app.use(express.static("client/build"));
// index.html for all page routes html or routing and naviagtion
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "../client", "build", "index.html"));
});
}
const port = process.env.PORT || 5000
app.listen(port, () => {
console.log(`Server Listening on ${port}`)
});
server/config
key.js
if (process.env.NODE_ENV === 'production') {
module.exports = require('./prod');
} else {
module.exports = require('./dev');
}
prod.js
module.exports = {
mongoURI:process.env.MONGO_URI
}
server/middleware
auth.js
const { User } = require('../models/User');
let auth = (req, res, next) => {
let token = req.cookies.w_auth;
User.findByToken(token, (err, user) => {
if (err) throw err;
if (!user)
return res.json({
isAuth: false,
error: true
});
req.token = token;
req.user = user;
next();
});
};
module.exports = { auth };
server/models
User.js
const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const saltRounds = 10;
const jwt = require('jsonwebtoken');
const moment = require("moment");
const userSchema = mongoose.Schema({
name: {
type:String,
maxlength:50
},
email: {
type:String,
trim:true,
unique: 1
},
password: {
type: String,
minglength: 5
},
lastname: {
type:String,
maxlength: 50
},
role : {
type:Number,
default: 0
},
image: String,
token : {
type: String,
},
tokenExp :{
type: Number
}
})
userSchema.pre('save', function( next ) {
var user = this;
if(user.isModified('password')){
// console.log('password changed')
bcrypt.genSalt(saltRounds, function(err, salt){
if(err) return next(err);
bcrypt.hash(user.password, salt, function(err, hash){
if(err) return next(err);
user.password = hash
next()
})
})
} else {
next()
}
});
userSchema.methods.comparePassword = function(plainPassword,cb){
bcrypt.compare(plainPassword, this.password, function(err, isMatch){
if (err) return cb(err);
cb(null, isMatch)
})
}
userSchema.methods.generateToken = function(cb) {
var user = this;
console.log('user',user)
console.log('userSchema', userSchema)
var token = jwt.sign(user._id.toHexString(),'secret')
var oneHour = moment().add(1, 'hour').valueOf();
user.tokenExp = oneHour;
user.token = token;
user.save(function (err, user){
if(err) return cb(err)
cb(null, user);
})
}
userSchema.statics.findByToken = function (token, cb) {
var user = this;
jwt.verify(token,'secret',function(err, decode){
user.findOne({"_id":decode, "token":token}, function(err, user){
if(err) return cb(err);
cb(null, user);
})
})
}
const User = mongoose.model('User', userSchema);
module.exports = { User }
server/routes
user.js
const express = require('express');
const router = express.Router();
const { User } = require("../models/User");
const { auth } = require("../middleware/auth");
//=================================
// User
//=================================
router.get("/auth", auth, (req, res) => {
res.status(200).json({
_id: req.user._id,
isAdmin: req.user.role === 0 ? false : true,
isAuth: true,
email: req.user.email,
name: req.user.name,
lastname: req.user.lastname,
role: req.user.role,
image: req.user.image,
});
});
router.post("/register", (req, res) => {
const user = new User(req.body);
user.save((err, doc) => {
if (err) return res.json({ success: false, err });
return res.status(200).json({
success: true
});
});
});
router.post("/login", (req, res) => {
User.findOne({ email: req.body.email }, (err, user) => {
if (!user)
return res.json({
loginSuccess: false,
message: "Auth failed, email not found"
});
user.comparePassword(req.body.password, (err, isMatch) => {
if (!isMatch)
return res.json({ loginSuccess: false, message: "Wrong password" });
user.generateToken((err, user) => {
if (err) return res.status(400).send(err);
res.cookie("w_authExp", user.tokenExp);
res
.cookie("w_auth", user.token)
.status(200)
.json({
loginSuccess: true, userId: user._id
});
});
});
});
});
router.get("/logout", auth, (req, res) => {
User.findOneAndUpdate({ _id: req.user._id }, { token: "", tokenExp: "" }, (err, doc) => {
if (err) return res.json({ success: false, err });
return res.status(200).send({
success: true
});
});
});
module.exports = router;
반응형
'Back-end > node.js' 카테고리의 다른 글
json 파일 파싱 시 배열로 감싸져 있는 경우 (0) | 2021.03.26 |
---|
Comments