api-authentication

Usage & Build

  • mongoDB
docker-composer up
  • node.js Server
# install the packages first
yarn install
# or npm install

# tsc is executed first(If didn't execute, an error will be displayed but it does not matter)
yarn tsc

yarn dev
# or npm run dev

Get access_token

  • Google

    • Google OAuth2 API v2
      • pick https://www.googleapis.com/auth/userinfo.email
  • Facebook

    • pick email

Request / Response

Request

POST /thing/

curl -i -H 'Accept: application/json' -d 'name=Foo&status=new' http://localhost:7000/thing

Response

HTTP/1.1 201 Created
Date: Thu, 24 Feb 2011 12:36:30 GMT
Status: 201 Created
Connection: close
Content-Type: application/json
Location: /thing/1
Content-Length: 36

{"id":1,"name":"Foo","status":"new"}

Get a specific Thing

Request

GET /thing/id

curl -i -H 'Accept: application/json' http://localhost:7000/thing/1

Response

HTTP/1.1 200 OK
Date: Thu, 24 Feb 2011 12:36:30 GMT
Status: 200 OK
Connection: close
Content-Type: application/json
Content-Length: 36

{"id":1,"name":"Foo","status":"new"}

Get a non-existent Thing

Email registration

Request

POST /auth/signup HTTP/1.1
Host: localhost:3000
Content-Type: application/json

{
"name":"keyo",
"email": "814007@gmail.com",
"password": "12345678",
"repassword": "12345678"
}

Response

{
    "token": "A JWT TOKEN"
}

error

{
    "error": [
        {
            "msg": "使用者名稱不得為空",
            "param": "name",
            "location": "body"
        },
        {
            "msg": "Email 不得為空",
            "param": "email",
            "location": "body"
        },
        {
            "msg": "Email 格式錯誤",
            "param": "email",
            "location": "body"
        },
        {
            "msg": "密碼不得為空",
            "param": "password",
            "location": "body"
        },
        {
            "msg": "密碼少於8個字",
            "param": "password",
            "location": "body"
        },
        {
            "msg": "請再次輸入密碼確認",
            "param": "repassword",
            "location": "body"
        }
    ]
}

Google registration

Request

POST /auth/google HTTP/1.1
Host: localhost:3000
Content-Type: application/json

{
    "access_token": "access_token from google"
}

Response

{
    "token": "A JWT TOKEN"
}

error

{
    "error": "InternalOAuthError"
}

Facebook registration

Request

POST /auth/facebook HTTP/1.1
Host: localhost:3000
Content-Type: application/json

{
    "access_token": "access_token from facebook"
}

Response

{
    "token": "A JWT TOKEN"
}

error

{
    "error": "InternalOAuthError"
}

Directory Structure

/src
  controllers/
  middleware/
  models/
  requests/
  routes/
  services/
  typings/ 
  passport.ts
  app.ts
  router.ts

fork

Acknowledgements

Solution