Clients.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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" 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="action-link" tabindex="-1" @click="edit(client)">
  51. Edit
  52. </a>
  53. </td>
  54. <!-- Delete Button -->
  55. <td style="vertical-align: middle;">
  56. <a class="action-link text-danger" @click="destroy(client)">
  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. <!-- Redirect URL -->
  100. <div class="form-group row">
  101. <label class="col-md-3 col-form-label">Redirect URL</label>
  102. <div class="col-md-9">
  103. <input type="text" class="form-control" name="redirect"
  104. @keyup.enter="store" v-model="createForm.redirect">
  105. <span class="form-text text-muted">
  106. Your application's authorization callback URL.
  107. </span>
  108. </div>
  109. </div>
  110. </form>
  111. </div>
  112. <!-- Modal Actions -->
  113. <div class="modal-footer">
  114. <button type="button" class="btn btn-secondary font-weight-bold" data-dismiss="modal">Close</button>
  115. <button type="button" class="btn btn-primary font-weight-bold" @click="store">
  116. Create
  117. </button>
  118. </div>
  119. </div>
  120. </div>
  121. </div>
  122. <!-- Edit Client Modal -->
  123. <div class="modal fade" id="modal-edit-client" tabindex="-1" role="dialog">
  124. <div class="modal-dialog">
  125. <div class="modal-content">
  126. <div class="modal-header">
  127. <h4 class="modal-title">
  128. Edit Client
  129. </h4>
  130. <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
  131. </div>
  132. <div class="modal-body">
  133. <!-- Form Errors -->
  134. <div class="alert alert-danger" v-if="editForm.errors.length > 0">
  135. <p class="mb-0"><strong>Whoops!</strong> Something went wrong!</p>
  136. <br>
  137. <ul>
  138. <li v-for="error in editForm.errors">
  139. {{ error }}
  140. </li>
  141. </ul>
  142. </div>
  143. <!-- Edit Client Form -->
  144. <form role="form">
  145. <!-- Name -->
  146. <div class="form-group row">
  147. <label class="col-md-3 col-form-label">Name</label>
  148. <div class="col-md-9">
  149. <input id="edit-client-name" type="text" class="form-control"
  150. @keyup.enter="update" v-model="editForm.name">
  151. <span class="form-text text-muted">
  152. Something your users will recognize and trust.
  153. </span>
  154. </div>
  155. </div>
  156. <!-- Redirect URL -->
  157. <div class="form-group row">
  158. <label class="col-md-3 col-form-label">Redirect URL</label>
  159. <div class="col-md-9">
  160. <input type="text" class="form-control" name="redirect"
  161. @keyup.enter="update" v-model="editForm.redirect">
  162. <span class="form-text text-muted">
  163. Your application's authorization callback URL.
  164. </span>
  165. </div>
  166. </div>
  167. </form>
  168. </div>
  169. <!-- Modal Actions -->
  170. <div class="modal-footer">
  171. <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
  172. <button type="button" class="btn btn-primary" @click="update">
  173. Save Changes
  174. </button>
  175. </div>
  176. </div>
  177. </div>
  178. </div>
  179. </div>
  180. </template>
  181. <script>
  182. export default {
  183. /*
  184. * The component's data.
  185. */
  186. data() {
  187. return {
  188. clients: [],
  189. createForm: {
  190. errors: [],
  191. name: '',
  192. redirect: ''
  193. },
  194. editForm: {
  195. errors: [],
  196. name: '',
  197. redirect: ''
  198. }
  199. };
  200. },
  201. /**
  202. * Prepare the component (Vue 1.x).
  203. */
  204. ready() {
  205. this.prepareComponent();
  206. },
  207. /**
  208. * Prepare the component (Vue 2.x).
  209. */
  210. mounted() {
  211. this.prepareComponent();
  212. },
  213. methods: {
  214. /**
  215. * Prepare the component.
  216. */
  217. prepareComponent() {
  218. this.getClients();
  219. $('#modal-create-client').on('shown.bs.modal', () => {
  220. $('#create-client-name').focus();
  221. });
  222. $('#modal-edit-client').on('shown.bs.modal', () => {
  223. $('#edit-client-name').focus();
  224. });
  225. },
  226. /**
  227. * Get all of the OAuth clients for the user.
  228. */
  229. getClients() {
  230. axios.get('/oauth/clients')
  231. .then(response => {
  232. this.clients = response.data;
  233. });
  234. },
  235. /**
  236. * Show the form for creating new clients.
  237. */
  238. showCreateClientForm() {
  239. $('#modal-create-client').modal('show');
  240. },
  241. /**
  242. * Create a new OAuth client for the user.
  243. */
  244. store() {
  245. this.persistClient(
  246. 'post', '/oauth/clients',
  247. this.createForm, '#modal-create-client'
  248. );
  249. },
  250. /**
  251. * Edit the given client.
  252. */
  253. edit(client) {
  254. this.editForm.id = client.id;
  255. this.editForm.name = client.name;
  256. this.editForm.redirect = client.redirect;
  257. $('#modal-edit-client').modal('show');
  258. },
  259. /**
  260. * Update the client being edited.
  261. */
  262. update() {
  263. this.persistClient(
  264. 'put', '/oauth/clients/' + this.editForm.id,
  265. this.editForm, '#modal-edit-client'
  266. );
  267. },
  268. /**
  269. * Persist the client to storage using the given form.
  270. */
  271. persistClient(method, uri, form, modal) {
  272. form.errors = [];
  273. axios[method](uri, form)
  274. .then(response => {
  275. this.getClients();
  276. form.name = '';
  277. form.redirect = '';
  278. form.errors = [];
  279. $(modal).modal('hide');
  280. })
  281. .catch(error => {
  282. if (typeof error.response.data === 'object') {
  283. form.errors = _.flatten(_.toArray(error.response.data.errors));
  284. } else {
  285. form.errors = ['Something went wrong. Please try again.'];
  286. }
  287. });
  288. },
  289. /**
  290. * Destroy the given client.
  291. */
  292. destroy(client) {
  293. axios.delete('/oauth/clients/' + client.id)
  294. .then(response => {
  295. this.getClients();
  296. });
  297. }
  298. }
  299. }
  300. </script>