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

前後分離組(胡晉嘉/Felicia/ikuki/張景泰) #908

Open
wants to merge 100 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
100 commits
Select commit Hold shift + click to select a range
79a0783
feat: project init and add packages
DannyHucc Aug 21, 2023
9161478
feat: add local and jwt passport function
DannyHucc Aug 21, 2023
71fb4db
feat: add eslint packages
DannyHucc Aug 21, 2023
e9c90a9
feat: add error handler middleware
DannyHucc Aug 21, 2023
1a66c2a
modify: file name
DannyHucc Aug 21, 2023
c607544
feat: add env.exp and lint
DannyHucc Aug 21, 2023
fbb50a9
feat: use eslint
DannyHucc Aug 21, 2023
eb314f0
modify: tests to test in .eslintignore
DannyHucc Aug 21, 2023
fafd5f5
feat: add helpers and middleware and gitignore for multer
DannyHucc Aug 21, 2023
94600ab
feat: add auth middleware for passport
DannyHucc Aug 21, 2023
77464ff
feat: add middleware role-check
DannyHucc Aug 21, 2023
022fc67
feat: add tweet model
muco0521 Aug 22, 2023
de6fce6
Merge pull request #1 from DannyHucc/feature/add-model-tweet
DannyHucc Aug 22, 2023
5ffe74c
feat: add user model git merge --squash feature/user-models
DannyHucc Aug 22, 2023
34b1b54
feat: modify tweet model file name
DannyHucc Aug 22, 2023
784272b
feat: refactor tweet.js
DannyHucc Aug 22, 2023
88effef
feat: add reply model git merge --squash feature/reply
DannyHucc Aug 22, 2023
1341bbf
feat: add like model git merge --squash feature/like
DannyHucc Aug 22, 2023
17aa373
feat: modify like model file name
DannyHucc Aug 22, 2023
b3bf32c
feat: add followship model git merge --squash feature/followship-model
DannyHucc Aug 22, 2023
47f2d0b
modify: add api router path in app.js
DannyHucc Aug 22, 2023
bd2d5c3
modify: passport in local.js
DannyHucc Aug 22, 2023
76ee3f7
feat add router users signUp git merge --squash feature/users-signUp
DannyHucc Aug 22, 2023
bc0a185
feat: add user seeder
muco0521 Aug 22, 2023
8eaad6b
feat: add tweet seeder
muco0521 Aug 22, 2023
d50ef38
feat: add reply seeder
muco0521 Aug 22, 2023
36a5cc5
Merge pull request #6 from DannyHucc/feature/add-seeder
DannyHucc Aug 23, 2023
de79986
feat: npm i
DannyHucc Aug 23, 2023
883e1f4
feat: add admin login
muco0521 Aug 23, 2023
5b7d237
Merge pull request #7 from muco0521/feature/admin-login
DannyHucc Aug 23, 2023
c3ac6a5
feat: add users signin
DannyHucc Aug 23, 2023
10136a9
feat: add get user tweet feature
DannyHucc Aug 23, 2023
926a7e5
feat: add get user reply feature
DannyHucc Aug 23, 2023
7c89978
feat: add get user like
DannyHucc Aug 23, 2023
d57f930
feat: add get user reply and like feature
DannyHucc Aug 23, 2023
b6af7ba
feat: add get user following feature
DannyHucc Aug 23, 2023
a215ace
get user follower feature
DannyHucc Aug 23, 2023
591f5db
feat: add get user feature
DannyHucc Aug 23, 2023
8cf5aaa
feat: add put user profile feature
DannyHucc Aug 23, 2023
f243a89
feat: add update user account feature
DannyHucc Aug 23, 2023
ffbb17c
feat: modify put user profile
DannyHucc Aug 23, 2023
102b315
feat: add admin get users
muco0521 Aug 23, 2023
8d259d2
feat: add admin delete tweet feature
muco0521 Aug 23, 2023
3b911e4
fix: admin-getUsers delete filter role
muco0521 Aug 23, 2023
4c12ce3
Merge pull request #13 from muco0521/feature/admin-function
DannyHucc Aug 24, 2023
f83d812
feat: add followship following
muco0521 Aug 24, 2023
c4d9d3f
feat: add sequelize sync
DannyHucc Aug 24, 2023
09ec336
feat: add cors
DannyHucc Aug 24, 2023
e20ef14
feat: add remove following feature
muco0521 Aug 24, 2023
e98c855
Merge pull request #14 from muco0521/feature/followship-function
DannyHucc Aug 24, 2023
d3f96f0
feat: add get tweets feature
DannyHucc Aug 24, 2023
20ae4d5
feat add post tweet feature
DannyHucc Aug 24, 2023
5da766e
feat: modify config for heroku
DannyHucc Aug 24, 2023
a3f9a19
feat: modify seed for heroku
DannyHucc Aug 24, 2023
ea79e83
feat: modify create user avatar and cover defaultValue
DannyHucc Aug 25, 2023
b7dad4f
feat: add get tweet feature
muco0521 Aug 25, 2023
06ffa71
Merge pull request #17 from muco0521/feature/get-tweet
DannyHucc Aug 25, 2023
399203d
feat: add get replies feature
DannyHucc Aug 25, 2023
8f61276
feat: modify app.js for heroku
DannyHucc Aug 25, 2023
087ba38
feat: get top 10 follower
muco0521 Aug 26, 2023
cb4903a
Merge pull request #19 from muco0521/feature/top-follower
DannyHucc Aug 26, 2023
8a72222
feat: modify get user for optimization
DannyHucc Aug 26, 2023
2883d03
feat: modify get user
DannyHucc Aug 26, 2023
4d18559
feat: limit role add following function and show error msg
muco0521 Aug 28, 2023
a7cd2d4
feat: add post reply feature
DannyHucc Aug 28, 2023
6eaa51a
feat: update getTopFollower
muco0521 Aug 28, 2023
f2b93b5
feat: add add like feature
DannyHucc Aug 28, 2023
9f8c592
Merge pull request #23 from muco0521/feature/followship-update
DannyHucc Aug 28, 2023
9fb0a65
feat: add remove like feature
DannyHucc Aug 29, 2023
214dd50
feat: add admin get tweets feature
DannyHucc Aug 29, 2023
3e7f5a8
feat: modify admin.js
DannyHucc Aug 29, 2023
a7fd569
feat: modify admin.js
DannyHucc Aug 29, 2023
3745baa
feat: add README
DannyHucc Sep 1, 2023
1cf76b9
feat: modify README
DannyHucc Sep 1, 2023
c930a2c
feat: modify README
DannyHucc Sep 1, 2023
45d9bf4
feat: update getTweet
muco0521 Sep 1, 2023
bb13977
Merge pull request #27 from muco0521/feature/update-getTweet
DannyHucc Sep 1, 2023
0224011
update get user feature
DannyHucc Sep 1, 2023
d01742b
feat: modify get user follower feature
DannyHucc Sep 1, 2023
c310983
feat: modify get user following feature
DannyHucc Sep 1, 2023
6306e7b
feat: modify userController.js
DannyHucc Sep 1, 2023
ae5bd10
feat: modify admin get users feature for heroku
DannyHucc Sep 1, 2023
e5597f2
feat: modify admin get users feature for heroku
DannyHucc Sep 1, 2023
76605e2
feat: npm i
DannyHucc Sep 1, 2023
bb90d34
feat: modify image
DannyHucc Sep 1, 2023
ee66df5
feat: limit admin tweets list description
muco0521 Sep 2, 2023
1815a05
Merge pull request #31 from muco0521/feature/update-admin-getTweets
DannyHucc Sep 2, 2023
0854e66
feat: add return
DannyHucc Sep 2, 2023
4359dbb
feat: update get tweet
muco0521 Sep 2, 2023
b09b1ae
Merge pull request #32 from muco0521/feature/update-getTweet
DannyHucc Sep 2, 2023
6ee1c38
feat: modify get tweet
DannyHucc Sep 2, 2023
98e592a
feat: update get top follower data name
muco0521 Sep 3, 2023
ebf4657
Merge pull request #34 from muco0521/feature/update-followship
DannyHucc Sep 3, 2023
fe04e49
feat: modify package.json
DannyHucc Sep 3, 2023
2da8f3f
feat: npm i
DannyHucc Sep 3, 2023
ac9b9c7
feat: modify package
DannyHucc Sep 3, 2023
3e28c48
feat: update-package-lock
muco0521 Sep 3, 2023
45251de
Merge pull request #35 from muco0521/feature/update-package-lock
DannyHucc Sep 3, 2023
7e1f3cc
feat: add dotenv
DannyHucc Sep 3, 2023
0f8d9e7
feat: modify code
DannyHucc Sep 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
2 changes: 2 additions & 0 deletions .env.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
IMGUR_CLIENT_ID=
JWT_SECRET=
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules/*
/test/*
12 changes: 12 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
env:
browser: true
commonjs: true
es2021: true
extends:
- standard
parserOptions:
ecmaVersion: 12
rules:
arrow-parens:
- warn
- as-needed
53 changes: 53 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
* text=auto

# Source code
*.css text diff=css
*.html text diff=html
*.js text
*.json text

# Documentation
*.markdown text diff=markdown
*.md text diff=markdown
*.txt text

# Templates
*.handlebars text
*.hbs text

# Configs
*.cnf text
*.conf text
*.config text
.editorconfig text
.env text
.env.* text
.gitattributes text
.gitconfig text
*.lock text -diff
package.json text eol=lf
package-lock.json text -diff
yarn.lock text -diff
*.yaml text
*.yml text
browserslist text

# Heroku
Procfile text

# Graphics
*.gif binary
*.ico binary
*.jpg binary
*.jpeg binary
*.pdf binary
*.png binary
# SVG treated as an asset (binary) by default.
*.svg text
# If you want to treat it as binary,
# use the following line instead.
# *.svg binary
*.webp binary

# Ignore files (like .npmignore or .gitignore)
*.*ignore text
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,8 @@ typings/
.fusebox/

# DynamoDB Local files
.dynamodb/
.dynamodb/

# multer
temp
upload
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
144 changes: 144 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
# Simple Twitter

![screenshot](public/images/placeholder1.png)

## About - 介紹

這是一個前後端分離的小組專案,前端使用React,後端則使用Node.js、Express搭配MySQL關連式資料庫所打造的API Server。
Simple Twitter 提供後台管理及前台頁面,使用者可以註冊,登入,推文,回覆推文,喜歡跟追蹤使用者等功能。
<br><br>

## 前端github repo - <https://github.com/akedaikuki/react-twitter>

<br><br>

## API - 接口文件

文件內提供每個街口的的使用方式,回傳,成功跟失敗的回傳資訊:<br>
<https://documenter.getpostman.com/view/28413018/2s9Xy5LqWf>
<br><br>

## Environment - 開發環境

* node v18.16.1
* nodemon
<br><br>

## Installation and Execution - 安裝與執行步驟

### 1. 開啟Terminal, Clone此專案至本機

```
git clone https://github.com/DannyHucc/twitter-api-2020.git
```

### 2. 進入存放此專案的資料夾

```
cd twitter-api-2020
```

### 3. 安裝所需要的NPM Packages

```
npm install
```

### 4. 在SQL WorkBench 建立資料庫 (在workBench內輸入)

```
create database ac_twitter_workspace
```

### 5. 建立資料庫 tables

```
npx sequelize db:migrate
```

### 5. 建立資料庫 seed

```
npx sequelize db:seed:all
```

### 6. 建立.env 檔案放入密碼 (參考.env.exp)

```
IMGUR_CLIENT_ID= 你的密碼
JWT_SECRET= 你的密碼
```

### 7. 啟動伺服器 (使用 nodemon)

```
npm run dev
```

### 8. Terminal出現以下字樣代表成功啟動

```
Server running on port:3000!
<...sequelize message...>
yes re-sync done!
```

<br>

## Seed Accounts - 測試帳號

專案提供 1 個後台管理者帳號與跟 5 位使用者帳號<br>
管理者帳號只可以使用後台功能,使用者帳號只能使用前台功能

### # 管理者帳號 - 後台

account: root <br>
email: <[email protected]> <br>
password: 12345678 <br>

### # 使用者帳號 - 前台

account: user1 ~ user5 <br>
email: <[email protected]> <br>
password: 12345678 <br>
<br>

## Development Tools - 開發工具

* bcrypt-nodejs: "0.0.3",
* bcryptjs: "^2.4.3",
* body-parser: "^1.18.3",
* chai: "^4.2.0",
* connect-flash: "^0.1.1",
* cors: "^2.8.5",
* dotenv: "^16.3.1",
* express: "^4.16.4",
* express-session: "^1.15.6",
* express-validator: "^7.0.1",
* faker: "^5.5.3",
* imgur: "^1.0.2",
* jsonwebtoken: "^8.5.1",
* method-override: "^3.0.0",
* mocha: "^6.0.2",
* multer: "^1.4.3",
* mysql2: "^1.6.4",
* passport: "^0.4.0",
* passport-jwt: "4.0.0",
* passport-local: "^1.0.0",
* sequelize: "^6.18.0",
* sequelize-cli: "^5.5.0",
* sinon-chai: "^3.3.0",
* socket.io: "^4.7.1"
<br><br>

## Team - 團隊成員

### 前端

[akedaikuki](https://github.com/akedaikuki)<br>
[aria198a](https://github.com/aria198a)

### 後端

[DannyHucc](https://github.com/DannyHucc)<br>
[muco0521](https://github.com/muco0521)
8 changes: 4 additions & 4 deletions _helpers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

function getUser(req) {
return req.user;
function getUser (req) {
return req.user
}

module.exports = {
getUser,
};
getUser
}
36 changes: 28 additions & 8 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}

const express = require('express')
const helpers = require('./_helpers');
const cors = require('cors')
const methodOverride = require('method-override')

const routes = require('./routes')
const passport = require('./config/passport')

const app = express()
const port = 3000
const port = process.env.PORT || 3000

const corsOptions = {
origin: '*',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS',
allowedHeaders: ['Content-Type', 'Authorization']
}

// middleware
app.use(cors(corsOptions))
app.use(express.urlencoded({ extended: true }))
app.use(express.json())
app.use(passport.initialize())
app.use(methodOverride('_method'))

// use helpers.getUser(req) to replace req.user
function authenticated(req, res, next){
// passport.authenticate('jwt', { ses...
};
// routes
app.use('/api', routes)
app.get('/', (req, res) => { res.send('Hello World!') })

app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
// start
app.listen(port, () => console.log(`Server running on port:${port}!`))

module.exports = app
6 changes: 1 addition & 5 deletions config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@
"logging": false
},
"production": {
"username": "root",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "mysql"
"use_env_variable": "MYSQL_DATABASE_URL"
},
"travis": {
"username": "travis",
Expand Down
8 changes: 6 additions & 2 deletions config/passport.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const passport = require('passport')
const LocalStrategy = require('./strategies/local')
const JtwStrategy = require('./strategies/jwt')

// strategy
LocalStrategy(passport)
JtwStrategy(passport)


module.exports = passport
module.exports = passport
26 changes: 26 additions & 0 deletions config/strategies/jwt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}

const passportJWT = require('passport-jwt')
const { User } = require('../../models')

const JwtStrategy = passportJWT.Strategy
const ExtractJwt = passportJWT.ExtractJwt

const jwtOptions = {
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: process.env.JWT_SECRET || 'Secret'
}

module.exports = passport => {
// 認證JWT
passport.use(new JwtStrategy(jwtOptions, (jwtPayload, done) => {
User.findByPk(jwtPayload.id)
.then(user => {
if (!user) return done(null, false)
return done(null, user)
})
.catch(err => done(err))
}))
}
32 changes: 32 additions & 0 deletions config/strategies/local.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const LocalStrategy = require('passport-local').Strategy
const { User } = require('../../models')
const bcrypt = require('bcryptjs')

module.exports = passport => {
passport.use(new LocalStrategy({
usernameField: 'account',
passwordField: 'password'
}, (account, password, done) => {
// use email to check user exist
// 查出使用者資料,放入req.user
User.findOne({ where: { account } })
.then(user => {
if (!user) {
const error = new Error('帳號不存在!')
error.status = 401
return done(error, false)
}
// compare password
bcrypt.compare(password, user.password).then(isMatch => {
if (!isMatch) {
const error = new Error('帳號或是密碼錯誤!')
error.status = 401
return done(error, false)
}
// authenticated, return user
return done(null, user)
})
.catch(err => done(err, false))
})
}))
}
Loading