Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
207 commits
Select commit Hold shift + click to select a range
9d56059
feat: stepup folders, error handlers and app.js
klu0926 Jun 6, 2023
1e67ca8
feat: add Procfile, README and npm run lint
klu0926 Jun 6, 2023
544d808
Merge pull request #1 from klu0926/setup
kim1037 Jun 6, 2023
f861f08
feat: add column to users and likes table
kim1037 Jun 6, 2023
c4de65d
feat: add associate to each model
kim1037 Jun 6, 2023
3a2e27d
Merge branch 'master' into set-models
kim1037 Jun 6, 2023
41be05f
fix: add model init and pass model test
kim1037 Jun 6, 2023
bb8534a
feat: add isLike to like model init
kim1037 Jun 6, 2023
4479e04
Merge pull request #2 from klu0926/set-models
klu0926 Jun 6, 2023
52b0c8e
feat: add all models seeders
kim1037 Jun 6, 2023
4b07e27
Merge pull request #3 from klu0926/seeders
klu0926 Jun 6, 2023
319f43c
feat: add auth and login
klu0926 Jun 6, 2023
3086897
chore: ESlint
klu0926 Jun 6, 2023
6e4a8f3
Merge pull request #4 from klu0926/auth-and-login
kim1037 Jun 6, 2023
d9c77d0
refactor: role-check, and local auth
klu0926 Jun 7, 2023
8008be3
fix: change user role to have defaul
klu0926 Jun 7, 2023
8e6db4f
feat: add user role not allow null
klu0926 Jun 7, 2023
72ea7e9
Merge pull request #5 from klu0926/fix-login
kim1037 Jun 7, 2023
e58403d
feat: signup
klu0926 Jun 7, 2023
9affef4
feat:add routes and controllter function name
kim1037 Jun 7, 2023
d3e591f
Merge pull request #6 from klu0926/user-signup
kim1037 Jun 7, 2023
b7580da
Merge branch 'master' into get-user-data
kim1037 Jun 7, 2023
098a042
feat: add get User Data
kim1037 Jun 7, 2023
7698298
feat: user model role default
kim1037 Jun 7, 2023
a8075c8
Merge pull request #7 from klu0926/get-user-data
klu0926 Jun 7, 2023
126ea3f
fix: change role check, and add some npm scripts
klu0926 Jun 7, 2023
de81f15
Merge pull request #8 from klu0926/fix-roleCheck
kim1037 Jun 7, 2023
b0f261a
feat: add tweet routes
klu0926 Jun 7, 2023
8840ec1
feat: getTweets getTweet
klu0926 Jun 7, 2023
a73f58b
feat: add get users tweets
kim1037 Jun 7, 2023
66780ce
Merge pull request #9 from klu0926/tweet-api
kim1037 Jun 7, 2023
74f26c7
Merge branch 'master' into get-user-tweets
kim1037 Jun 7, 2023
ad85fab
feat: add user get replies
kim1037 Jun 7, 2023
fe7385d
Merge pull request #10 from klu0926/get-user-tweets
klu0926 Jun 7, 2023
1bc5a57
feat: add getReplies and postReply
klu0926 Jun 7, 2023
80d3759
Merge pull request #11 from klu0926/tweet-reply
kim1037 Jun 7, 2023
2e58d4f
feat: add length check for postTweet, and bring replies array for get…
klu0926 Jun 7, 2023
c816128
Merge pull request #12 from klu0926/fix-tweet
kim1037 Jun 7, 2023
98d17e7
feat: add get user liked tweets
kim1037 Jun 7, 2023
97b803b
Merge branch 'master' into get-user-likes
kim1037 Jun 7, 2023
d08d735
fix: revise order condition by createdAt
kim1037 Jun 7, 2023
53619b6
Merge pull request #13 from klu0926/get-user-likes
klu0926 Jun 7, 2023
bbbb48a
feat: add Followship associate
kim1037 Jun 7, 2023
2a755fb
feat: add get user followers and get followings
kim1037 Jun 7, 2023
fef6629
Merge pull request #14 from klu0926/get-user-followship
klu0926 Jun 8, 2023
18b9cfb
heroku setup
klu0926 Jun 8, 2023
484c373
set up root url
klu0926 Jun 8, 2023
c38321d
Merge pull request #15 from klu0926/heroku-setup
kim1037 Jun 8, 2023
ecc5a7d
fix: _helpers.js and test pass
kim1037 Jun 8, 2023
0dff4de
Merge pull request #16 from klu0926/fix-test-bug
klu0926 Jun 8, 2023
bc2b423
feat: add cors
klu0926 Jun 8, 2023
b334657
Merge pull request #17 from klu0926/cors
kim1037 Jun 8, 2023
1e43372
feat: like & unlike
klu0926 Jun 8, 2023
75a7488
feat: set directory and file-helper
kim1037 Jun 8, 2023
4c383ea
Merge branch 'like' of https://github.com/klu0926/twitter-api-2023 in…
kim1037 Jun 8, 2023
8752e97
Merge pull request #18 from klu0926/like
kim1037 Jun 8, 2023
a9edf72
feat: followship
klu0926 Jun 8, 2023
3454ddb
feat: add put user setting data
kim1037 Jun 8, 2023
c9d2be4
fix: deleteFollowship use followingId
klu0926 Jun 8, 2023
5739eb5
feat: add introduction attribute to putUserSetting
kim1037 Jun 8, 2023
9209d6e
Merge pull request #20 from klu0926/followships
kim1037 Jun 8, 2023
b375c41
Merge branch 'master' into put-user
kim1037 Jun 8, 2023
5dacda4
feat: add multer and put user pfofile route
kim1037 Jun 8, 2023
7844e72
Merge pull request #21 from klu0926/put-user
klu0926 Jun 8, 2023
40af9c2
fix user api response message
klu0926 Jun 8, 2023
d8c87a3
Merge pull request #22 from klu0926/fix-user
kim1037 Jun 8, 2023
525aaa9
feat: add topFollowers and add current user liked, followed state to …
kim1037 Jun 8, 2023
f0c63d3
Merge pull request #23 from klu0926/get-topFollowers
klu0926 Jun 8, 2023
a52bb4e
feat: add admin get users and delete tweet
kim1037 Jun 9, 2023
26288f5
feat: add tweets ondelete attribute
kim1037 Jun 9, 2023
8694946
Merge pull request #24 from klu0926/admin-api
klu0926 Jun 9, 2023
c66670b
fix: pass user test except fn getUserLikes
kim1037 Jun 9, 2023
0089ca2
fix: pass admin test
kim1037 Jun 9, 2023
688c71f
fix: change postLike postUnlike to fit spec
klu0926 Jun 9, 2023
a189e90
fix: remove isLike attribute
kim1037 Jun 9, 2023
54f6cfa
Merge branch 'test-tweet' of https://github.com/klu0926/twitter-api-2…
kim1037 Jun 9, 2023
a40bb61
Merge pull request #25 from klu0926/test-tweet
kim1037 Jun 9, 2023
346ce28
Merge pull request #26 from klu0926/user-api-test
klu0926 Jun 9, 2023
8146469
ac-test
klu0926 Jun 9, 2023
15133c7
ac-test
klu0926 Jun 9, 2023
7823857
travis log
klu0926 Jun 9, 2023
a13079a
Merge pull request #27 from klu0926/ac-test
kim1037 Jun 9, 2023
d80ce83
wip
klu0926 Jun 9, 2023
3001dd5
use account instead of email
klu0926 Jun 9, 2023
bb3bc6c
Merge pull request #28 from klu0926/travis-2
kim1037 Jun 9, 2023
b4e7cea
remove: isLike migration
kim1037 Jun 9, 2023
83f48d6
fix: error-handler
kim1037 Jun 9, 2023
5fc7967
fix: remove isLike attributes
kim1037 Jun 9, 2023
386d308
Merge pull request #29 from klu0926/fix-getUserLikes
klu0926 Jun 9, 2023
e54525a
wip
klu0926 Jun 9, 2023
f34e1ef
fix gerUserLike to include original tweet attributes
klu0926 Jun 10, 2023
611f17d
delete client file
klu0926 Jun 10, 2023
ac28dd5
fix putUserSetting
klu0926 Jun 10, 2023
002c27b
Merge pull request #31 from klu0926/fix-putUserSetting
kim1037 Jun 10, 2023
1901172
fix:error-handler
kim1037 Jun 10, 2023
60689bf
Merge pull request #32 from klu0926/fix-putUserSetting
klu0926 Jun 10, 2023
a8223fa
feat: add avatar and cover default value
kim1037 Jun 10, 2023
78f20a8
fix: revise seeder image url
kim1037 Jun 10, 2023
d350d58
Merge pull request #33 from klu0926/fix-models
klu0926 Jun 10, 2023
92fa76f
doc: add readme
klu0926 Jun 10, 2023
f9e1e3d
doc: add more readme
klu0926 Jun 10, 2023
534ae27
Merge pull request #34 from klu0926/readme
kim1037 Jun 10, 2023
f4bdc79
fix: admin getUsers & users get topFollowers
kim1037 Jun 11, 2023
314aad2
fix: comment out admin receivedLikedCount
kim1037 Jun 11, 2023
7520a56
Merge pull request #35 from klu0926/fix-admin-api
klu0926 Jun 12, 2023
6f713dd
fix:add isCurrentUserLiked to getTweets and getTweet
kim1037 Jun 12, 2023
7659480
Merge pull request #36 from klu0926/fix-tweet-2
klu0926 Jun 12, 2023
680b811
fix login message
klu0926 Jun 12, 2023
64a2bfa
fix login message
klu0926 Jun 12, 2023
137876f
Merge pull request #38 from klu0926/fix-login-message
kim1037 Jun 12, 2023
8d330f5
fix: change the default cover
kim1037 Jun 12, 2023
d936375
Merge pull request #39 from klu0926/fix-cover
klu0926 Jun 12, 2023
3d09d2d
fix putUserProfile check file?.avatar?
klu0926 Jun 13, 2023
533f1ec
Merge pull request #40 from klu0926/fix-avatar-cover
kim1037 Jun 13, 2023
3b16502
feat: add front end page to cors
klu0926 Jun 13, 2023
962a5dc
Merge pull request #41 from klu0926/cors
kim1037 Jun 13, 2023
0191c62
fix user model sequence
kim1037 Jun 13, 2023
940af5d
Merge pull request #42 from klu0926/fix-model
klu0926 Jun 13, 2023
6ee16ab
fix: change cors to allow all access
klu0926 Jun 13, 2023
936a01d
Merge pull request #43 from klu0926/cors-all
kim1037 Jun 13, 2023
cd2087d
feat: add admin get tweets
klu0926 Jun 13, 2023
ca2dc44
Merge pull request #44 from klu0926/admin-get-tweets
kim1037 Jun 13, 2023
aeee5c8
fix: change isCurrentUserFollower checking
klu0926 Jun 14, 2023
9e2affe
delete array length check
klu0926 Jun 14, 2023
f656b6a
add sql query
klu0926 Jun 14, 2023
b2a5f3b
chore: add comments
klu0926 Jun 14, 2023
2702b72
fix: fix npm test script
klu0926 Jun 14, 2023
bb7ce38
Merge pull request #45 from klu0926/FIX-isCurrentUserFollower2
kim1037 Jun 14, 2023
095d004
feat: add a new route adimn/usersdata
kim1037 Jun 14, 2023
bdea25a
fix: change account exsit message
kim1037 Jun 14, 2023
52ad0d0
Merge pull request #46 from klu0926/fix-adminUsers
klu0926 Jun 14, 2023
395508c
fix readme.md
kim1037 Jun 14, 2023
c41c982
fix: cover default img
kim1037 Jun 15, 2023
c680066
Merge pull request #47 from klu0926/fix-readme
klu0926 Jun 15, 2023
bcca446
feat: getUserData add tweet count
klu0926 Jun 16, 2023
13acb28
Merge pull request #48 from klu0926/user-tweetCount
kim1037 Jun 16, 2023
8e82d5b
fix top follower
klu0926 Jun 16, 2023
9a7802f
fix user seeder avatar
klu0926 Jun 16, 2023
b3df96a
Merge pull request #49 from klu0926/fix-setting
kim1037 Jun 16, 2023
e1999fa
feat: add tweet own
klu0926 Jun 16, 2023
9470e65
feat:add tweet.user
klu0926 Jun 16, 2023
0f9a4da
Merge pull request #50 from klu0926/fix-replies
kim1037 Jun 16, 2023
f1b61d5
fix: getUserLike isCurrentUserLiked logic
klu0926 Jun 17, 2023
c0bf3ce
feat: add tweet.user in getUserReplies
klu0926 Jun 17, 2023
8faaace
Merge pull request #51 from klu0926/fix-getUserLike
kim1037 Jun 17, 2023
9cc7f06
fix:amount of user seeder
kim1037 Jun 17, 2023
35e05a3
Merge pull request #52 from klu0926/fix-userSeeder
klu0926 Jun 17, 2023
e616690
fix: userId to UserId
kim1037 Jun 18, 2023
e329c3d
fix: renew readme.md
kim1037 Jun 18, 2023
99de0e1
Merge pull request #54 from klu0926/fix-UserId
klu0926 Jun 18, 2023
6b41857
fix: error message
klu0926 Jun 19, 2023
3ac83c3
fix: login message
klu0926 Jun 19, 2023
69bb319
fix
klu0926 Jun 19, 2023
d647af4
fix
klu0926 Jun 19, 2023
e2031b3
fix
klu0926 Jun 19, 2023
12f763f
Merge pull request #55 from klu0926/fix-message
zebrrrra Jun 19, 2023
2a12c5a
feat:add socket folder & create socket server
kim1037 Jun 28, 2023
cae2c38
Merge pull request #56 from klu0926/chatroom
klu0926 Jun 28, 2023
d36c3f5
feat: add event listner to socket/index.js
kim1037 Jun 28, 2023
9344e8b
Merge pull request #57 from klu0926/socket-index
klu0926 Jun 29, 2023
b2c6066
feat: socket events
klu0926 Jun 29, 2023
d8f2531
npm install
klu0926 Jun 29, 2023
3d1315f
Merge pull request #58 from klu0926/socket-event
kim1037 Jun 29, 2023
16cc540
feat: add error message in join and leave
klu0926 Jun 29, 2023
567d456
Merge pull request #59 from klu0926/add-error-message
kim1037 Jun 29, 2023
cbcc1fa
fix: socket cores
klu0926 Jul 2, 2023
3a265b0
Merge pull request #60 from klu0926/socket-cores
kim1037 Jul 2, 2023
9584252
feat:add room migration
kim1037 Jul 3, 2023
004a079
feat: add room model
kim1037 Jul 3, 2023
1610fee
feat:add chat migration
kim1037 Jul 3, 2023
37bfe7f
feat: on socket disconnect server-leave the user
klu0926 Jul 3, 2023
8c90c44
fix: use broadcast for usersInPublic instead of to everyone
klu0926 Jul 3, 2023
9649202
Merge pull request #61 from klu0926/add-auto-leave
kim1037 Jul 3, 2023
86a1871
Merge branch 'master' into chat-table
kim1037 Jul 3, 2023
db92f55
feat: add store historical message
kim1037 Jul 3, 2023
0184f80
fix: room id cannot find
kim1037 Jul 3, 2023
44d6ba3
Merge pull request #62 from klu0926/chat-table
klu0926 Jul 3, 2023
99d6e24
test
kim1037 Jul 4, 2023
b80b3e4
Merge branch 'master' of https://github.com/klu0926/twitter-api-2023
kim1037 Jul 4, 2023
761a8ca
fix: change heroku set up
klu0926 Jul 5, 2023
03ea40f
change db url
klu0926 Jul 5, 2023
c267de4
Merge pull request #63 from klu0926/new-heroku
kim1037 Jul 5, 2023
9d9c33b
Merge branch 'master' of https://github.com/klu0926/twitter-api-2023
klu0926 Jul 5, 2023
f9fb84d
feat: add user disconnect setTimout
klu0926 Jul 5, 2023
820c922
Merge pull request #64 from klu0926/fix-disconnect
kim1037 Jul 5, 2023
48a2706
Merge branch 'master' of https://github.com/klu0926/twitter-api-2023
kim1037 Jul 5, 2023
681a60f
feat: add records
kim1037 Jul 5, 2023
394aec8
Merge pull request #65 from klu0926/chat-record
klu0926 Jul 5, 2023
6ad3608
feat: add getRoom event, and change helpers functions
klu0926 Jul 8, 2023
651fbc4
feat: reconnect update users socket.id
klu0926 Jul 8, 2023
977d944
doc: clear some comments
klu0926 Jul 8, 2023
a71e0be
Merge pull request #66 from klu0926/getRoom
kim1037 Jul 9, 2023
b1c6fd2
feat:join socket to rooms on client-join
klu0926 Jul 9, 2023
41c057c
feat: send direct message, and join target to room
klu0926 Jul 9, 2023
5f67753
fix: fix some bugs in logic
klu0926 Jul 9, 2023
9708df0
fix: getRoom return roomId as string
klu0926 Jul 9, 2023
c1bacc7
feat: add room and chat seeds
kim1037 Jul 9, 2023
2c285db
fix: chat code
kim1037 Jul 9, 2023
f02eda6
Merge pull request #67 from klu0926/joinRoom
kim1037 Jul 9, 2023
83339bd
Merge branch 'master' into room-seed
kim1037 Jul 9, 2023
d94ffbb
fix: same users in different room bug
kim1037 Jul 10, 2023
d08b5d0
fix: public room id
kim1037 Jul 10, 2023
9399dac
fix:remove defualt public id
kim1037 Jul 10, 2023
1ba7921
Merge pull request #68 from klu0926/room-seed
klu0926 Jul 10, 2023
a6158a8
refactor: client-get-room property
klu0926 Jul 10, 2023
5d0794e
fix: record.js
kim1037 Jul 10, 2023
2cc019e
feat: add error message in sendMessage.js
kim1037 Jul 10, 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.example
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/*
/tests/*
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.DS_Store
*.DS_Store
frontend-test.html

# Logs
logs
Expand Down Expand Up @@ -42,6 +43,7 @@ build/Release
# Dependency directories
node_modules/
jspm_packages/
temp/*

# TypeScript v1 declaration files
typings/
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ services:

# 設定參數(Travis CI 會按照參數分別執行)
env:
- NODE_ENV=travis
- NODE_ENV=travis JWT_SECRET=alphacamp

# 在 install 前執行的指令
before_install:
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
112 changes: 112 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# Simple Twitter

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

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

## API - 接口文件
文件內提供每個街口的的使用方式,回傳,成功跟失敗的回傳資訊:<br>
https://www.notion.so/API-c1a90264a00848d2af202689ae0148be
<br><br>

## Environment - 開發環境
* node v14.16.0
* nodemon
<br><br>
## Installation and Execution - 安裝與執行步驟
### 1. 開啟Terminal, Clone此專案至本機:
```
git clone https://github.com/klu0926/twitter-api-2023.git
```
### 2. 進入存放此專案的資料夾
```
cd twitter-api-2023
```
### 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.example)
```
IMGUR_CLIENT_ID= 你的密碼
JWT_SECRET= 你的密碼
```
### 7. 啟動伺服器 (使用 nodemon)
```
npm run dev
```

### 8. Terminal出現以下字樣代表成功啟動!
```
Example app listening on port 3000!
```
<br>

## Seed Accounts - 測試帳號
專案提供 1 個後台管理者帳號與跟 9 位使用者帳號<br>
管理者帳號只可以使用後台功能,使用者帳號只能使用前台功能
### # 管理者帳號 - 後台
account: root <br>
email: [email protected] <br>
password: 12345678 <br>
### # 使用者帳號 - 前台
account: user1 ~ user9 <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: "^10.0.0",
* express: "^4.16.4",
* express-session: "^1.15.6",
* faker: "^4.1.0",
* 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",
* passport-local: "^1.0.0",
* sequelize: "^6.18.0",
* sequelize-cli: "^5.5.0",
* sinon: "^10.0.0",
* sinon-chai: "^3.3.0"
<br><br>

## Team - 團隊成員
### 前端
[zebrrrra](https://github.com/zebrrrra)<br>
[Jena Lin](https://github.com/J6127)
### 後端
[kim1037](https://github.com/kim1037)<br>
[klu0926](https://github.com/klu0926)


9 changes: 4 additions & 5 deletions _helpers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

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

module.exports = {
getUser,
};
getUser
}
53 changes: 44 additions & 9 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,50 @@
const express = require('express')
const helpers = require('./_helpers');
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}

const express = require('express')
const routes = require('./routes')
const methodOverride = require('method-override')
const passport = require('./config/passport')
const cors = require('cors')
const app = express()
const port = 3000

// use helpers.getUser(req) to replace req.user
function authenticated(req, res, next){
// passport.authenticate('jwt', { ses...
};
// create socket.io server
const { createServer } = require('http')
const httpServer = createServer(app)
const io = require('socket.io')(httpServer, {
cors: {
origin: '*',
credentials: true
}
})
const useSocket = require('./socket/index')
const port = process.env.PORT || 3000

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

// cors
app.use(
cors({
// origin: 'https://zebrrrra.github.io',
origin: '*',
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH']
})
)

// routes
app.get('/', (req, res) => { res.send('Welcome to the real world!') })
app.use(routes)

// use socket modules
useSocket(io)

app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
// start
httpServer.listen(port, () => console.log(`Example app listening 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": "CLEARDB_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
22 changes: 22 additions & 0 deletions config/strategies/jwt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { User } = require('../../models')
const passportJWT = require('passport-jwt')
const JwtStrategy = passportJWT.Strategy
const ExtractJwt = passportJWT.ExtractJwt

const jwtOptions = {
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: process.env.JWT_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')
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.get())
})
.catch(err => done(err, false))
})
}))
}
Loading