Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
124 commits
Select commit Hold shift + click to select a range
bf48439
feat: build models
av124773 Jun 8, 2023
d110775
R1 (#3)
jiasyuanchu Jun 9, 2023
f72c668
Merge branch 'master' of https://github.com/av124773/twitter-api-2020
jiasyuanchu Jun 9, 2023
94c44ea
feat: build models (#2)
av124773 Jun 9, 2023
f3e9cf2
Merge branch 'master' of https://github.com/av124773/twitter-api-2020
jiasyuanchu Jun 9, 2023
fb09120
R2 seeds (#4)
jiasyuanchu Jun 9, 2023
af3d002
Merge branch 'master' of https://github.com/av124773/twitter-api-2020
jiasyuanchu Jun 10, 2023
eda25b0
Tweet api (#7)
av124773 Jun 10, 2023
23dfb83
R3 auth (#5)
jiasyuanchu Jun 10, 2023
0364869
Merge branch 'master' of https://github.com/av124773/twitter-api-2020
jiasyuanchu Jun 10, 2023
7e45f52
feat: modify config for heroku (#8)
av124773 Jun 10, 2023
d4bc672
Merge branch 'master' of https://github.com/av124773/twitter-api-2020
jiasyuanchu Jun 10, 2023
4724569
feat: add env.PORT (#9)
av124773 Jun 10, 2023
27e2143
feat: add postTweet & deleteTweet
av124773 Jun 10, 2023
58eaec1
feat: add postReplies
av124773 Jun 11, 2023
23c7b68
feat: add postLike
av124773 Jun 11, 2023
b69afeb
Merge pull request #10 from av124773/tweet_api_2
jiasyuanchu Jun 11, 2023
8f219d4
Merge branch 'master' of https://github.com/av124773/twitter-api-2020
jiasyuanchu Jun 11, 2023
a9860a8
R5 auth by account (#11)
jiasyuanchu Jun 11, 2023
7167fb6
Merge branch 'master' of https://github.com/av124773/twitter-api-2020
jiasyuanchu Jun 11, 2023
c732271
feat: add signin error message
av124773 Jun 11, 2023
7181f34
Merge pull request #13 from av124773/build_errmsg
jiasyuanchu Jun 11, 2023
5a98a6b
feat: config cors
av124773 Jun 11, 2023
e8312e5
Merge pull request #14 from av124773/build_cors
jiasyuanchu Jun 11, 2023
076db2c
Merge branch 'master' of https://github.com/av124773/twitter-api-2020
jiasyuanchu Jun 11, 2023
7cf42eb
R4 user controller (#12)
jiasyuanchu Jun 12, 2023
eb62098
Merge branch 'master' of https://github.com/av124773/twitter-api-2020
jiasyuanchu Jun 12, 2023
0b21c8c
feat: add authAPI (#15)
av124773 Jun 12, 2023
6449328
feat: getUserReplies (#17)
jiasyuanchu Jun 12, 2023
9072861
feat: add postUnlike (#16)
av124773 Jun 12, 2023
11bb3bd
Merge branch 'master' of https://github.com/av124773/twitter-api-2020
jiasyuanchu Jun 12, 2023
185fc1e
feat: rebuild signin error message
av124773 Jun 12, 2023
7255908
Merge pull request #18 from av124773/rework_signin_errmsg
jiasyuanchu Jun 12, 2023
9fe0d5b
Merge branch 'master' of https://github.com/av124773/twitter-api-2020
jiasyuanchu Jun 12, 2023
18e4991
feat: add addFollowing and deleteFollowing
av124773 Jun 12, 2023
df05552
feat: add getUserLikes (#19)
jiasyuanchu Jun 12, 2023
c8c066d
Merge pull request #20 from av124773/followship_api
jiasyuanchu Jun 13, 2023
a9952d2
Merge branch 'master' of https://github.com/av124773/twitter-api-2020
jiasyuanchu Jun 13, 2023
9b01774
feat: add getTopUser
av124773 Jun 13, 2023
f22b96d
feat: exclude password
av124773 Jun 13, 2023
dc8d463
Merge pull request #22 from av124773/followship_api_2
jiasyuanchu Jun 13, 2023
4795756
Merge branch 'master' of https://github.com/av124773/twitter-api-2020
jiasyuanchu Jun 13, 2023
9c3dd27
fixed
jiasyuanchu Jun 13, 2023
c54b876
feat: add getUserFollowings, fix followship model (#21)
jiasyuanchu Jun 13, 2023
530fda6
Fix account check (#24)
av124773 Jun 13, 2023
cc02eae
Merge branch 'master' of https://github.com/av124773/twitter-api-2020
jiasyuanchu Jun 14, 2023
04f6f9e
feat: modify image updata feature
av124773 Jun 14, 2023
b6cbe17
Merge pull request #26 from av124773/fix_image_upload
jiasyuanchu Jun 14, 2023
3b0ad32
Merge branch 'master' of https://github.com/av124773/twitter-api-2020
jiasyuanchu Jun 14, 2023
33f71e6
feat: add tweet user info
av124773 Jun 14, 2023
f340785
feat: add getAllUsers, getAllTweets, deleteTweet (#25)
jiasyuanchu Jun 14, 2023
edd4326
Merge pull request #27 from av124773/tweet_add_include
jiasyuanchu Jun 14, 2023
6a5a734
feat: add likeCount and replyCount
av124773 Jun 14, 2023
6c86134
Merge pull request #28 from av124773/add_dataCount
jiasyuanchu Jun 15, 2023
ed763b0
Merge branch 'master' of https://github.com/av124773/twitter-api-2020
jiasyuanchu Jun 15, 2023
a633996
feat: add isLiked in getTweets
jiasyuanchu Jun 15, 2023
c5abbd1
fix: add limit: 10 in getTopUser (#29)
jiasyuanchu Jun 15, 2023
d5caf5f
fix: fine tune isLiked() in getTweets
jiasyuanchu Jun 15, 2023
69f22cc
feat: add getUserDataByAccount
av124773 Jun 15, 2023
9135b7d
Merge branch 'master' of https://github.com/av124773/twitter-api-2020
jiasyuanchu Jun 15, 2023
b58ec7a
Merge pull request #31 from av124773/add_getUserDataByAccount
jiasyuanchu Jun 15, 2023
87e3689
add isLiked: getTweet, getUserTweets, getUserLikes
jiasyuanchu Jun 15, 2023
0fc4aba
feat: modify API path
av124773 Jun 15, 2023
ffcf8d2
Merge pull request #32 from av124773/fix_API_router
jiasyuanchu Jun 15, 2023
fb989d6
feat: modify faker text
av124773 Jun 15, 2023
8c2e99a
Merge pull request #33 from av124773/fix_textLengthLimit
jiasyuanchu Jun 15, 2023
6456c86
fix: revise req.user.id in getUserTweets
jiasyuanchu Jun 15, 2023
a0c0409
Merge branch 'master' of https://github.com/av124773/twitter-api-2020
jiasyuanchu Jun 16, 2023
db639b6
Merge pull request #30 from av124773/R11
jiasyuanchu Jun 16, 2023
15309ae
Merge branch 'master' of https://github.com/av124773/twitter-api-2020
jiasyuanchu Jun 16, 2023
b509083
fix: fix getTweet test error
av124773 Jun 16, 2023
121670d
fix: fix getUserTweets and getUserLikes
av124773 Jun 16, 2023
838dcc5
fix: getUserFollowers (isCurrentUserFollowed) (#35)
jiasyuanchu Jun 16, 2023
d5462dd
Merge pull request #36 from av124773/fix_test_error
jiasyuanchu Jun 16, 2023
56acdac
Merge branch 'master' of https://github.com/av124773/twitter-api-2020
jiasyuanchu Jun 16, 2023
79a4471
feat: README v2
jiasyuanchu Jun 16, 2023
6009eef
feat: add dataCount at getUserTweets
av124773 Jun 16, 2023
44790ae
feat: add dataCount at getUserLikes
av124773 Jun 16, 2023
5cec3c5
fix: modify sequelize.literal()
av124773 Jun 16, 2023
f6c36a3
Merge pull request #37 from av124773/R16
jiasyuanchu Jun 16, 2023
797b46c
fix: fix getUserFollowers isFollowed error
av124773 Jun 16, 2023
0f18f1b
fix: fix getUserFollowings isFollowed error
av124773 Jun 16, 2023
e261de5
feat: add isFollowed at getUserDataByAccount
av124773 Jun 17, 2023
9225dbf
Merge pull request #39 from av124773/add_dataCount_2
jiasyuanchu Jun 17, 2023
2242a78
feat: add reply user account
av124773 Jun 17, 2023
dee10e2
Merge pull request #40 from av124773/fix_get_user_followship
jiasyuanchu Jun 17, 2023
94d7a63
Merge pull request #41 from av124773/add_userData_at_replies
jiasyuanchu Jun 17, 2023
c158490
feat: config eslint
av124773 Jun 17, 2023
aae802e
Merge pull request #42 from av124773/set_eslint
jiasyuanchu Jun 17, 2023
83e05c3
fix: fix test error
av124773 Jun 17, 2023
815e033
fix: filter out admin data in getTopUser (#43)
jiasyuanchu Jun 17, 2023
0f2dcf7
feat: add order
av124773 Jun 18, 2023
5ef45d5
feat: add role filter
av124773 Jun 18, 2023
a34a09f
Merge pull request #44 from av124773/fix_test_error_2
jiasyuanchu Jun 18, 2023
02542c9
Merge pull request #45 from av124773/fix_tweet_error
jiasyuanchu Jun 18, 2023
08e1cc9
Merge pull request #46 from av124773/filter_admin
jiasyuanchu Jun 18, 2023
266bd46
refactor: modify getTweets Promise
av124773 Jun 18, 2023
b552f19
refactor: modify getTweet Promise
av124773 Jun 18, 2023
5875b6b
refactor: modify getReplies Promise
av124773 Jun 18, 2023
d05abcd
refactor: modify postTweet Promise
av124773 Jun 18, 2023
00c5e0d
refactor: modify deleteTweet Promise
av124773 Jun 18, 2023
9a7b6f2
refactor: modify postReplies Promise
av124773 Jun 18, 2023
bbf84a3
refactor: modify postLike Promise
av124773 Jun 18, 2023
9ece3f5
refactor: modify postUnlike Promise
av124773 Jun 18, 2023
9d23c96
fix: add admin back
av124773 Jun 18, 2023
adc6562
refactor: modify addFollowing Promise
av124773 Jun 18, 2023
ddefe72
refactor: modify deleteFollowing Promise
av124773 Jun 18, 2023
2ea6491
refactor: modify getTopUser Promise
av124773 Jun 18, 2023
158263f
Merge pull request #48 from av124773/fix_test_error
jiasyuanchu Jun 18, 2023
e27ab1f
Merge pull request #47 from av124773/modify_Promise
jiasyuanchu Jun 18, 2023
504f525
feat: modify introduction
av124773 Jun 19, 2023
c2577e0
feat: modify introduction
av124773 Jun 19, 2023
4fcbf4e
feat: modify introduction
av124773 Jun 19, 2023
13178b8
Merge pull request #49 from av124773/modify_seed
jiasyuanchu Jun 19, 2023
e95719d
feat: remove useless file
av124773 Jun 20, 2023
3803c99
fix: fix getUserLikes (#52)
jiasyuanchu Jun 21, 2023
6ed5c99
Merge pull request #50 from av124773/remove_uselessFile
jiasyuanchu Jun 21, 2023
494f9f1
fix: fix getAllUsers likeCount calculation (#53)
jiasyuanchu Jun 21, 2023
0db9526
feat: check code style and remove useless code
av124773 Jun 21, 2023
62d7380
Merge pull request #54 from av124773/tidy_code
jiasyuanchu Jun 22, 2023
16d8d03
fine tune README.md (#55)
jiasyuanchu Jun 22, 2023
c78e2df
export "JWT_SECRET" for Travis CI
jiasyuanchu Jun 23, 2023
f1dbc43
Merge pull request #56 from av124773/R21
jiasyuanchu Jun 23, 2023
3f1a4fe
fix 版本差異大小寫問題 (#57)
jiasyuanchu Jun 23, 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=
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/node_modules/*
/test/*
/models/index.js
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
node_modules/
temp/
upload/

.DS_Store
*.DS_Store

Expand Down
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ before_install:

# 在執行測試之前的指令
before_script:
- echo "JWT_SECRET=alphacamp" >> .env
- sequelize db:migrate
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
101 changes: 101 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Simple Twitter API 2023

## v1.0.0

## About The Project
![Alt text](image-1.png)

A RESTful API server for Simple Twitter project built with Node.js, Express framework, and MySQL.

## Usage

Through this web program, after the user registers an account, users can use post, reply to other user's tweets, like other users' tweets, track other users, and use the account settings functions like uploading profile picture, uploading cover page photo, etc.

_Check this out to view more - [documentation](https://magic9701.github.io/Simple-Twitter/)_

### Base URL
```sh
https://pure-falls-11392.herokuapp.com/
```

### Installation

1. Clone the repo
```sh
git clone https://github.com/av124773/twitter-api-2020.git
```
2. Install NPM packages
```sh
npm install
```
3. Create database via SQL WorkBench(enter in WorkBench Application)
```sh
create database ac_twitter_workspace
```
4. Create models
```sh
npx sequelize db:migrate
```
5. Create built-in data via seeds
```sh
npx sequelize db:seed:all
```
6. Establish .env file and put passowrds in
```sh
IMGUR_CLIENT_ID= 'YOUR PASSWORD'
JWT_SECRET= 'YOUR PASSWORD'
```
7. Start the server
```sh
- MacOS
npm run start
- Windows OS
npm run startWin
```

## Getting Started
Use the default accounts to login
```sh
- User
account: user1
email: user1@example.com
password: 12345678
- Admin
account: root
email: root@example.com
password: 12345678
```

## Environment
- node: ^v14.16.0,
- nodemon

## Built With
- 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.1.4,
- 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.4,
- mysql2: ^1.6.4,
- passport: ^0.4.1,
- passport-jwt: ^4.0.0,
- passport-local: ^1.0.0,
- sequelize: ^6.18.0,
- sequelize-cli: ^5.5.0,
- sinon: ^10.0.0,
- sinon-chai: ^3.3.0,
- tslib: ^2.5.3

## Contributors
- Kevin_L [https://github.com/av124773](https://github.com/av124773)
- Chia-Hsuan Chu [https://github.com/jiasyuanchu](https://github.com/jiasyuanchu)
11 changes: 8 additions & 3 deletions _helpers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@

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

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

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

const express = require('express')
const helpers = require('./_helpers');
const bodyParser = require('body-parser')
const cors = require('cors')
const routes = require('./routes')

const methodOverride = require('method-override')

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

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

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

// use helpers.getUser(req) to replace req.user
function authenticated(req, res, next){
// passport.authenticate('jwt', { ses...
};
app.use(express.urlencoded({ extended: true }))

app.get('/', (req, res) => res.send('Hello World!'))
app.use(routes)
app.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
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 JWTStrategy = passportJWT.Strategy
const ExtractJWT = passportJWT.ExtractJwt
const bcrypt = require('bcryptjs')

const { User } = require('../models')

// set up local Passport strategy
passport.use(new LocalStrategy({
usernameField: 'account',
passwordField: 'password'
}, async (account, password, done) => {
try {
const user = await User.findOne({
where: { account } // user checking by account
})
if (!user) {
return done(null, false, { status: 401, message: '帳號不存在' })
}
const isMatch = await bcrypt.compare(password, user.password)
if (!isMatch) {
return done(null, false, { status: 401, message: '密碼錯誤' })
}
return done(null, user.get())
} catch (err) {
return done(err, false)
}
}))

module.exports = passport
// middleware for jwt
const jwtOptions = {
jwtFromRequest: ExtractJWT.fromAuthHeaderAsBearerToken(),
secretOrKey: process.env.JWT_SECRET
}

// jwtStrategy
passport.use(new JWTStrategy(jwtOptions, (jwtPayload, cb) => {
User.findByPk(jwtPayload.id, { // 拿payload裡面的id
include: [
{ model: User, as: 'Followers' },
{ model: User, as: 'Followings' }
]
})
.then(user => {
if (!user) return cb(null, false)
return cb(null, user)
})
.catch(err => cb(err))
})
)

// for local: serialize and deserialize user
passport.serializeUser((user, cb) => {
cb(null, user.id)
})
passport.deserializeUser((id, cb) => {
return User.findByPk(id, { // 使以下可透過req.user查詢
include: [
{ model: User, as: 'Followers' },
{ model: User, as: 'Followings' }
]
})
.then(user => cb(null, user.toJSON()))
.catch(err => cb(err))
})

module.exports = passport
Loading