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

[前後分離組]于旺、Yan.Y、ET #846

Open
wants to merge 226 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
226 commits
Select commit Hold shift + click to select a range
a9d0fba
feat: add ESlint setting
Ray7808 Jun 3, 2023
a0e4bce
Merge pull request #1 from s1030905/feature/a
s1030905 Jun 4, 2023
aec8dbd
feat: use ESLint to adjust the format and set associate id
Ray7808 Jun 4, 2023
7588766
Merge pull request #2 from s1030905/feature/c
s1030905 Jun 4, 2023
21ad13c
add .env, .env.test, .env.example, set scripts for windows
s1030905 Jun 5, 2023
4b880eb
add relationship between models
s1030905 Jun 5, 2023
5a0812c
create userseeds, add passport
s1030905 Jun 5, 2023
ff5427e
debug Reply
s1030905 Jun 5, 2023
b0f23bb
passport debug, models/index undefined
s1030905 Jun 5, 2023
99b5af8
debug models underscored part
s1030905 Jun 5, 2023
2c9651f
Merge pull request #3 from s1030905/feature/user
Ray7808 Jun 5, 2023
2d759b8
remove login user password in json
s1030905 Jun 5, 2023
9389fad
feat: add jwt and error-handler.js
Ray7808 Jun 5, 2023
8ab38af
Merge pull request #5 from s1030905/feature/user2
s1030905 Jun 5, 2023
19d1fd0
fix: remove the unwanted path
Ray7808 Jun 5, 2023
56bbb96
feat: add seeder for tweets
Ray7808 Jun 5, 2023
b9f44c3
add signup & login fuinction and revise relationship of models
s1030905 Jun 6, 2023
76a6cd7
Merge branch 'master' into feature/user
s1030905 Jun 6, 2023
0f8b801
Merge pull request #7 from s1030905/feature/user
s1030905 Jun 6, 2023
051a18b
Merge pull request #6 from s1030905/feature/tweet1
Ray7808 Jun 6, 2023
dc485bb
add logout function
s1030905 Jun 6, 2023
d78fa7e
add adminLogin feature
s1030905 Jun 6, 2023
9d290e2
Merge branch 'master' of https://github.com/s1030905/twitter-api-2023…
s1030905 Jun 6, 2023
23195fb
test
s1030905 Jun 6, 2023
34a84e4
Merge pull request #8 from s1030905/feature/user
s1030905 Jun 6, 2023
848c434
fix: check the relation
Ray7808 Jun 6, 2023
acc90b2
Merge branch 'feature/tweet2'
Ray7808 Jun 6, 2023
21ea22c
feat: pass all models/tweet testing
Ray7808 Jun 6, 2023
56e76c2
Merge pull request #9 from s1030905/feature/tweet3
s1030905 Jun 6, 2023
ad83de0
fix: trying to get user info using post /api/tweets
Ray7808 Jun 6, 2023
64e98ed
Merge pull request #10 from s1030905/feature/tweet4
s1030905 Jun 6, 2023
e9bf8a2
feat: modify config for heroku
s1030905 Jun 7, 2023
6e5998a
Merge pull request #11 from s1030905/feature/user
s1030905 Jun 7, 2023
5e882c6
for heroku test
s1030905 Jun 7, 2023
aff9589
remove jwt
s1030905 Jun 7, 2023
57abf78
remove passport jwt
s1030905 Jun 7, 2023
bfdc06b
test
s1030905 Jun 7, 2023
43f3b1e
test heroku
s1030905 Jun 7, 2023
125f466
Merge pull request #13 from s1030905/heroku-test
s1030905 Jun 7, 2023
c81ddd1
remove model user attribures allownNull
s1030905 Jun 7, 2023
fcab436
rebuild models and all pass test/models
s1030905 Jun 7, 2023
f8b139b
Merge pull request #14 from s1030905/rebuild-models
s1030905 Jun 7, 2023
efbd6a1
Merge branch 'master' of https://github.com/s1030905/twitter-api-2023
s1030905 Jun 7, 2023
95a5194
rebuild seeders
s1030905 Jun 7, 2023
8652aa1
Merge pull request #15 from s1030905/seeders
s1030905 Jun 7, 2023
7578ccc
feat: add user inside the res.locals
Ray7808 Jun 7, 2023
7a06e7a
feat: add postTweet func
Ray7808 Jun 7, 2023
58cb7b0
Merge pull request #16 from s1030905/feature/tweet5
s1030905 Jun 7, 2023
15863ff
feat: add some file
Ray7808 Jun 7, 2023
40ddf24
feat: add some file
Ray7808 Jun 7, 2023
6db7e52
change status failed to error
s1030905 Jun 7, 2023
bb3cc4c
feat: add process.env.PORT
Ray7808 Jun 7, 2023
12026d7
feat: admin getUsers
s1030905 Jun 7, 2023
c983fff
feat: add session
Ray7808 Jun 7, 2023
6bede94
Merge pull request #17 from s1030905/admin
s1030905 Jun 7, 2023
8c33516
Merge pull request #18 from s1030905/herokutest
s1030905 Jun 7, 2023
745651d
feat: add getTweets and getTweet func
Ray7808 Jun 8, 2023
8f7911c
feat: getTweets & deleteTweet
s1030905 Jun 8, 2023
634a3cb
feat: admin rolechecker
s1030905 Jun 8, 2023
b8fa9f1
feat: add authenticatedAdmin
s1030905 Jun 8, 2023
b5cc20e
Merge pull request #20 from s1030905/admin
s1030905 Jun 8, 2023
59b97b1
getTweets by user.id
s1030905 Jun 8, 2023
38a883b
Merge pull request #21 from s1030905/main
s1030905 Jun 8, 2023
83b7df2
feat: getUser, getUserTweets, getUserRepliedTweet
s1030905 Jun 8, 2023
6ba5384
add: replies seeds and rebuild tweets seeds
s1030905 Jun 8, 2023
787bbf4
resolve getUserTweets and passport login promblem
s1030905 Jun 8, 2023
9cf45d1
Merge pull request #22 from s1030905/users/feature1
s1030905 Jun 8, 2023
1cc8667
resolve getUserRepliedTweet & getUserTweets problem
s1030905 Jun 8, 2023
b55335b
fix: solve the conflict
Ray7808 Jun 8, 2023
367d770
feat: add passport cb error
s1030905 Jun 8, 2023
601ca2f
feat: add passport-jwt cb error
s1030905 Jun 8, 2023
51ead6e
Merge pull request #24 from s1030905/feature/user3
s1030905 Jun 8, 2023
c6f08b8
feat add reply GET /api/tweets/:tweet_id/replies and POST /api/tweets…
Ray7808 Jun 8, 2023
728f7fb
Merge pull request #25 from s1030905/reply1
s1030905 Jun 8, 2023
601feb8
feat: add likeTweet and unlikeTweet
Ray7808 Jun 8, 2023
4cefb33
feat: add likeTweet and unlikeTweet and revisd the Id
Ray7808 Jun 8, 2023
4bfa395
Merge pull request #26 from s1030905/like1
s1030905 Jun 8, 2023
778a21c
Merge pull request #27 from s1030905/like2
s1030905 Jun 8, 2023
39e6ec6
feat: getUserRepliedTweet join Tweets to get tweet
s1030905 Jun 8, 2023
94a5a32
Merge branch 'master' of https://github.com/s1030905/twitter-api-2023…
s1030905 Jun 8, 2023
f64a7bd
feat: add cors
Ray7808 Jun 8, 2023
2076a0c
Merge pull request #28 from s1030905/cors
s1030905 Jun 9, 2023
397a9e2
Merge branch 'master' of https://github.com/s1030905/twitter-api-2023…
s1030905 Jun 9, 2023
ef90229
feat: postFollowship & deleteFollowship
s1030905 Jun 9, 2023
781e9fa
Merge pull request #29 from s1030905/feature/followship1
s1030905 Jun 9, 2023
7c1d00c
feat: replace errormessage with thorw new Error
s1030905 Jun 9, 2023
d3180e2
Merge pull request #30 from s1030905/feature/followship2
s1030905 Jun 9, 2023
8ddcdae
login, adminLogin and signup error status code 200
s1030905 Jun 9, 2023
76d113d
Merge pull request #31 from s1030905/featrure/passport1
s1030905 Jun 9, 2023
8c74ffb
feat: add like seeds and followship seeds
s1030905 Jun 9, 2023
8a95ab2
feat : getUserLiked, getUserFollows, getUserFollowers
s1030905 Jun 9, 2023
1466c41
fix: finish one pass from requests/admin
Ray7808 Jun 9, 2023
3eff110
fix: revise like and unlike to post
Ray7808 Jun 9, 2023
1a4327d
fix: revise return json format
Ray7808 Jun 9, 2023
2615196
Merge pull request #32 from s1030905/test1
s1030905 Jun 9, 2023
fee027d
Merge pull request #33 from s1030905/test2
s1030905 Jun 9, 2023
6c215a3
feat: editUser. add: middleware multer
s1030905 Jun 9, 2023
eada926
Merge branch 'master' of https://github.com/s1030905/twitter-api-2023…
s1030905 Jun 10, 2023
04adeca
Merge pull request #34 from s1030905/feature/user4
s1030905 Jun 10, 2023
a81aa75
fix: revise the format to pass the test
Ray7808 Jun 10, 2023
dad2c81
fix: remove the imgur part
Ray7808 Jun 10, 2023
410da9c
fix: add imgurFileHandler func
Ray7808 Jun 10, 2023
629de49
Merge pull request #35 from s1030905/test3
s1030905 Jun 10, 2023
d892620
fix: remove the unwanted format
Ray7808 Jun 10, 2023
b3b98a9
Merge pull request #36 from s1030905/test4
s1030905 Jun 10, 2023
f220e98
feat: add followingCount & followerCount to getUser
s1030905 Jun 11, 2023
55bd0c6
add followerCount & followingCount to admin/getUsers
s1030905 Jun 11, 2023
4451d21
mdoel: add likedCount to Tweet
s1030905 Jun 11, 2023
c9b2ea7
feat: getUsers add followerCount, followingCount, userTweetCount and …
s1030905 Jun 11, 2023
c74a3b2
Merge branch 'master' into feature/admin1
s1030905 Jun 11, 2023
9d7eb71
Merge pull request #37 from s1030905/feature/admin1
s1030905 Jun 11, 2023
db941c7
fix: add seeder which each tweet has 3 replies and each user has at l…
Ray7808 Jun 11, 2023
810003e
fix: check when editUser need to deal with the account and email
Ray7808 Jun 11, 2023
a3ec3a5
redefine error status code
s1030905 Jun 11, 2023
593ce9c
Merge pull request #38 from s1030905/refactor1
s1030905 Jun 11, 2023
ccf8599
Merge pull request #39 from s1030905/admin2
s1030905 Jun 11, 2023
f4dc1bf
fix: add relation between different model
Ray7808 Jun 11, 2023
ad7125b
Merge pull request #40 from s1030905/refactor2
s1030905 Jun 11, 2023
c5a0d24
feat: update remaining status code
s1030905 Jun 12, 2023
28593e6
Merge pull request #41 from s1030905/feature/fix1
s1030905 Jun 12, 2023
b5ace7a
Create README.md
s1030905 Jun 12, 2023
1727f9c
add repliedCount to Tweet
s1030905 Jun 12, 2023
76811b6
Merge branch 'master' of https://github.com/s1030905/twitter-api-2023…
s1030905 Jun 12, 2023
95fbaa7
Merge pull request #42 from s1030905/feature/fix2
s1030905 Jun 12, 2023
40cc9fd
include model by attributes
s1030905 Jun 13, 2023
762a98a
Merge branch 'master' of https://github.com/s1030905/twitter-api-2023…
s1030905 Jun 13, 2023
87472bc
Merge pull request #43 from s1030905/feature/fix3
s1030905 Jun 13, 2023
5969edd
fix: multer setting and upload file func
Ray7808 Jun 13, 2023
363b16d
Merge pull request #44 from s1030905/feature/user3
s1030905 Jun 13, 2023
098c141
fix: revise user-controller/edutUserProfile to pass the test
Ray7808 Jun 13, 2023
9f4cc1c
Merge pull request #45 from s1030905/test5
s1030905 Jun 13, 2023
f382959
fix: revise the user background url inside seeders
Ray7808 Jun 13, 2023
1d0cc76
fix: 1. add if fun inside api-auth.js that admin cannot postTweet, fo…
Ray7808 Jun 13, 2023
41bad0d
Merge pull request #46 from s1030905/test6
s1030905 Jun 13, 2023
b8893d8
revise postTweet, postFollowship, editUserProfile
s1030905 Jun 13, 2023
9176da1
Merge branch 'master' of https://github.com/s1030905/twitter-api-2023…
s1030905 Jun 13, 2023
96102d7
remove never used variable
s1030905 Jun 13, 2023
26ce7c9
Merge pull request #47 from s1030905/feature/fix4
s1030905 Jun 13, 2023
1bcab6f
add user top 10
s1030905 Jun 14, 2023
e4f368a
Merge pull request #48 from s1030905/feature/user5
s1030905 Jun 14, 2023
e352fe2
post like count number + 1
s1030905 Jun 14, 2023
9dcd8f8
add TweetUser info at getUserLiked
s1030905 Jun 14, 2023
a6ddfa3
remove user from top10 list and add isFollowed status
s1030905 Jun 14, 2023
328b10a
Merge pull request #49 from s1030905/feature/user5
s1030905 Jun 14, 2023
cbfdf29
remove introduction from putUserSetting
s1030905 Jun 15, 2023
52baeaa
feat: add comment at getTweet api
s1030905 Jun 15, 2023
5c01ac7
fix putUserSetting
s1030905 Jun 15, 2023
4c0aa9a
Merge branch 'master' of https://github.com/s1030905/twitter-api-2023…
s1030905 Jun 15, 2023
38f8409
Merge pull request #50 from s1030905/feature/fix5
s1030905 Jun 15, 2023
656b035
caculate likedCount and repliedCount
s1030905 Jun 15, 2023
82449d0
feat: add LikedCount and repliedCount
s1030905 Jun 16, 2023
37f2aa0
add required attributes on getUsers and getTweets
s1030905 Jun 16, 2023
2c95324
Merge branch 'master' of https://github.com/s1030905/twitter-api-2023…
s1030905 Jun 16, 2023
05b25ea
Merge pull request #51 from s1030905/feature/fix5
s1030905 Jun 16, 2023
00ff1cf
feat: following-btn and like-btn status
s1030905 Jun 16, 2023
fbc64d9
Merge pull request #52 from s1030905/feature/fix6
s1030905 Jun 16, 2023
9cbe288
revise getTweets isLiked status by hash
s1030905 Jun 16, 2023
e01298a
none
s1030905 Jun 16, 2023
0aafa78
fix: revise getUserLiked
Ray7808 Jun 16, 2023
fd784fc
Merge pull request #53 from s1030905/test7
s1030905 Jun 16, 2023
21e10bc
revise getUserLiked by hash
s1030905 Jun 16, 2023
8218d88
Merge branch 'master' of https://github.com/s1030905/twitter-api-2023…
s1030905 Jun 16, 2023
3a4b3f7
revise getUserFollows
s1030905 Jun 16, 2023
cc1ca14
Merge pull request #54 from s1030905/feature/fix6
s1030905 Jun 16, 2023
7c8aedd
revise getUserLiked and getUser
s1030905 Jun 17, 2023
5a7e900
Merge pull request #55 from s1030905/feature/fix7
s1030905 Jun 17, 2023
4d86efc
revise unlikeTweet
s1030905 Jun 17, 2023
7949853
Merge pull request #56 from s1030905/feature/fix7
s1030905 Jun 17, 2023
570389c
sort getUserLiked, getUserRepliedTweet, getUserTweets
s1030905 Jun 17, 2023
97e5fb6
Merge branch 'master' of https://github.com/s1030905/twitter-api-2023…
s1030905 Jun 17, 2023
8a6f3e8
Merge pull request #57 from s1030905/feature/fix7
s1030905 Jun 17, 2023
fd41ea1
revise login error msg
s1030905 Jun 17, 2023
d2aa270
rebuild routes
s1030905 Jun 17, 2023
d97c3f0
Merge pull request #58 from s1030905/feature/fix8
s1030905 Jun 17, 2023
9135795
add required attributes on getReply
s1030905 Jun 17, 2023
e583bf9
Merge pull request #59 from s1030905/feature/fix8
s1030905 Jun 17, 2023
de50b58
fix: add createdAt for replied and add password or account error mess…
Ray7808 Jun 17, 2023
2688120
Merge branch 'master' into test8
s1030905 Jun 17, 2023
d05926c
Merge pull request #60 from s1030905/test8
s1030905 Jun 17, 2023
da178f6
no auth in admin router
s1030905 Jun 17, 2023
18859b4
Merge pull request #61 from s1030905/feature/auth1
s1030905 Jun 17, 2023
6d02f43
revise getUserRepliedTweet
s1030905 Jun 18, 2023
ac3e342
Merge branch 'master' of https://github.com/s1030905/twitter-api-2023…
s1030905 Jun 18, 2023
0053c64
delete console
s1030905 Jun 18, 2023
f783d14
Merge pull request #62 from s1030905/feature/auth1
s1030905 Jun 18, 2023
46c2e2d
create dic on getUserRepliedTweet to fix null of length
s1030905 Jun 18, 2023
1d2e517
Merge pull request #63 from s1030905/feature/fix9
s1030905 Jun 18, 2023
9e4f8f2
deny root login and revise userSeeders
s1030905 Jun 18, 2023
13fb580
Merge pull request #64 from s1030905/feature/fix9
s1030905 Jun 18, 2023
97212a0
revise getUserLiked nest problem
s1030905 Jun 18, 2023
e624906
Merge pull request #65 from s1030905/feature/fix9
s1030905 Jun 18, 2023
9367c79
rebuild getReply
s1030905 Jun 18, 2023
53a521d
Merge pull request #66 from s1030905/feature/fix9
s1030905 Jun 18, 2023
321b5b8
revise adminChecker error msg
s1030905 Jun 18, 2023
90838c7
Merge pull request #67 from s1030905/feature/fix9
s1030905 Jun 18, 2023
ebf863b
fix: revise information and add cors setting
Ray7808 Jun 19, 2023
46d0527
Merge pull request #68 from s1030905/test10
s1030905 Jun 19, 2023
0833566
dix
s1030905 Jun 19, 2023
0a0836a
Merge pull request #69 from s1030905/feature/fix9
s1030905 Jun 19, 2023
f9019a4
fix: add cors setting
Ray7808 Jun 19, 2023
9609b91
Merge pull request #70 from s1030905/test11
s1030905 Jun 19, 2023
f3e0e4d
delete relationship of tweet
s1030905 Jun 20, 2023
1add0a8
Merge pull request #71 from s1030905/feature/fix10
s1030905 Jun 20, 2023
9a40d75
revise null of toJSON()
s1030905 Jun 20, 2023
24dccc5
Merge pull request #72 from s1030905/feature/fix10
s1030905 Jun 20, 2023
13d2555
revise corsOptions.
s1030905 Jun 20, 2023
af403ee
Merge pull request #73 from s1030905/feature/fix10
s1030905 Jun 20, 2023
ec85f61
revise 2
s1030905 Jun 20, 2023
bb433e9
Merge pull request #74 from s1030905/feature/fix10
s1030905 Jun 20, 2023
e6fc1bc
revise userSignUp
s1030905 Jun 20, 2023
ea7ea5a
Merge pull request #75 from s1030905/feature/fix10
s1030905 Jun 20, 2023
e76a425
revise introduction 160
s1030905 Jun 20, 2023
bf8ecfe
Merge pull request #76 from s1030905/feature/fix10
s1030905 Jun 20, 2023
aa69f2a
fix: add user totoal number
Ray7808 Jun 21, 2023
7e28eec
Merge pull request #77 from s1030905/feature/seed15
s1030905 Jun 21, 2023
1a44e6f
revise userFollower & userFollowing order
s1030905 Jun 21, 2023
faf5c40
Merge pull request #78 from s1030905/feature/fix11
s1030905 Jun 21, 2023
245f9cf
fix: avatar content and introduction limitation
Ray7808 Jun 22, 2023
c6e81e3
Merge pull request #79 from s1030905/feature/seed17
s1030905 Jun 22, 2023
d194954
fix introduction problem
s1030905 Jun 22, 2023
9716248
Merge pull request #80 from s1030905/feature/fix11
s1030905 Jun 22, 2023
53f5d53
feat: cancel whitelist
s1030905 Jul 17, 2023
b39941e
Merge pull request #81 from s1030905/feature/fix11
s1030905 Jul 17, 2023
59cb5f5
Update README.md
s1030905 Jul 19, 2023
dfbf83c
Update README.md
s1030905 Jul 19, 2023
a466384
Update README.md
s1030905 Jul 20, 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
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SESSION_SECRET=
JWT_SECRET=
PORT=
IMGUR_CLIENT_ID=
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
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/

#temp
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
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# twitter-api-2023
ALPHA Camp | 學期 3 | Simple Twitter | 自動化測試檔 (前後分離組)
# 前端網站:
https://s1030905.github.io/twitter-frontend-2023/
由 blueelsa 開發(https://github.com/bluelsa)

# Environment - 開發環境
node v14.16.0
nodemon

#Installation and Execution - 安裝與執行步驟
1. 創建資料夾
2. 安裝所需packages。指令`npm i`
3. 建立SQL資料庫。指令 `create database ac_twitter_workspace`
4. 建立資料表。指令 `npx sequelize db:migrate`
5. 建立種子檔。指令 `npx sequelize db:seed:all`
6. 建立環境參數(請參考.env.example)。
7. 啟動伺服器。指令`npm run dev`

# Note:
#管理者帳號:
account: root
password: 12345678

#使用者帳號:
account: user1
password: 12345678
17 changes: 13 additions & 4 deletions _helpers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@

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

function ensureAuthenticated (req) {
return req.isAuthenticated()
}

module.exports = {
getUser,
};
getUser, ensureAuthenticated
}

// use helpers.getUser(req) to replace req.user
// function authenticated (req, res, next) {
// // passport.authenticate('jwt', { ses...
// }
68 changes: 60 additions & 8 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,67 @@
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}

const path = require('path')
const express = require('express')
const helpers = require('./_helpers');
const passport = require('./config/passport')
const flash = require('connect-flash')
const { apis, pages } = require('./routes')
const methodOverride = require('method-override')
const session = require('express-session')
const cors = require('cors')

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

// 解析request主體
app.use(express.urlencoded({ extended: true }))
app.use(express.json())
app.use(methodOverride('_method'))
app.use('/upload', express.static(path.join(__dirname, 'upload')))

// session設定
app.use(
session({
secret: process.env.SESSION_SECRET || 'NonSecret',
resave: false,
saveUninitialized: true
})
)

// passport初始化
app.use(passport.initialize())
// app.use(passport.session())

// flash
app.use(flash())

// cors
const corsOptions = {
// origin: [
// 'http://localhost:3000',
// 'https://bluelsa.github.io',
// 'https://s1030905.github.io',
// 'https://4457-2001-b011-7003-76bd-f067-d1c2-c9bc-905.ngrok-free.app'
// ],
origin: '*',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS',
allowedHeaders: ['Content-Type', 'Authorization']
}
app.use(cors(corsOptions))

// locals
app.use((req, res, next) => {
res.locals.error_messages = req.flash('error_messages')
next()
})

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

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

module.exports = app
8 changes: 2 additions & 6 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 All @@ -34,4 +30,4 @@
"host": "127.0.0.1",
"dialect": "mysql"
}
}
}
72 changes: 71 additions & 1 deletion config/passport.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,75 @@
const passport = require('passport')
const LocalStrategy = require('passport-local').Strategy
const passportJWT = require('passport-jwt')
const JWTStrategy = passportJWT.Strategy
const ExtractJWT = passportJWT.ExtractJwt
const bcrypt = require('bcryptjs')
const { User } = require('../models')

// LocalStrategy Setting
passport.use(new LocalStrategy(
{ usernameField: 'account', passwordField: 'password', passReqToCallback: true },
async (req, account, password, cb) => {
try {
const accountError = '帳號不存在!'
const passwordError = '帳號或密碼輸入錯誤!'

const user = await User.findOne({ where: { account } })

module.exports = passport
// 帳號或密碼輸入錯誤 暫時的錯誤處理 status code 200
if (!user) {
// return cb(null, false)
return cb(accountError, false)
}
const isMatch = await bcrypt.compare(password, user.password)

// 帳號或密碼輸入錯誤 暫時的錯誤處理 status code 200
if (!isMatch) {
// return cb(null, false)
return cb(passwordError, false)
}
return cb(null, user)
} catch (error) {
cb(error)
}
}
))

// JWTStrategy Setting
const jwtOptions = {
jwtFromRequest: ExtractJWT.fromAuthHeaderAsBearerToken(),
secretOrKey: process.env.JWT_SECRET || 'secret'
}

passport.use(new JWTStrategy(jwtOptions, async (jwtPayload, cb) => {
try {
const errorMessage = 'unAuthenticated'
const user = await User.findByPk(jwtPayload.id
, {
include: [
// join table FollowShip
{ model: User, as: 'Followers' },
{ model: User, as: 'Followings' }
]
}
)
if (!user) return cb(errorMessage, false, { message: errorMessage })
return cb(null, user)
} catch (error) {
cb(error)
}
}))

// passport serializeUser & deserializeUser
passport.serializeUser((user, cb) => cb(null, user.id))

passport.deserializeUser(async (id, cb) => {
try {
const user = await User.findByPk(id)
return cb(null, user.toJSON())
} catch (error) {
cb(error)
}
})

module.exports = passport
98 changes: 98 additions & 0 deletions controllers/apis/admin-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
const jwt = require('jsonwebtoken')
const { User, Tweet, Like, Reply } = require('../../models')
const { getUser } = require('../../_helpers')

const adminController = {
adminLogin: async (req, res, next) => {
try {
const reqUser = getUser(req).toJSON()
const token = jwt.sign(reqUser, process.env.JWT_SECRET, { expiresIn: '30d' })
const { account } = req.body
const user = await User.findOne({
where: { account },
raw: true
})

// 錯誤處理
if (!user || user.role !== 'admin') {
const error = new Error('You are not admin')
error.status = 404
throw error
}
const userData = user
delete userData.password
userData.token = token
return res.status(200).json(userData)
} catch (error) {
next(error)
}
},
getUsers: async (req, res, next) => {
try {
const users = await User.findAll({
attributes: ['id', 'name', 'account', 'avatar', 'background'],
include: [
{ model: User, as: 'Followers' },
{ model: User, as: 'Followings' },
{ model: Tweet, as: 'UserTweets' }
]
})

const data = users.map(user => {
const { Followers, Followings, UserTweets, ...userData } = user.toJSON()
let likedCount = 0
for (const i of UserTweets) {
likedCount += i.likedCount
}
userData.followerCount = Followers.length
userData.followingCount = Followings.length
userData.userTweetCount = UserTweets.length
userData.likedCount = likedCount
return userData
})
const dataSorted = data.sort((a, b) => b.userTweetCount - a.userTweetCount)
return res.status(200).json(dataSorted)
} catch (error) {
next(error)
}
},
getTweets: async (req, res, next) => {
try {
const tweets = await Tweet.findAll({
include: [{ model: User, as: 'TweetUser', attributes: ['id', 'name', 'account', 'avatar'] }],
order: [['createdAt', 'DESC']]
})

// 50字快覽
const data = tweets.map(tweet => {
const { description, ...rest } = tweet.toJSON()
return {
...rest,
description: description.length >= 50 ? description.slice(0, 50) : description
}
})

return res.status(200).json(data)
} catch (error) {
next(error)
}
},
deleteTweet: async (req, res, next) => {
try {
const { id } = req.params
const tweet = await Tweet.findByPk(id)

if (!tweet) throw new Error('The tweet does not exist')
await Promise.all([
tweet.destroy(),
Like.destroy({ where: { TweetId: id } }),
Reply.destroy({ where: { TweetId: id } })
])
return res.status(200).json({ status: 'success', message: 'The tweet was successfully deleted' })
} catch (error) {
next(error)
}
}
}

module.exports = adminController
Loading