api_auth_otp.txt 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. # EsimLao Authentication API Documentation
  2. ## Overview
  3. API xác thực người dùng qua email với OTP (One-Time Password).
  4. URL_UAT : http://149.28.132.56:8360/
  5. ---
  6. ## 1. Request OTP
  7. Gửi mã OTP đến email người dùng để xác thực đăng nhập.
  8. ### Endpoint
  9. ```
  10. POST /apis/auth/request-otp
  11. ```
  12. ### Request Headers
  13. | Header | Value | Required |
  14. |--------|-------|----------|
  15. | Content-Type | application/json | Yes |
  16. ### Request Body
  17. ```json
  18. {
  19. "email": "user@example.com",
  20. "lang": "lo" // Default: "lo" / en
  21. }
  22. ```
  23. ### Parameters
  24. | Field | Type | Required | Default | Description |
  25. |-------|------|----------|---------|-------------|
  26. | email | string | Yes | - | Email address của người dùng |
  27. | lang | string | No | "vi" | Ngôn ngữ email: `vi` (Tiếng Việt), `en` (English), `lo` (ລາວ) |
  28. ### Response Success (200)
  29. ```json
  30. {
  31. "code": 0,
  32. "message": "<Config: OTP_SENT_SUCCESS>",
  33. "data": {
  34. "email": "user@example.com",
  35. "expireInSeconds": 300
  36. }
  37. }
  38. ```
  39. ### Response Error (200)
  40. ```json
  41. // Email không được cung cấp
  42. {
  43. "code": -801,
  44. "message": "<Config: EMAIL_REQUIRED>",
  45. "data": {}
  46. }
  47. // Lỗi hệ thống
  48. {
  49. "code": -6,
  50. "message": "<Config: SYSTEM_FAILURE>",
  51. "data": {}
  52. }
  53. ```
  54. ### Response Fields
  55. | Field | Type | Description |
  56. |-------|------|-------------|
  57. | code | int | 0 = Success, khác 0 = Error (xem Error Codes) |
  58. | message | string | Thông báo từ CONFIG table (theo ngôn ngữ) |
  59. | data.email | string | Email đã gửi OTP |
  60. | data.expireInSeconds | int | Thời gian OTP hết hạn (giây) |
  61. ### Notes
  62. - OTP gồm 6 chữ số
  63. - OTP có hiệu lực trong 5 phút
  64. - Mỗi lần request mới sẽ hủy các OTP cũ chưa sử dụng
  65. - Nếu email chưa tồn tại, hệ thống tự động tạo tài khoản mới
  66. ---
  67. ## 2. Verify OTP
  68. Xác thực mã OTP và hoàn tất đăng nhập.
  69. ### Endpoint
  70. ```
  71. POST /apis/auth/verify-otp
  72. ```
  73. ### Request Headers
  74. | Header | Value | Required |
  75. |--------|-------|----------|
  76. | Content-Type | application/json | Yes |
  77. ### Request Body
  78. ```json
  79. {
  80. "email": "user@example.com",
  81. "otpCode": "123456",
  82. "lang": "lo" // Default: "lo" / en
  83. }
  84. ```
  85. ### Parameters
  86. | Field | Type | Required | Default | Description |
  87. |-------|------|----------|---------|-------------|
  88. | email | string | Yes | - | Email đã nhận OTP |
  89. | otpCode | string | Yes | - | Mã OTP 6 số |
  90. | lang | string | No | "lo" | Ngôn ngữ thông báo: `lo` (ລາວ), `en` (English) |
  91. ### Response Success (200)
  92. ```json
  93. {
  94. "code": 0,
  95. "message": "<Config: LOGIN_SUCCESS>",
  96. "data": {
  97. "userId": 12345,
  98. "email": "user@example.com",
  99. "fullName": "Nguyen Van A",
  100. "avatarUrl": "https://example.com/avatar.jpg",
  101. "accessToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  102. "refreshToken": "dGhpcyBpcyBhIHJlZnJlc2ggdG9rZW4...",
  103. "expiresAt": "2024-12-30T10:00:00Z"
  104. }
  105. }
  106. ```
  107. ### Response Error Cases
  108. ```json
  109. // Thiếu email hoặc OTP
  110. {
  111. "code": -801,
  112. "message": "<Config: EMAIL_OTP_REQUIRED>",
  113. "data": {}
  114. }
  115. // OTP không hợp lệ
  116. {
  117. "code": -201,
  118. "message": "<Config: OTP_INVALID>",
  119. "data": {}
  120. }
  121. // OTP đã được sử dụng
  122. {
  123. "code": -203,
  124. "message": "<Config: OTP_ALREADY_USED>",
  125. "data": {}
  126. }
  127. // OTP đã hết hạn
  128. {
  129. "code": -202,
  130. "message": "<Config: OTP_EXPIRED>",
  131. "data": {}
  132. }
  133. // Không tìm thấy người dùng
  134. {
  135. "code": -300,
  136. "message": "<Config: USER_NOT_FOUND>",
  137. "data": {}
  138. }
  139. // Lỗi hệ thống
  140. {
  141. "code": -6,
  142. "message": "<Config: SYSTEM_FAILURE>",
  143. "data": {}
  144. }
  145. ```
  146. ### Response Fields
  147. | Field | Type | Description |
  148. |-------|------|-------------|
  149. | code | int | 0 = Success, khác 0 = Error (xem Error Codes) |
  150. | message | string | Thông báo từ CONFIG table (theo ngôn ngữ) |
  151. | data.userId | int | ID người dùng |
  152. | data.email | string | Email người dùng |
  153. | data.fullName | string | Họ tên đầy đủ |
  154. | data.avatarUrl | string | URL ảnh đại diện (nullable) |
  155. | data.accessToken | string | JWT access token |
  156. | data.refreshToken | string | Refresh token để làm mới access token |
  157. | data.expiresAt | datetime | Thời điểm access token hết hạn |
  158. ### Notes
  159. - Access token có hiệu lực 24 giờ
  160. - Refresh token có hiệu lực 30 ngày
  161. - Mỗi lần đăng nhập thành công, các token cũ sẽ bị thu hồi
  162. ---
  163. ## Error Codes
  164. ### Success
  165. | Code | Constant | Description |
  166. |------|----------|-------------|
  167. | 0 | Success | Thành công (mọi request thành công đều trả về 0) |
  168. ### General Errors (-1 to -99)
  169. | Code | Constant | Description |
  170. |------|----------|-------------|
  171. | -1 | Error | Lỗi chung |
  172. | -6 | SystemError | Lỗi hệ thống |
  173. ### OTP Errors (-200 to -299)
  174. | Code | Constant | Description |
  175. |------|----------|-------------|
  176. | -200 | OtpRequired | Yêu cầu OTP |
  177. | -201 | OtpInvalid | OTP không hợp lệ |
  178. | -202 | OtpExpired | OTP đã hết hạn |
  179. | -203 | OtpAlreadyUsed | OTP đã được sử dụng |
  180. | -204 | OtpMaxAttemptsExceeded | Vượt quá số lần thử |
  181. | -205 | OtpSendFailed | Gửi OTP thất bại |
  182. | -206 | OtpTooManyRequests | Request quá nhiều |
  183. ### User Errors (-300 to -399)
  184. | Code | Constant | Description |
  185. |------|----------|-------------|
  186. | -300 | UserNotFound | Không tìm thấy người dùng |
  187. | -304 | InvalidEmail | Email không hợp lệ |
  188. ### Validation Errors (-800 to -899)
  189. | Code | Constant | Description |
  190. |------|----------|-------------|
  191. | -801 | RequiredFieldMissing | Thiếu trường bắt buộc |
  192. ---
  193. ## Authentication
  194. Sau khi đăng nhập thành công, sử dụng `accessToken` trong header cho các API yêu cầu xác thực:
  195. ```
  196. Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
  197. ```
  198. ---
  199. ## Flow Diagram
  200. ```
  201. ┌─────────────┐ ┌─────────────┐ ┌─────────────┐
  202. │ Client │ │ API │ │ Email │
  203. └──────┬──────┘ └──────┬──────┘ └──────┬──────┘
  204. │ │ │
  205. │ POST /request-otp │ │
  206. │──────────────────>│ │
  207. │ │ │
  208. │ │ Generate OTP │
  209. │ │ Save to DB │
  210. │ │ Queue Email │
  211. │ │ │
  212. │ Response │ │
  213. │<──────────────────│ │
  214. │ │ │
  215. │ │ Send OTP Email │
  216. │ │──────────────────>│
  217. │ │ │
  218. │ │ │ OTP Email
  219. │<──────────────────────────────────────│
  220. │ │ │
  221. │ POST /verify-otp │ │
  222. │──────────────────>│ │
  223. │ │ │
  224. │ │ Verify OTP │
  225. │ │ Generate JWT │
  226. │ │ │
  227. │ Token Response │ │
  228. │<──────────────────│ │
  229. │ │ │
  230. ```
  231. ---
  232. ## Example Usage (cURL)
  233. ### Request OTP
  234. ```bash
  235. curl -X POST https://api.esimlao.com/apis/auth/request-otp \
  236. -H "Content-Type: application/json" \
  237. -d '{
  238. "email": "user@example.com",
  239. "lang": "vi"
  240. }'
  241. ```
  242. ### Verify OTP
  243. ```bash
  244. curl -X POST https://api.esimlao.com/apis/auth/verify-otp \
  245. -H "Content-Type: application/json" \
  246. -d '{
  247. "email": "user@example.com",
  248. "otpCode": "123456"
  249. }'
  250. ```
  251. ### Use Token
  252. ```bash
  253. curl -X GET https://api.esimlao.com/apis/user/profile \
  254. -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
  255. ```