Clients.vue 12 KB

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