# `PUT` /api/users/:id ## API 功能 更新自己的使用者資料。 * 驗證身份: * 需登入,且role = user * 只能修改自己的資料,動態路由傳入之id值與req.user.id需相符 * 資料處理: * 若輸入字串前後有空白鍵,則空白鍵會被移除;若只有輸入空白鍵,處理後變成無資料 * 若無輸入值或無上傳圖片,視為不修改,自動帶入使用者原本的資料 * 上傳圖片(avatar, cover)會送到第三方儲存空間(imgur),並收到圖片連結網址,資料庫存入網址 * 資料驗證: * email, accout不可與資料庫內已註冊之email/account重複 * name長度不可超過50字,introduction不可超過160字 * password與checkPassword必須相符 * 成功更新後,將使用者的資料回傳(密碼除外) ## 前端傳入資料 ### parameters | params | Description | required | | ------ | ----------- | -------- | | `id` | user id | true | ### Request Content-Type: multipart/form-data All values are optional. params | Description | --- | ---| `id` | user id `name` | user name `email` | user email `password` | user password `checkPassword` | user password again `introduction` | user introduction `avatar` | user avatar (string: image url) `cover` | user cover (string: image url) ## 後端回傳資料 ### 更新成功 ```json // status code: 200 { "id": 2, "account": "user1", "email": "user1@example.com", "name": "user1", "avatar": "https://avatar-url", "cover": "https://cover-url", "introduction": "helloworld", "role": "user", "createdAt": "2022-08-03T11:10:34.000Z", "updatedAt": "2022-08-04T03:34:37.263Z" } ``` ### 失敗 發生原因:未收到動態路由傳入之 id ```json // status code: 401 { "status": "error", "message": "Id number is not found in url request" } ``` 發生原因:動態路由傳入 id 與目前使用者id不符 ```json // status code: 400 { "status": "error", "message": "Can not edit other's profile" } ``` 發生原因:輸入的email已註冊過 ```json // status code: 401 { "status": "error", "message": "The email is registered." } ``` 發生原因:輸入的account已註冊過 ```json // status code: 401 { "status": "error", "message": "The account is registered." } ``` 發生原因:輸入的 name 超過50字 ```json // status code: 400 { "status": "error", "message": "Name is too long." } ``` 發生原因:輸入的 introduction 超過160字 ```json // status code: 400 { "status": "error", "message": "Introduction is too long." } ``` 發生原因:輸入之 password 與 checkPassword 不相符 ```json // status code: 401 { "status": "error", "message": "Password and checkPassword are not same." } ``` ## 相關連結 * [回首頁](https://hackmd.io/@twitter-2022/index) * [API 總表](/Gl56cI2LQ5ObBpmQnbnphw) ###### tags: `API-doc`