1
0

Clients.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <style scoped>
  2. .action-link {
  3. cursor: pointer;
  4. }
  5. </style>
  6. <template>
  7. <div>
  8. <div class="card card-default mb-4">
  9. <div class="card-header font-weight-bold bg-white">
  10. <div style="display: flex; justify-content: space-between; align-items: center;">
  11. <span>
  12. OAuth Clients
  13. </span>
  14. <a class="action-link" tabindex="-1" @click="showCreateClientForm">
  15. Create New Client
  16. </a>
  17. </div>
  18. </div>
  19. <div class="card-body">
  20. <!-- Current Clients -->
  21. <p class="mb-0 font-weight-bold text-center lead p-5" v-if="clients.length === 0">
  22. You have not created any OAuth clients.
  23. </p>
  24. <table class="table table-borderless mb-0" v-if="clients.length > 0">
  25. <thead>
  26. <tr>
  27. <th>Client ID</th>
  28. <th>Name</th>
  29. <th>Secret</th>
  30. <th></th>
  31. <th></th>
  32. </tr>
  33. </thead>
  34. <tbody>
  35. <tr v-for="client in clients">
  36. <!-- ID -->
  37. <td style="vertical-align: middle;">
  38. {{ client.id }}
  39. </td>
  40. <!-- Name -->
  41. <td style="vertical-align: middle;">
  42. {{ client.name }}
  43. </td>
  44. <!-- Secret -->
  45. <td style="vertical-align: middle;">
  46. <code>{{ client.secret }}</code>
  47. </td>
  48. <!-- Edit Button -->
  49. <td style="vertical-align: middle;">
  50. <a class="btn btn-outline-secondary btn-sm py-1" tabindex="-1" @click="edit(client)">
  51. Edit
  52. </a>
  53. </td>
  54. <!-- Delete Button -->
  55. <td style="vertical-align: middle;">
  56. <a class="btn btn-outline-danger btn-sm py-1" @click="destroy(client)" href="">
  57. Delete
  58. </a>
  59. </td>
  60. </tr>
  61. </tbody>
  62. </table>
  63. </div>
  64. </div>
  65. <!-- Create Client Modal -->
  66. <div class="modal fade" id="modal-create-client" tabindex="-1" role="dialog">
  67. <div class="modal-dialog">
  68. <div class="modal-content">
  69. <div class="modal-header">
  70. <h4 class="modal-title">
  71. Create Client
  72. </h4>
  73. <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
  74. </div>
  75. <div class="modal-body">
  76. <!-- Form Errors -->
  77. <div class="alert alert-danger" v-if="createForm.errors.length > 0">
  78. <p class="mb-0"><strong>Whoops!</strong> Something went wrong!</p>
  79. <br>
  80. <ul>
  81. <li v-for="error in createForm.errors">
  82. {{ error }}
  83. </li>
  84. </ul>
  85. </div>
  86. <!-- Create Client Form -->
  87. <form role="form">
  88. <!-- Name -->
  89. <div class="form-group row">
  90. <label class="col-md-3 col-form-label">Name</label>
  91. <div class="col-md-9">
  92. <input id="create-client-name" type="text" class="form-control" autocomplete="off"
  93. @keyup.enter="store" v-model="createForm.name">
  94. <span class="form-text text-muted">
  95. Something your users will recognize and trust.
  96. </span>
  97. </div>
  98. </div>
  99. <div class="form-group row">
  100. <label class="col-md-3 col-form-label">Description</label>
  101. <div class="col-md-9">
  102. <textarea class="form-control" rows="3"></textarea>
  103. <span class="form-text text-muted">
  104. A brief description of your app
  105. </span>
  106. </div>
  107. </div>
  108. <div class="form-group row">
  109. <label class="col-md-3 col-form-label">Website</label>
  110. <div class="col-md-9">
  111. <input type="text" class="form-control" autocomplete="off">
  112. <span class="form-text text-muted">
  113. Your website url
  114. </span>
  115. </div>
  116. </div>
  117. <!-- Redirect URL -->
  118. <div class="form-group row">
  119. <label class="col-md-3 col-form-label">Redirect URL</label>
  120. <div class="col-md-9">
  121. <input type="text" class="form-control" name="redirect"
  122. @keyup.enter="store" v-model="createForm.redirect">
  123. <span class="form-text text-muted">
  124. Your application's authorization callback URL.
  125. </span>
  126. </div>
  127. </div>
  128. <div class="form-group row">
  129. <label class="col-md-3 col-form-label">Scopes</label>
  130. <div class="col-md-9">
  131. <div class="custom-control custom-switch">
  132. <input type="checkbox" class="custom-control-input" id="customSwitch1">
  133. <label class="custom-control-label" for="customSwitch1">Read</label>
  134. </div>
  135. <div class="custom-control custom-switch">
  136. <input type="checkbox" class="custom-control-input" id="customSwitch2">
  137. <label class="custom-control-label" for="customSwitch2">Write</label>
  138. </div>
  139. <span class="form-text text-muted">
  140. Your application's scopes.
  141. </span>
  142. </div>
  143. </div>
  144. </form>
  145. </div>
  146. <!-- Modal Actions -->
  147. <div class="modal-footer">
  148. <button type="button" class="btn btn-secondary font-weight-bold" data-dismiss="modal">Close</button>
  149. <button type="button" class="btn btn-primary font-weight-bold" @click="store">
  150. Create
  151. </button>
  152. </div>
  153. </div>
  154. </div>
  155. </div>
  156. <!-- Edit Client Modal -->
  157. <div class="modal fade" id="modal-edit-client" tabindex="-1" role="dialog">
  158. <div class="modal-dialog">
  159. <div class="modal-content">
  160. <div class="modal-header">
  161. <h4 class="modal-title">
  162. Edit Client
  163. </h4>
  164. <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
  165. </div>
  166. <div class="modal-body">
  167. <!-- Form Errors -->
  168. <div class="alert alert-danger" v-if="editForm.errors.length > 0">
  169. <p class="mb-0"><strong>Whoops!</strong> Something went wrong!</p>
  170. <br>
  171. <ul>
  172. <li v-for="error in editForm.errors">
  173. {{ error }}
  174. </li>
  175. </ul>
  176. </div>
  177. <!-- Edit Client Form -->
  178. <form role="form">
  179. <!-- Name -->
  180. <div class="form-group row">
  181. <label class="col-md-3 col-form-label">Name</label>
  182. <div class="col-md-9">
  183. <input id="edit-client-name" type="text" class="form-control"
  184. @keyup.enter="update" v-model="editForm.name">
  185. <span class="form-text text-muted">
  186. Something your users will recognize and trust.
  187. </span>
  188. </div>
  189. </div>
  190. <!-- Redirect URL -->
  191. <div class="form-group row">
  192. <label class="col-md-3 col-form-label">Redirect URL</label>
  193. <div class="col-md-9">
  194. <input type="text" class="form-control" name="redirect"
  195. @keyup.enter="update" v-model="editForm.redirect">
  196. <span class="form-text text-muted">
  197. Your application's authorization callback URL.
  198. </span>
  199. </div>
  200. </div>
  201. </form>
  202. </div>
  203. <!-- Modal Actions -->
  204. <div class="modal-footer">
  205. <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
  206. <button type="button" class="btn btn-primary" @click="update">
  207. Save Changes
  208. </button>
  209. </div>
  210. </div>
  211. </div>
  212. </div>
  213. </div>
  214. </template>
  215. <script>
  216. export default {
  217. /*
  218. * The component's data.
  219. */
  220. data() {
  221. return {
  222. clients: [],
  223. createForm: {
  224. errors: [],
  225. name: '',
  226. redirect: ''
  227. },
  228. editForm: {
  229. errors: [],
  230. name: '',
  231. redirect: ''
  232. }
  233. };
  234. },
  235. /**
  236. * Prepare the component (Vue 1.x).
  237. */
  238. ready() {
  239. this.prepareComponent();
  240. },
  241. /**
  242. * Prepare the component (Vue 2.x).
  243. */
  244. mounted() {
  245. this.prepareComponent();
  246. },
  247. methods: {
  248. /**
  249. * Prepare the component.
  250. */
  251. prepareComponent() {
  252. this.getClients();
  253. $('#modal-create-client').on('shown.bs.modal', () => {
  254. $('#create-client-name').focus();
  255. });
  256. $('#modal-edit-client').on('shown.bs.modal', () => {
  257. $('#edit-client-name').focus();
  258. });
  259. },
  260. /**
  261. * Get all of the OAuth clients for the user.
  262. */
  263. getClients() {
  264. axios.get('/oauth/clients')
  265. .then(response => {
  266. this.clients = response.data;
  267. });
  268. },
  269. /**
  270. * Show the form for creating new clients.
  271. */
  272. showCreateClientForm() {
  273. $('#modal-create-client').modal('show');
  274. },
  275. /**
  276. * Create a new OAuth client for the user.
  277. */
  278. store() {
  279. this.persistClient(
  280. 'post', '/oauth/clients',
  281. this.createForm, '#modal-create-client'
  282. );
  283. },
  284. /**
  285. * Edit the given client.
  286. */
  287. edit(client) {
  288. this.editForm.id = client.id;
  289. this.editForm.name = client.name;
  290. this.editForm.redirect = client.redirect;
  291. $('#modal-edit-client').modal('show');
  292. },
  293. /**
  294. * Update the client being edited.
  295. */
  296. update() {
  297. this.persistClient(
  298. 'put', '/oauth/clients/' + this.editForm.id,
  299. this.editForm, '#modal-edit-client'
  300. );
  301. },
  302. /**
  303. * Persist the client to storage using the given form.
  304. */
  305. persistClient(method, uri, form, modal) {
  306. form.errors = [];
  307. axios[method](uri, form)
  308. .then(response => {
  309. this.getClients();
  310. form.name = '';
  311. form.redirect = '';
  312. form.errors = [];
  313. $(modal).modal('hide');
  314. })
  315. .catch(error => {
  316. if (typeof error.response.data === 'object') {
  317. form.errors = _.flatten(_.toArray(error.response.data.errors));
  318. } else {
  319. form.errors = ['Something went wrong. Please try again.'];
  320. }
  321. });
  322. },
  323. /**
  324. * Destroy the given client.
  325. */
  326. destroy(client) {
  327. axios.delete('/oauth/clients/' + client.id)
  328. .then(response => {
  329. this.getClients();
  330. });
  331. }
  332. }
  333. }
  334. </script>