Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

全端: Felix Leon Zephyr # #335

Open
wants to merge 35 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
cd3edb4
feat: initialize models and seeders
Leon180 Mar 19, 2023
2ab0c16
first commit
zell0323 Mar 20, 2023
1ba1e8d
delete somehing unnecessary
zell0323 Mar 20, 2023
4a69f2f
add basic folders
zell0323 Mar 20, 2023
b2514cb
added ctroller routes
zell0323 Mar 22, 2023
77fda71
home complete
zell0323 Mar 23, 2023
ca9cfc5
layout finish
zell0323 Mar 23, 2023
d8679a7
zzzz
zephyrlin33 Mar 26, 2023
736bbbf
merge leon to zLin33
zephyrlin33 Mar 26, 2023
6bfdde0
add admin/admin_main page
zephyrlin33 Mar 26, 2023
b2682b2
feat: user regist
zephyrlin33 Mar 26, 2023
09013aa
feat: flash message at regist page
zephyrlin33 Mar 26, 2023
6e165ef
feat: passport init & signin
zephyrlin33 Mar 26, 2023
cb9921e
feat: login & regist page for user
zephyrlin33 Mar 26, 2023
d88d4b4
following completed
zell0323 Mar 27, 2023
b7e6f1d
merged with zlin
zell0323 Mar 27, 2023
2aa3a19
all done except reply
zell0323 Mar 29, 2023
fa4b11b
basically finished
zell0323 Mar 29, 2023
6c951be
combined user profile completed
zell0323 Mar 30, 2023
c8c9fa2
combining settnig
zell0323 Mar 31, 2023
4e3b942
aaa
zell0323 Apr 1, 2023
c770c58
notification
zell0323 Apr 1, 2023
16dfef8
in progress admin
zell0323 Apr 2, 2023
e3647bf
finish
zell0323 Apr 2, 2023
f535a7e
almost!
zell0323 Apr 2, 2023
6e1e19a
final
zell0323 Apr 2, 2023
86c7dd7
heroku
zell0323 Apr 2, 2023
86a0584
heroku
zell0323 Apr 2, 2023
c9599a5
heroku
zell0323 Apr 2, 2023
f8c1c49
heroku fix
zell0323 Apr 2, 2023
f7b5f82
heroku fix again
zell0323 Apr 2, 2023
9a3dcf6
add bcrypt-nodejs
zell0323 Apr 3, 2023
c30f93b
try to fix bcrypt error
zell0323 Apr 3, 2023
ce4ef3b
change to node 14
zell0323 Apr 3, 2023
84023c0
Create README.md
zell0323 Apr 3, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
env:
browser: true
commonjs: true
es2021: true
extends: eslint:recommended
overrides: []
parserOptions:
ecmaVersion: latest
rules: {}
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,11 @@ typings/
.fusebox/

# DynamoDB Local files
.dynamodb/
.dynamodb/

# Others
.note.ac

.scss

gulpfile.js
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: NODE_ENV=production node app.js
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# twitter-fullstack-2020
ALPHA Camp | 學期 3 | Simple Twitter | 自動化測試檔 (全端開發組)
## 安裝所需環境

可執行git指令之終端機

## 安裝方法

1. 於終端機輸入下列指令,或直接將程式碼打包下載
```
git clone https://github.com/zell0323/twitter-fullstack-2020/
```
2. 進入專案資料夾
```
cd twitter-fullstack-2020
```
3. 下載所需套件
```
npm install
```
4. 依照config/config.json的development設定,建立MySQL資料庫
```js
"development": {
"username": "root",
"password": "password",
"database": "ac_twitter_workspace",
"host": "127.0.0.1",
"dialect": "mysql",
"logging": false
}

```
5. 執行migration,建立table和種子檔案
```
npx sequelize db:migrate
npx sequelize db:seed:all
```
6. 執行專案,開啟虛擬伺服器
```
npm run dev
```
7. 於瀏覽器輸入網址 http://localhost:3000/

## 測試帳號

### 一般使用者
帳號:user1
密碼:12345678
### 管理者(僅能進入後台)
帳號:root
密碼:12345678


## 專案功能
一般使用者可推文、回覆推文、追蹤其他使用者、加入喜愛推文
管理者可刪除推文、看到使用者一覽

58 changes: 55 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,65 @@
// eslint-disable-next-line no-undef
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}

const express = require('express')
const handlebars = require('express-handlebars')
const Handlebars = require('handlebars')
const flash = require('connect-flash')
const session = require('express-session')
const passport = require('./config/passport')
const helpers = require('./_helpers');

const app = express()
const port = 3000
const port = process.env.PORT || 3000
const methodOverride = require('method-override')
const getTopTen = require('./helpers/getTopTen')
const { pages, apis } = require('./routes')



const SESSION_SECRET = 'secret'

Handlebars.registerHelper('raw', function (options) {
return options.fn();
});

app.engine('hbs', handlebars({
extname: '.hbs',
partialsDir: ['views/partials', 'views/partials/svgs']
}));
app.set('view engine', 'hbs')
// use helpers.getUser(req) to replace req.user
// use helpers.ensureAuthenticated(req) to replace req.isAuthenticated()

app.get('/', (req, res) => res.send('Hello World!'))
app.use(session({ secret: SESSION_SECRET, resave: false, saveUninitialized: true }))
app.use(passport.initialize()) // 初始化 Passport
app.use(passport.session()) // 啟動 session 功能
app.use(flash()) // 掛載套件
app.use(express.static('public'));
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.use(methodOverride('_method'))




app.use(async (req, res, next) => {
res.locals.success_messages = req.flash('success_messages') // 設定 success_msg 訊息
res.locals.error_messages = req.flash('error_messages') // 設定 warning_msg 訊息
res.locals.currentUser = helpers.getUser(req)
res.locals.topTenFollowed = await getTopTen(req)
next();
});

app.use(apis)
app.use(pages)





app.listen(port, () => console.log(`Example app listening on port ${port}!`))


module.exports = app
9 changes: 3 additions & 6 deletions config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"password": "password",
"database": "ac_twitter_workspace",
"host": "127.0.0.1",
"dialect": "mysql"
"dialect": "mysql",
"logging": false
},
"test": {
"username": "root",
Expand All @@ -15,11 +16,7 @@
"logging": false
},
"production": {
"username": "root",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "mysql"
"use_env_variable": "CLEARDB_DATABASE_URL"
},
"travis": {
"username": "travis",
Expand Down
64 changes: 64 additions & 0 deletions config/passport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const passport = require('passport')
const LocalStrategy = require('passport-local')
const bcrypt = require('bcryptjs')
const { User, Like } = require('../models')

// set up Passport strategy
passport.use('user-local', new LocalStrategy(
// customize user field
{
usernameField: 'account',
passwordField: 'password',
passReqToCallback: true
},
// authenticate user
(req, account, password, cb) => {
User.findOne({ where: { account } })
.then(user => {
if (!user ) return cb(null, false, req.flash('error_messages', '帳號不存在!'))
bcrypt.compare(password, user.password).then(res => {
if (!res) return cb(null, false, req.flash('error_messages', '帳號或密碼輸入錯誤!'))
return cb(null, user)
})
})
}
))


passport.use('admin-local', new LocalStrategy(
// customize user field
{
usernameField: 'account',
passwordField: 'password',
passReqToCallback: true
},
// authenticate user
(req, account, password, cb) => {
User.findOne({ where: { account } })
.then(user => {
console.log(user)
if (!user ) return cb(null, false, req.flash('error_messages', '帳號不存在!'))
bcrypt.compare(password, user.password).then(res => {
if (!res) return cb(null, false, req.flash('error_messages', '帳號或密碼輸入錯誤!'))
return cb(null, user)
})
})
}
))

// serialize and deserialize user
passport.serializeUser((user, cb) => {
cb(null, user.id)
})
passport.deserializeUser((id, cb) => {
return User.findByPk(id, {
include: [
{ model: Like },
{ model: User, as: 'Followings' },
{ model: User, as: 'Followers' }
],
})
.then(user => cb(null, user.toJSON()))
.catch(err => cb(err))
})
module.exports = passport
Loading