GithubHelp home page GithubHelp logo

express-demo's Introduction

Express User CRUD

  • 此專案為利用 express 與 node js 以 RESTful API 實現會員系統的 CRUD 操作。

  • 會員資料處存在 mysql 。

  • refresh token 儲存在 redis ,利用 TTL 自動刪除 token。

  • 每一支 API 的 Response 都會包固定格式的 JSON 中,當成功 success 會是 true ,反之亦然, data 欄位則是 nullable。

  • API Success JSON:

{
  "success": true,
  "data": {
    "accessToken": "new token",
    "refreshToken": "new token"
  }
}
  • 當 API 失敗會回傳一組自定義的 error object 在 response body 之中,包含 codemessage
  • API Fail JSON:
{
  "success": false,
  "data": null,
  "error": {
    "code": "999",
    "message": "/api/v2/user/apiapi not found"
  }
}

Feature

1. 會員登入

  • Request:
POST /api/v2/user/signIn
Accept: application/json

{
    "email":"[email protected]",
    "password": "password"
}
  • Response:
HTTP/1.1 200 OK
Content-Type: application/json

{
    "success": true,
    "data": {
        "id": 185,
        "uid": "20231025100185",
        "userName": "u21",
        "email": "[email protected]",
        "accessToken": "new access token",
        "refreshToken": "new refresh token",
        "isEmailVerified": false
    }
}

2. 會員註冊

  • Request:
POST /api/v2/user/signUp
Accept: application/json

{
    "userName": "u40",
    "email": "[email protected]",
    "password": "123"
}
  • Response:
HTTP/1.1 200 OK
Content-Type: application/json

{
    "success": true,
    "data": {
        "id": 192,
        "uid": "20231116100192",
        "userName": "u40",
        "email": "[email protected]",
        "accessToken": "new token",
        "isEmailVerified": false
    }
}

3. 會員資料

  • Request:
GET /api/v2/user/profile
Host: example.com
Accept: application/json
Authorization: Bearer your_access_token
  • Response:
HTTP/1.1 200 OK
Content-Type: application/json

{
    "success": true,
    "data": {
        "id": 192,
        "uid": "20231116100192",
        "userName": "u40",
        "email": "[email protected]",
        "accessToken": "new token",
        "isEmailVerified": false
    }
}

4. 修改會員資料

5. 更新 Access Token

  • Request:
POST /api/v2/user/token
Accept: application/json
Authorization: Bearer your_access_token

{
    "refreshToken":"{{refresh_token}}"
}
  • Response:
HTTP/1.1 200 OK
Content-Type: application/json

{
    "success": true,
    "data": {
        "accessToken": "new access token",
        "refreshToken": "new refresh token"
    }
}

6. 發送會員驗證信

由於此專案沒有串接 SMTP 服務,模擬發送信件功能是利用 Ethereal,Resposne 會包含一組 Ethereal 的連結,代表模擬的一封信,信件內文就是驗證 email 的連結。

  • Request:
POST /api/v2/user/sendVerifyEmail
Accept: application/json
Authorization: Bearer your_access_token
  • Response:
HTTP/1.1 200 OK
Content-Type: application/json

{
    "success": true,
    "data": {
        "etherealLink": "https://ethereal.email/message/newLink"
    }
}
  • 若是已驗證過的會員,Response 則會失敗
HTTP/1.1 200 OK
Content-Type: application/json

{
    "success": true,
    "data": null,
    "error": {
        "code": "ERROR_MAIL_01",
        "message": "The email has previously been successfully validated."
    }
}

7. 驗證會員驗證信

  • Request:
GET /api/v2/user/verifyEmailToken?token=token_from_email_link
Accept: application/json
  • Response:
HTTP/1.1 200 OK
Content-Type: application/json

{
    "success": true,
    "data": null
}

8. 發送重設密碼信

9. 驗證重設密碼信

10. 更改密碼

相依套件

  1. bcrypt
  2. cookie
  3. cookie-parser
  4. cors
  5. csrf-csrf"
  6. dotenv
  7. ejs
  8. express
  9. express-jwt
  10. express-session
  11. express-validator
  12. jsonwebtoken
  13. lodash-es
  14. mysql2
  15. nodemailer
  16. redis
  17. sequelize
  18. uuid

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.