Skip to content

Commit

Permalink
refactor: JavaScript Standard Style
Browse files Browse the repository at this point in the history
  • Loading branch information
Kris3131 committed Sep 5, 2022
1 parent 41e1e21 commit 16f2a7a
Show file tree
Hide file tree
Showing 14 changed files with 395 additions and 395 deletions.
3 changes: 1 addition & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ require('dotenv').config()
require('./config/mongoose')

const app = express()
const port = 3000

app.engine(
'hbs',
Expand All @@ -31,7 +30,7 @@ app.use(
mongoUrl: process.env.MONGODB_URI,
touchAfter: 24 * 3600,
}),
cookie: { maxAge: 60 * 1000 },
cookie: { maxAge: 360 * 1000 },
})
)
app.use(express.static('publics'))
Expand Down
4 changes: 2 additions & 2 deletions config/mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const mongoose = require('mongoose')

mongoose.connect(process.env.MONGODB_URI)
const db = mongoose.connection
db.on('err', () => console.log(`Mongoose connected to MONGODB Error`))
db.once('open', () => console.log(`Mongoose connected to MONGODB Success`))
db.on('err', () => console.log('Mongoose connected to MONGODB Error'))
db.once('open', () => console.log('Mongoose connected to MONGODB Success'))

module.exports = db
206 changes: 103 additions & 103 deletions config/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,110 +7,110 @@ const GoogleStrategy = require('passport-google-oauth20').Strategy
const User = require('../models/User')

module.exports = (app) => {
app.use(passport.initialize())
app.use(passport.session())
app.use(passport.initialize())
app.use(passport.session())

passport.use(
new LocalStrategy(
{ usernameField: 'email', passReqToCallback: true }, //
(req, email, password, done) => {
User.findOne({ email }) // 從資料庫找有沒有人
.then((user) => {
if (!user) {
return done(null, false, {
message: req.flash('warning_msg', `Email 沒有註冊過`),
}) // 假如沒有資料->結束程序,不用帶錯誤訊息/沒有用戶資料/顯示訊息
}
return bcrypt.compare(password, user.password).then((isMatch) => {
if (!isMatch) {
return done(null, false, {
message: req.flash('warning_msg', `Email 或 Password 不正確`),
})
}
return done(null, user)
})
})
.catch((err) => done(err, false))
}
)
)
passport.use(
new LocalStrategy(
{ usernameField: 'email', passReqToCallback: true }, //
(req, email, password, done) => {
User.findOne({ email }) // 從資料庫找有沒有人
.then((user) => {
if (!user) {
return done(null, false, {
message: req.flash('warning_msg', 'Email 沒有註冊過')
}) // 假如沒有資料->結束程序,不用帶錯誤訊息/沒有用戶資料/顯示訊息
}
return bcrypt.compare(password, user.password).then((isMatch) => {
if (!isMatch) {
return done(null, false, {
message: req.flash('warning_msg', 'Email 或 Password 不正確')
})
}
return done(null, user)
})
})
.catch((err) => done(err, false))
}
)
)

passport.use(
new FacebookStrategy(
{
clientID: process.env.FB_CLIENTID,
clientSecret: process.env.FB_CLIENT_SECRET,
callbackURL: process.env.FB_CALLBACK_URL,
profileFields: ['email', 'displayName'],
},
(accessToken, refreshToken, profile, done) => {
const { name, email } = profile._json
User.findOne({ email })
.then((user) => {
if (user) {
return done(null, user)
}
const randomPassword = Math.random().toString(36).slice(-8)
bcrypt
.genSalt(10)
.then((salt) => bcrypt.hash(randomPassword, salt))
.then((hash) =>
User.create({
name,
email,
password: hash,
})
)
.then((user) => done(null, user))
.catch((err) => done(err, false))
})
.catch((err) => done(err, false))
}
)
)
passport.use(
new FacebookStrategy(
{
clientID: process.env.FB_CLIENTID,
clientSecret: process.env.FB_CLIENT_SECRET,
callbackURL: process.env.FB_CALLBACK_URL,
profileFields: ['email', 'displayName']
},
(accessToken, refreshToken, profile, done) => {
const { name, email } = profile._json
User.findOne({ email })
.then((user) => {
if (user) {
return done(null, user)
}
const randomPassword = Math.random().toString(36).slice(-8)
bcrypt
.genSalt(10)
.then((salt) => bcrypt.hash(randomPassword, salt))
.then((hash) =>
User.create({
name,
email,
password: hash
})
)
.then((user) => done(null, user))
.catch((err) => done(err, false))
})
.catch((err) => done(err, false))
}
)
)

passport.use(
new GoogleStrategy(
{
clientID: process.env.GOOGLE_CLIENTID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: process.env.GOOGLE_CALLBACK_URL,
profileFields: ['email', 'displayName'],
},
(accessToken, refreshToken, profile, done) => {
const { name, email } = profile._json
User.findOne({ email })
.then((user) => {
if (user) {
return done(null, user)
}
const randomPassword = Math.random().toString(36).slice(-8)
bcrypt
.genSalt(10)
.then((salt) => bcrypt.hash(randomPassword, salt))
.then((hash) =>
User.create({
name,
email,
password: hash,
})
)
.then((user) => done(null, user))
.catch((err) => done(err, false))
})
.catch((err) => done(err, false))
}
)
)
// user -> session
passport.serializeUser((user, done) => {
return done(null, user.id)
})
// session -> user
passport.deserializeUser((id, done) => {
return User.findById(id)
.lean()
.then((user) => done(null, user))
.catch((err) => done(err, null))
})
passport.use(
new GoogleStrategy(
{
clientID: process.env.GOOGLE_CLIENTID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: process.env.GOOGLE_CALLBACK_URL,
profileFields: ['email', 'displayName']
},
(accessToken, refreshToken, profile, done) => {
const { name, email } = profile._json
User.findOne({ email })
.then((user) => {
if (user) {
return done(null, user)
}
const randomPassword = Math.random().toString(36).slice(-8)
bcrypt
.genSalt(10)
.then((salt) => bcrypt.hash(randomPassword, salt))
.then((hash) =>
User.create({
name,
email,
password: hash
})
)
.then((user) => done(null, user))
.catch((err) => done(err, false))
})
.catch((err) => done(err, false))
}
)
)
// user -> session
passport.serializeUser((user, done) => {
return done(null, user.id)
})
// session -> user
passport.deserializeUser((id, done) => {
return User.findById(id)
.lean()
.then((user) => done(null, user))
.catch((err) => done(err, null))
})
}
14 changes: 7 additions & 7 deletions middleware/auth.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module.exports = {
authenticator: (req, res, next) => {
if (req.isAuthenticated()) {
return next()
}
req.flash('warning_msg', '請先登入')
res.redirect('/users/login')
},
authenticator: (req, res, next) => {
if (req.isAuthenticated()) {
return next()
}
req.flash('warning_msg', '請先登入')
res.redirect('/users/login')
}
}
14 changes: 7 additions & 7 deletions models/Category.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const mongoose = require('mongoose')
const Schema = mongoose.Schema
const categorySchema = new Schema({
name: {
type: String,
required: true,
},
icon: {
type: String,
},
name: {
type: String,
required: true
},
icon: {
type: String
}
})
module.exports = mongoose.model('Category', categorySchema)
48 changes: 24 additions & 24 deletions models/Record.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ const mongoose = require('mongoose')
const Schema = mongoose.Schema

const recordSchema = new Schema({
name: {
type: String,
required: true,
},
date: {
type: Date,
required: true,
},
amount: {
type: Number,
required: true,
},
categoryId: {
type: Schema.Types.ObjectId,
ref: 'Category',
index: true,
required: true,
},
userId: {
type: Schema.Types.ObjectId,
ref: 'User',
index: true,
required: true,
},
name: {
type: String,
required: true
},
date: {
type: Date,
required: true
},
amount: {
type: Number,
required: true
},
categoryId: {
type: Schema.Types.ObjectId,
ref: 'Category',
index: true,
required: true
},
userId: {
type: Schema.Types.ObjectId,
ref: 'User',
index: true,
required: true
}
})

module.exports = mongoose.model('Record', recordSchema)
26 changes: 13 additions & 13 deletions models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ const mongoose = require('mongoose')
const Schema = mongoose.Schema

const userSchema = new Schema({
name: {
type: String,
required: true,
},
email: {
type: String,
unique: true,
required: true,
},
password: {
type: String,
required: true,
},
name: {
type: String,
required: true
},
email: {
type: String,
unique: true,
required: true
},
password: {
type: String,
required: true
}
})

module.exports = mongoose.model('User', userSchema)
12 changes: 6 additions & 6 deletions models/seeds/categorySeeder.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ const Category = require('../Category')
const categoryList = require('../seeds/categoryList.json')

db.once('open', () => {
Category.create(categoryList)
.then(() => {
console.log('Category created')
process.exit()
})
.catch((error) => console.log(error))
Category.create(categoryList)
.then(() => {
console.log('Category created')
process.exit()
})
.catch((error) => console.log(error))
})
Loading

0 comments on commit 16f2a7a

Please sign in to comment.