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

前後分離組 (YUNJONE / 葉明荃 / Eric Huang / 江祐敏) #882

Open
wants to merge 137 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
137 commits
Select commit Hold shift + click to select a range
c72f24a
feat: add signin&signup api
AlanChiangg Aug 20, 2023
0924032
fix: repair by npm install
erikku54 Aug 20, 2023
7ba1f90
refactor: restore test file
erikku54 Aug 20, 2023
4e9db41
feat: add cross-env
AlanChiangg Aug 20, 2023
c02af37
feat: add acount,baner to user model/migration
erikku54 Aug 20, 2023
6380d57
feat: add middleware:error-handler
erikku54 Aug 20, 2023
393d17f
feat: add models relation
AlanChiangg Aug 21, 2023
d20a95e
feat: rewrite user model
erikku54 Aug 21, 2023
e2b796c
feat: rewrite signup/signin function
erikku54 Aug 21, 2023
eebd235
Merge branch 'feature/signup-rewrite' of https://github.com/AlanChian…
AlanChiangg Aug 21, 2023
e1cbfcf
Merge pull request #1 from AlanChiangg/feature/signup-rewrite
AlanChiangg Aug 21, 2023
6484cde
feat: add models relation
AlanChiangg Aug 21, 2023
1863510
feat: modify models relation
AlanChiangg Aug 21, 2023
2863a76
refactor: rewrite passport using asyncc/await
erikku54 Aug 21, 2023
35c2935
feat: modify models userId=>UserId & tweetId=>TweetId
AlanChiangg Aug 21, 2023
9417017
fix: solve not returning id on create
erikku54 Aug 21, 2023
367d18e
feat: add user seeder
erikku54 Aug 22, 2023
2dcda1b
feat: add id-attribute to Like model
AlanChiangg Aug 22, 2023
0a1873f
Merge pull request #4 from AlanChiangg/feature/seeder
AlanChiangg Aug 22, 2023
8dcb86b
Merge branch 'master' into feature/add-models-relation
erikku54 Aug 22, 2023
8e21dfc
feat: add id-attribute to Like model
AlanChiangg Aug 22, 2023
92fd6e5
Merge pull request #3 from AlanChiangg/feature/add-models-relation
erikku54 Aug 22, 2023
4e34bcc
Merge pull request #5 from AlanChiangg/feature/add-models-relation
erikku54 Aug 22, 2023
514a202
repush
AlanChiangg Aug 22, 2023
f062815
re-clone
AlanChiangg Aug 22, 2023
caab710
Merge pull request #6 from AlanChiangg/repush
erikku54 Aug 22, 2023
c4ada87
feat: add tweets-seeder
erikku54 Aug 22, 2023
49b7c05
feat: add replies seeder
erikku54 Aug 22, 2023
e51c843
feat: add likes seeder
erikku54 Aug 22, 2023
98bba3e
feat: add followships seeder
erikku54 Aug 23, 2023
c4aea4c
Merge branch 'feature/seeder'
erikku54 Aug 23, 2023
6b943d1
feat: modify config for heroku
AlanChiangg Aug 23, 2023
6e1e01f
feat: add cross-env
AlanChiangg Aug 23, 2023
9e0b951
set port
AlanChiangg Aug 23, 2023
c64a908
feat: modify package scripts
AlanChiangg Aug 23, 2023
c6e82c4
Merge pull request #8 from AlanChiangg/feature/set-heroku
erikku54 Aug 23, 2023
dd92204
Merge pull request #7 from AlanChiangg/feature/seeder
AlanChiangg Aug 23, 2023
5de7f75
feat: edit signin/signup controller for frontend
erikku54 Aug 23, 2023
d402ed3
Merge remote-tracking branch 'origin/master' into feature/user-contro…
erikku54 Aug 23, 2023
dc9edb6
fix: fix by npm install
erikku54 Aug 23, 2023
1baec03
Merge pull request #9 from AlanChiangg/feature/user-controller
AlanChiangg Aug 23, 2023
604dbc3
add-cors
AlanChiangg Aug 23, 2023
3e8a339
feat: add a route handler - /users/:id/tweets
erikku54 Aug 23, 2023
0854749
feat: 1. change response format 2. install cors
erikku54 Aug 23, 2023
3734db2
feat: continue to signIn after signUp
erikku54 Aug 24, 2023
a21e4c6
fix: change the return format of No.4 route
erikku54 Aug 24, 2023
a19d408
feature/user-controller
AlanChiangg Aug 24, 2023
fb51674
Merge pull request #10 from AlanChiangg/feature/user-controller
AlanChiangg Aug 24, 2023
5632f3c
Merge branch 'master' of https://github.com/AlanChiangg/twitter-api-2020
AlanChiangg Aug 24, 2023
b79d6c2
feat: add getTweets controller
AlanChiangg Aug 24, 2023
c39329a
feat: reset helpers
AlanChiangg Aug 24, 2023
8946aa1
feat: add getTweet controller
AlanChiangg Aug 24, 2023
3b9fed3
feat: add postTweet controller
AlanChiangg Aug 24, 2023
e369ce5
feat: modify postTweet
AlanChiangg Aug 24, 2023
7493a3f
feat: update authenticator for test
erikku54 Aug 24, 2023
d5c0000
feat: use helpers for api-auth
AlanChiangg Aug 24, 2023
e7d51c1
Merge pull request #12 from AlanChiangg/feature/user-controller
AlanChiangg Aug 24, 2023
cd8fa84
Merge branch 'master' into feature/tweets-api
erikku54 Aug 24, 2023
4978a58
feat: modify api-auth
AlanChiangg Aug 24, 2023
9ed89ed
feat: modify api-auth
AlanChiangg Aug 24, 2023
e542a72
Merge branch 'feature/tweets-api' of https://github.com/AlanChiangg/t…
AlanChiangg Aug 24, 2023
db9b289
Merge pull request #11 from AlanChiangg/feature/tweets-api
erikku54 Aug 24, 2023
238d415
feat: add User route No.3, No.5, No6, and revise route No.4
erikku54 Aug 25, 2023
5f81d62
refactor: align parameter name with others
erikku54 Aug 25, 2023
292fc87
feat: justify return structure of No.6 route
erikku54 Aug 25, 2023
71240df
feat: add dayjs & modify tweet-controller
AlanChiangg Aug 25, 2023
f5e4f10
feat:modify tweet-controller
AlanChiangg Aug 25, 2023
f7cbfd9
Merge pull request #13 from AlanChiangg/feature/tweets-api
erikku54 Aug 25, 2023
44651da
feat: add User route No.9
erikku54 Aug 25, 2023
0b1e86c
fix: fix return structure of No.9 route to pass test
erikku54 Aug 25, 2023
4c1ef1b
Merge remote-tracking branch 'origin/master' into feature/user-contro…
erikku54 Aug 25, 2023
2db171c
Merge remote-tracking branch 'origin/master' into feature/user-contro…
erikku54 Aug 25, 2023
15279e7
feat:add addLike and removeLike controller
AlanChiangg Aug 25, 2023
1d3ab10
feat: add getReplies
AlanChiangg Aug 25, 2023
b03a54a
add postReply
AlanChiangg Aug 25, 2023
e3e1332
feat: add route No.7 & No.8, revise route No.6
erikku54 Aug 25, 2023
05d6c00
Merge branch 'feature/user-controller' of https://github.com/AlanChia…
AlanChiangg Aug 25, 2023
bff758e
modify getReplies
AlanChiangg Aug 26, 2023
e105fbe
Merge branch 'feature/tweets-api' into feature/user-controller
AlanChiangg Aug 26, 2023
571af1a
feat: add route No.10
erikku54 Aug 26, 2023
d517bcd
modify getReplies
AlanChiangg Aug 26, 2023
02ae451
Merge pull request #15 from AlanChiangg/feature/tweets-api
erikku54 Aug 26, 2023
583d5f3
fix: fix bugs
erikku54 Aug 26, 2023
57c3213
feat: build user route module
erikku54 Aug 26, 2023
bbdac94
feat: transfer 1/0 to true/fase
erikku54 Aug 26, 2023
b3d595f
fix: fix 500 error in test file
erikku54 Aug 26, 2023
b1fef68
Merge remote-tracking branch 'origin/master' into feature/user-contro…
erikku54 Aug 26, 2023
ad0b6ae
doc: add information to .env.example
erikku54 Aug 26, 2023
1634099
feat: initial admin route
erikku54 Aug 26, 2023
d68fa82
fix: fix error on admin-signin
erikku54 Aug 26, 2023
6d587a7
feat: add followship-api
AlanChiangg Aug 26, 2023
e01a9b8
fix: fix naming bug
erikku54 Aug 26, 2023
fd57427
feat: modify removeFollowing
AlanChiangg Aug 26, 2023
91eadc7
Merge branch 'feature/admin-controller' of https://github.com/AlanChi…
AlanChiangg Aug 26, 2023
0c816ae
Merge pull request #17 from AlanChiangg/feature/followships-api
erikku54 Aug 26, 2023
f5a92a7
Merge branch 'master' into feature/admin-controller
AlanChiangg Aug 26, 2023
d3c8b28
Merge pull request #16 from AlanChiangg/feature/admin-controller
AlanChiangg Aug 26, 2023
17d1360
Merge branch 'master' of https://github.com/AlanChiangg/twitter-api-2020
AlanChiangg Aug 26, 2023
9d1c110
feat: add route No.21
erikku54 Aug 27, 2023
80fb0a9
fix: fix no.21 route to fit test file
erikku54 Aug 27, 2023
a977d6d
feat: add getTweets & deleteTweet
AlanChiangg Aug 27, 2023
44d3a2e
doc: add README.md
erikku54 Aug 27, 2023
188347e
feat: modify getTweets
AlanChiangg Aug 27, 2023
4e52b44
Merge pull request #18 from AlanChiangg/feature/admin-controller
erikku54 Aug 27, 2023
4745e69
Merge remote-tracking branch 'origin/master' into feature/admin-contr…
erikku54 Aug 27, 2023
beb2ee2
refactor: remove unnecessary import
erikku54 Aug 27, 2023
86d35c9
Merge pull request #19 from AlanChiangg/feature/admin-controller
AlanChiangg Aug 27, 2023
e731838
fix: revise seed of avatar
erikku54 Aug 27, 2023
5f261dd
Merge pull request #20 from AlanChiangg/feature/admin-controller
AlanChiangg Aug 27, 2023
95b875a
feat: modify getTweets response
AlanChiangg Aug 27, 2023
0a0cb95
fix: revise update condition of introduction
erikku54 Aug 27, 2023
97e5e7c
doc: edit README.md
erikku54 Aug 28, 2023
5eef522
fix: add default value of JWT_SECRET for test
erikku54 Aug 28, 2023
59a9d61
Merge pull request #21 from AlanChiangg/feature/admin-controller
AlanChiangg Aug 29, 2023
bdcab14
feat: modify tweet-controller response
AlanChiangg Aug 29, 2023
6832742
Merge branch 'master' of https://github.com/AlanChiangg/twitter-api-2020
AlanChiangg Aug 29, 2023
900a3ac
feat: modify getTweet response
AlanChiangg Aug 29, 2023
c58ae79
feat: remove session / revise bug in user-controller
erikku54 Aug 29, 2023
dade93e
Merge remote-tracking branch 'origin/master' into feature/chat-room
erikku54 Aug 29, 2023
02c7c72
fix: add default value of JWT_SECRET to pass travis test
erikku54 Aug 29, 2023
b753d97
Merge pull request #22 from AlanChiangg/feature/user-controller
AlanChiangg Aug 29, 2023
a08b852
feat: modify addLike & removeLike response
AlanChiangg Aug 30, 2023
876335b
feat: modify getUser response
AlanChiangg Aug 31, 2023
439ebe7
feat: modify getReplies response
AlanChiangg Aug 31, 2023
709285e
feat: modify seeder
AlanChiangg Aug 31, 2023
7f3b20a
feat: modify addLike controller
AlanChiangg Aug 31, 2023
ef118f1
feat: modify Like model
AlanChiangg Sep 1, 2023
97bc0de
feat: add like attributes
AlanChiangg Sep 1, 2023
9af7fcf
feat: modify putUser controller
AlanChiangg Sep 1, 2023
9da755a
feat: modify putUser
AlanChiangg Sep 1, 2023
1d39619
Merge pull request #23 from AlanChiangg/feature/admin-controller
erikku54 Sep 1, 2023
e76ef63
Merge remote-tracking branch 'origin' into feature/admin-controller
erikku54 Sep 1, 2023
92e3339
feat: add format checker in signup()/putUser()
erikku54 Sep 2, 2023
3c3e630
Merge remote-tracking branch 'origin/master' into feature/user-contro…
erikku54 Sep 2, 2023
0bb6da3
Merge pull request #24 from AlanChiangg/feature/user-controller
AlanChiangg Sep 2, 2023
a54db28
fix: fix bug of putUser
erikku54 Sep 2, 2023
1d48e6a
Merge pull request #25 from AlanChiangg/feature/user-controller
AlanChiangg Sep 2, 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
8 changes: 8 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
SESSION_SECRET=SKIP
JWT_SECRET=SKIP

# IMGUR相關
IMGUR_CLIENT_ID=SKIP
IMGUR_CLIENT_SECRET=SKIP
IMGUR_CLIENT_REFRESH_TOKEN=SKIP
IMGUR_ALBUM_ID=SKIP
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
.DS_Store
*.DS_Store

# multer
upload/


# Logs
logs
*.log
Expand Down
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
123 changes: 123 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# Simple Twitter API

* 一個簡易的twitter專案,提供使用者發文、回覆,以及喜歡和追蹤等和其他使用者互動的方式,並提供後台管理功能。
* 本專案為後端API部份,和Simple Twitter React[前端專案](https://github.com/u88872625/simple-twitter)共同使用才是完整專案。

## 功能說明

* 提供個人帳密註冊、登入、登出。
* 使用者可以維護個人基本資料、並可上傳自訂的頭貼和背景。
* 使用者可以發表推文以及回覆。
* 使用者可以追蹤其他使用者,也可以對其他使用者的推文按喜歡。
* 查看其他使用者資訊、發表的推文、回覆、追蹤中和追隨中的人等
* 提供推薦的使用者名單
* 後台管理功能:瀏覽全站使用者、瀏覽及刪除全站推文。


## 安裝與執行

1. 請先確認安裝有node.js及npm
2. 開啟終端機,進入專案目錄,並從github下載資料

```bash
> git clone https://github.com/AlanChiangg/twitter-api-2020.git
```

3. 安裝套件

```bash
> cd twitter-api-2020
> npm install
```

4. 設定環境變數: 在專案資料夾下的.env.example檔案名稱修改為.env,並填入你的JWT暗碼及IMGUR應用程式資料
```
SESSION_SECRET=<SESSION的暗碼>
JWT_SECRET=<JWT的暗碼>

# IMGUR相關
IMGUR_CLIENT_ID=<IMGUR應用程式的的ID>
IMGUR_CLIENT_SECRET=<IMGUR應用程式的的SECRET>
IMGUR_CLIENT_REFRESH_TOKEN=<IMGUR應用程式的的REFRESH_TOKEN>
IMGUR_ALBUM_ID=<自有IMGUR相簿的ID>
```

5. 設定資料庫:設定連線資訊。如果是開發環境,可直接在config.json中做設定,如果是佈署的情況,要設定對應的環境變數。
```
MYSQL_DATABASE_URL=<MySQL連線字串>
```
6. 如果是新資料庫,需要重新產生資料表結構
```bash
> npx sequelize db:migrate
```

5. 設定種子資料: 在終端機執行底下指令,匯入種子資料到資料庫裡
```bash
> npm run seed

```

6. 執行程式,啟動監聽

```
> npm run start
```

7. 啟動程式後,API即開始進行監聽。

## 提供的API列表

| 項次 | 路由 | 功能 |
|:---- |:------------------------------------|:------------------------------------ |
| | 和sign up/sign in相關 | |
| 1 | POST /api/users | 註冊帳號 |
| 2 | POST /api/users/signin | 登入前台帳號 |
| | 和User相關 | |
| 3 | GET /api/users/:id | 查看某使用者的資料 |
| 4 | GET /api/users/:id/tweets | 查看某使用者發過的推文 |
| 5 | GET /api/users/:id/replied_tweets | 查看某使用者發過的回覆 |
| 6 | GET /api/users/:id/likes | 查看某使用者點過like的推文 |
| 7 | GET /api/users/:id/followings | 查看某使用者追蹤中的人 |
| 8 | GET /api/users/:id/followers | 查看某使用者的追隨者 |
| 9 | GET /api/users? {limit=10} | 查看跟隨者數量排名(前10)的使用者資料 |
| 10 | PUT /api/users/:id | 編輯使用者資料 |
| | 和Followship相關 | |
| 11 | POST /api/followships | 追蹤一個使用者 |
| 12 | DELETE /api/followships/:followingId | 取消追蹤一個使用者 |
| | 和Tweet相關 | |
| 13 | GET /api/tweets | 查看所有的推文 |
| 14 | GET /api/tweets/:id | 查看某一則貼文 |
| 15 | POST /api/tweets | 發布一則推文 |
| | 和Reply相關 | |
| 16 | POST /api/tweets/:id/replies | 發佈一則回覆 |
| 17 | GET /api/tweets/:id/replies | 查看某一則貼文的所有回文 |
| | 和Like相關 | |
| 18 | POST /api/tweets/:id/like | 喜歡一則推文 |
| 19 | POST /api/tweets/:id/unlike | 取消喜歡一則推文 |
| | 和後台管理相關 | |
| 20 | POST /api/admin/signin | 登入後台帳號 |
| 21 | GET /api/admin/users | 取得所有使用者清單 |
| 22 | GET /api/admin/tweets | 取得所有推文清單 |
| 23 | DELETE /api/admin/tweets/:id | 刪除特定推文 |



## 開發環境與主要套件

* VS Code - 編程環境
* node.js / [email protected] - 後端框架
* [email protected] / [email protected] - 資料庫管理
* [email protected] - 認證策略
* [email protected] - 時間格式化
* [email protected] / imgur - 圖檔上傳

底下為開發中使用
* [email protected] - 管理環境變數
* [email protected] - 代碼風格

## 作者

* [**江祐敏**](https://github.com/AlanChiangg)
* [**Eric Huang**](https://github.com/erikku54)

前端專案合作: [**葉明荃**](https://github.com/u88872625)、[**YunJong**](https://github.com/Yunj0ng)
10 changes: 7 additions & 3 deletions _helpers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
function getUser (req) {
return req.user || null
}

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

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

const express = require('express')
const helpers = require('./_helpers');
// const session = require('express-session')
const cors = require('cors')

const passport = require('./config/passport')
const { getUser } = require('./_helpers')
const apis = require('./routes')

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

app.use(express.urlencoded({ extended: true }))
app.use(express.json())
// app.use(session({ secret: process.env.SESSION_SECRET || 'LittleSecret', resave: false, saveUninitialized: false }))
app.use(passport.initialize())
// app.use(passport.session())
app.use(cors())

app.use((req, res, next) => {
res.locals.user = getUser(req)
next()
})

// use helpers.getUser(req) to replace req.user
function authenticated(req, res, next){
// passport.authenticate('jwt', { ses...
};
app.use('/api', apis)

app.get('/', (req, res) => res.send('Hello World!'))
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": "MYSQL_DATABASE_URL"
},
"travis": {
"username": "travis",
Expand Down
66 changes: 65 additions & 1 deletion config/passport.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,69 @@
const passport = require('passport')
const LocalStrategy = require('passport-local')
const passportJWT = require('passport-jwt')
const bcrypt = require('bcryptjs')
const { User } = require('../models')

const ExtractJWT = passportJWT.ExtractJwt
const JWTStrategy = passportJWT.Strategy

// local strategy
passport.use(new LocalStrategy(
{
usernameField: 'account',
passwordField: 'password'
},
async (account, password, done) => {
try {
const cause = { // 定義不同的錯誤訊息,以便前端顯示之用
accountErrMsg: '',
passwordErrMsg: ''
}

module.exports = passport
// 確認必填值是否為空
if (!account) cause.accountErrMsg += '此為必填欄位。'
if (!password) cause.passwordErrMsg += '此為必填欄位。'
if (cause.accountErrMsg || cause.passwordErrMsg) {
throw new Error('Empty input value!', { cause })
}

// 確認帳號是否存在
const user = await User.findOne({ where: { account } })
if (!user) {
cause.accountErrMsg += '帳號不存在!'
throw new Error('User does not exist!', { cause })
}

// 確認密碼是否正確
const result = await bcrypt.compare(password, user.password)
if (!result) {
cause.passwordErrMsg += '不正確的密碼!'
throw new Error('Incorrect password!', { cause })
}

return done(null, user)
} catch (err) {
return done(err)
}
}
))

// jwt strategy
const jwtOptions = {
jwtFromRequest: ExtractJWT.fromAuthHeaderAsBearerToken(),
secretOrKey: process.env.JWT_SECRET || 'SecretTime'
}

passport.use(new JWTStrategy(jwtOptions, async (jwtPayload, done) => {
try {
const user = await User.findByPk(jwtPayload.id)
if (!user) return done(null, false)

return done(null, user)
} catch (error) {
return done(error)
}
})
)

module.exports = passport
Loading