AdminInvite.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. <template>
  2. <div class="admin-invite-component">
  3. <div class="admin-invite-component-inner">
  4. <div class="card bg-dark">
  5. <div v-if="tabIndex === 0" class="card-body d-flex align-items-center justify-content-center">
  6. <div class="text-center">
  7. <b-spinner variant="muted" />
  8. <p class="text-muted mb-0">Loading...</p>
  9. </div>
  10. </div>
  11. <div v-else-if="tabIndex === 1" class="card-body">
  12. <div class="d-flex justify-content-center my-3">
  13. <img src="/img/pixelfed-icon-color.png" width="60" alt="Pixelfed logo" />
  14. </div>
  15. <div class="d-flex flex-column align-items-center justify-content-center">
  16. <p class="lead mb-1 text-muted">You've been invited to join</p>
  17. <p class="h3 mb-2">{{ instance.uri }}</p>
  18. <p class="mb-0 text-muted">
  19. <span>{{ instance.stats.user_count.toLocaleString('en-CA', { compactDisplay: "short", notation: "compact"}) }} users</span>
  20. <span>·</span>
  21. <span>{{ instance.stats.status_count.toLocaleString('en-CA', { compactDisplay: "short", notation: "compact"}) }} posts</span>
  22. </p>
  23. <div v-if="inviteConfig.message != 'You\'ve been invited to join'">
  24. <div class="admin-message">
  25. <p class="small text-light mb-0">Message from admin(s):</p>
  26. {{ inviteConfig.message }}
  27. </div>
  28. </div>
  29. </div>
  30. <div class="mt-5">
  31. <div class="form-group">
  32. <label for="username">Username</label>
  33. <input
  34. type="text"
  35. class="form-control form-control-lg"
  36. placeholder="What should everyone call you?"
  37. minlength="2"
  38. maxlength="15"
  39. v-model="form.username" />
  40. <p v-if="errors.username" class="form-text text-danger">
  41. <i class="far fa-exclamation-triangle mr-1"></i>
  42. {{ errors.username }}
  43. </p>
  44. </div>
  45. <button
  46. class="btn btn-primary btn-block font-weight-bold"
  47. @click="proceed(tabIndex)"
  48. :disabled="isProceeding || !form.username || form.username.length < 2">
  49. <template v-if="isProceeding">
  50. <b-spinner small />
  51. </template>
  52. <template v-else>
  53. Continue
  54. </template>
  55. </button>
  56. <p class="login-link">
  57. <a href="/login">Already have an account?</a>
  58. </p>
  59. <p class="register-terms">
  60. By registering, you agree to our <a href="/site/terms">Terms of Service</a> and <a href="/site/privacy">Privacy Policy</a>.
  61. </p>
  62. </div>
  63. </div>
  64. <div v-else-if="tabIndex === 2" class="card-body">
  65. <div class="d-flex justify-content-center my-3">
  66. <img src="/img/pixelfed-icon-color.png" width="60" alt="Pixelfed logo" />
  67. </div>
  68. <div class="d-flex flex-column align-items-center justify-content-center">
  69. <p class="lead mb-1 text-muted">You've been invited to join</p>
  70. <p class="h3 mb-2">{{ instance.uri }}</p>
  71. </div>
  72. <div class="mt-5">
  73. <div class="form-group">
  74. <label for="username">Email Address</label>
  75. <input
  76. type="email"
  77. class="form-control form-control-lg"
  78. placeholder="Your email address"
  79. v-model="form.email" />
  80. <p v-if="errors.email" class="form-text text-danger">
  81. <i class="far fa-exclamation-triangle mr-1"></i>
  82. {{ errors.email }}
  83. </p>
  84. </div>
  85. <button
  86. class="btn btn-primary btn-block font-weight-bold"
  87. @click="proceed(tabIndex)"
  88. :disabled="isProceeding || !form.email || !validateEmail()">
  89. <template v-if="isProceeding">
  90. <b-spinner small />
  91. </template>
  92. <template v-else>
  93. Continue
  94. </template>
  95. </button>
  96. </div>
  97. </div>
  98. <div v-else-if="tabIndex === 3" class="card-body">
  99. <div class="d-flex justify-content-center my-3">
  100. <img src="/img/pixelfed-icon-color.png" width="60" alt="Pixelfed logo" />
  101. </div>
  102. <div class="d-flex flex-column align-items-center justify-content-center">
  103. <p class="lead mb-1 text-muted">You've been invited to join</p>
  104. <p class="h3 mb-2">{{ instance.uri }}</p>
  105. </div>
  106. <div class="mt-5">
  107. <div class="form-group">
  108. <label for="username">Password</label>
  109. <input
  110. type="password"
  111. class="form-control form-control-lg"
  112. placeholder="Use a secure password"
  113. minlength="8"
  114. v-model="form.password" />
  115. <p v-if="errors.password" class="form-text text-danger">
  116. <i class="far fa-exclamation-triangle mr-1"></i>
  117. {{ errors.password }}
  118. </p>
  119. </div>
  120. <button
  121. class="btn btn-primary btn-block font-weight-bold"
  122. @click="proceed(tabIndex)"
  123. :disabled="isProceeding || !form.password || form.password.length < 8">
  124. <template v-if="isProceeding">
  125. <b-spinner small />
  126. </template>
  127. <template v-else>
  128. Continue
  129. </template>
  130. </button>
  131. </div>
  132. </div>
  133. <div v-else-if="tabIndex === 4" class="card-body">
  134. <div class="d-flex justify-content-center my-3">
  135. <img src="/img/pixelfed-icon-color.png" width="60" alt="Pixelfed logo" />
  136. </div>
  137. <div class="d-flex flex-column align-items-center justify-content-center">
  138. <p class="lead mb-1 text-muted">You've been invited to join</p>
  139. <p class="h3 mb-2">{{ instance.uri }}</p>
  140. </div>
  141. <div class="mt-5">
  142. <div class="form-group">
  143. <label for="username">Confirm Password</label>
  144. <input
  145. type="password"
  146. class="form-control form-control-lg"
  147. placeholder="Use a secure password"
  148. minlength="8"
  149. v-model="form.password_confirm" />
  150. <p v-if="errors.password_confirm" class="form-text text-danger">
  151. <i class="far fa-exclamation-triangle mr-1"></i>
  152. {{ errors.password_confirm }}
  153. </p>
  154. </div>
  155. <button
  156. class="btn btn-primary btn-block font-weight-bold"
  157. @click="proceed(tabIndex)"
  158. :disabled="isProceeding || !form.password_confirm || form.password !== form.password_confirm">
  159. <template v-if="isProceeding">
  160. <b-spinner small />
  161. </template>
  162. <template v-else>
  163. Continue
  164. </template>
  165. </button>
  166. </div>
  167. </div>
  168. <div v-else-if="tabIndex === 5" class="card-body">
  169. <div class="d-flex justify-content-center my-3">
  170. <img src="/img/pixelfed-icon-color.png" width="60" alt="Pixelfed logo" />
  171. </div>
  172. <div class="d-flex flex-column align-items-center justify-content-center">
  173. <p class="lead mb-1 text-muted">You've been invited to join</p>
  174. <p class="h3 mb-2">{{ instance.uri }}</p>
  175. </div>
  176. <div class="mt-5">
  177. <div class="form-group">
  178. <label for="username">Display Name</label>
  179. <input
  180. type="text"
  181. class="form-control form-control-lg"
  182. placeholder="Add an optional display name"
  183. minlength="8"
  184. v-model="form.display_name" />
  185. <p v-if="errors.display_name" class="form-text text-danger">
  186. <i class="far fa-exclamation-triangle mr-1"></i>
  187. {{ errors.display_name }}
  188. </p>
  189. </div>
  190. <button
  191. class="btn btn-primary btn-block font-weight-bold"
  192. @click="proceed(tabIndex)"
  193. :disabled="isProceeding">
  194. <template v-if="isProceeding">
  195. <b-spinner small />
  196. </template>
  197. <template v-else>
  198. Continue
  199. </template>
  200. </button>
  201. </div>
  202. </div>
  203. <div v-else-if="tabIndex === 6" class="card-body d-flex flex-column">
  204. <div class="d-flex justify-content-center my-3">
  205. <img src="/img/pixelfed-icon-color.png" width="60" alt="Pixelfed logo" />
  206. </div>
  207. <div class="d-flex flex-column align-items-center justify-content-center">
  208. <p class="lead mb-1 text-muted">You've been invited to join</p>
  209. <p class="h3 mb-2">{{ instance.uri }}</p>
  210. </div>
  211. <div class="mt-5 d-flex align-items-center justify-content-center flex-column flex-grow-1">
  212. <b-spinner variant="muted" />
  213. <p class="text-muted">Registering...</p>
  214. </div>
  215. </div>
  216. <div v-else-if="tabIndex === 'invalid-code'" class="card-body d-flex align-items-center justify-content-center">
  217. <div>
  218. <h1 class="text-center">Invalid Invite Code</h1>
  219. <hr>
  220. <p class="text-muted mb-1">The invite code you were provided is not valid, this can happen when:</p>
  221. <ul class="text-muted">
  222. <li>Invite code has typos</li>
  223. <li>Invite code was already used</li>
  224. <li>Invite code has reached max uses</li>
  225. <li>Invite code has expired</li>
  226. <li>You have been rate limited</li>
  227. </ul>
  228. <hr>
  229. <a href="/" class="btn btn-primary btn-block rounded-pill font-weight-bold">Go back home</a>
  230. </div>
  231. </div>
  232. <div v-else class="card-body">
  233. <p>An error occured.</p>
  234. </div>
  235. </div>
  236. </div>
  237. </div>
  238. </template>
  239. <script type="text/javascript">
  240. export default {
  241. props: ['code'],
  242. data() {
  243. return {
  244. instance: {},
  245. inviteConfig: {},
  246. tabIndex: 0,
  247. isProceeding: false,
  248. errors: {
  249. username: undefined,
  250. email: undefined,
  251. password: undefined,
  252. password_confirm: undefined
  253. },
  254. form: {
  255. username: undefined,
  256. email: undefined,
  257. password: undefined,
  258. password_confirm: undefined,
  259. display_name: undefined,
  260. }
  261. }
  262. },
  263. mounted() {
  264. this.fetchInstanceData();
  265. },
  266. methods: {
  267. fetchInstanceData() {
  268. axios.get('/api/v1/instance')
  269. .then(res => {
  270. this.instance = res.data;
  271. })
  272. .then(res => {
  273. this.verifyToken();
  274. })
  275. .catch(err => {
  276. console.log(err);
  277. })
  278. },
  279. verifyToken() {
  280. axios.post('/api/v1.1/auth/invite/admin/verify', {
  281. token: this.code,
  282. })
  283. .then(res => {
  284. this.tabIndex = 1;
  285. this.inviteConfig = res.data;
  286. })
  287. .catch(err => {
  288. this.tabIndex = 'invalid-code';
  289. })
  290. },
  291. checkUsernameAvailability() {
  292. axios.post('/api/v1.1/auth/invite/admin/uc', {
  293. token: this.code,
  294. username: this.form.username
  295. })
  296. .then(res => {
  297. if(res && res.data) {
  298. this.isProceeding = false;
  299. this.tabIndex = 2;
  300. } else {
  301. this.tabIndex = 'invalid-code';
  302. this.isProceeding = false;
  303. }
  304. })
  305. .catch(err => {
  306. if(err.response.data && err.response.data.username) {
  307. this.errors.username = err.response.data.username[0];
  308. this.isProceeding = false;
  309. } else {
  310. this.tabIndex = 'invalid-code';
  311. this.isProceeding = false;
  312. }
  313. })
  314. },
  315. checkEmailAvailability() {
  316. axios.post('/api/v1.1/auth/invite/admin/ec', {
  317. token: this.code,
  318. email: this.form.email
  319. })
  320. .then(res => {
  321. if(res && res.data) {
  322. this.isProceeding = false;
  323. this.tabIndex = 3;
  324. } else {
  325. this.tabIndex = 'invalid-code';
  326. this.isProceeding = false;
  327. }
  328. })
  329. .catch(err => {
  330. if(err.response.data && err.response.data.email) {
  331. this.errors.email = err.response.data.email[0];
  332. this.isProceeding = false;
  333. } else {
  334. this.tabIndex = 'invalid-code';
  335. this.isProceeding = false;
  336. }
  337. })
  338. },
  339. validateEmail() {
  340. if(!this.form.email || !this.form.email.length) {
  341. return false;
  342. }
  343. return /^[a-zA-Z]+[a-zA-Z0-9_.-]+@[a-zA-Z0-9_.-]+[a-zA-Z]$/i.test(this.form.email);
  344. },
  345. handleRegistration() {
  346. var $form = $('<form>', {
  347. action: '/api/v1.1/auth/invite/admin/re',
  348. method: 'post'
  349. });
  350. let fields = {
  351. '_token': document.head.querySelector('meta[name="csrf-token"]').content,
  352. token: this.code,
  353. username: this.form.username,
  354. name: this.form.display_name,
  355. email: this.form.email,
  356. password: this.form.password,
  357. password_confirm: this.form.password_confirm
  358. };
  359. $.each(fields, function(key, val) {
  360. $('<input>').attr({
  361. type: "hidden",
  362. name: key,
  363. value: val
  364. }).appendTo($form);
  365. });
  366. $form.appendTo('body').submit();
  367. },
  368. proceed(cur) {
  369. this.isProceeding = true;
  370. event.currentTarget.blur();
  371. switch(cur) {
  372. case 1:
  373. this.checkUsernameAvailability();
  374. break;
  375. case 2:
  376. this.checkEmailAvailability();
  377. break;
  378. case 3:
  379. this.isProceeding = false;
  380. this.tabIndex = 4;
  381. break;
  382. case 4:
  383. this.isProceeding = false;
  384. this.tabIndex = 5;
  385. break;
  386. case 5:
  387. this.tabIndex = 6;
  388. this.handleRegistration();
  389. break;
  390. }
  391. }
  392. }
  393. }
  394. </script>
  395. <style lang="scss">
  396. .admin-invite-component {
  397. font-family: var(--font-family-sans-serif);
  398. &-inner {
  399. display: flex;
  400. width: 100wv;
  401. height: 100vh;
  402. justify-content: center;
  403. align-items: center;
  404. .card {
  405. width: 100%;
  406. color: #fff;
  407. padding: 1.25rem 2.5rem;
  408. border-radius: 10px;
  409. min-height: 530px;
  410. @media(min-width: 768px) {
  411. width: 30%;
  412. }
  413. label {
  414. color: var(--muted);
  415. font-weight: bold;
  416. text-transform: uppercase;
  417. }
  418. .login-link {
  419. margin-top: 10px;
  420. font-weight: 600;
  421. }
  422. .register-terms {
  423. font-size: 12px;
  424. color: var(--muted);
  425. }
  426. .form-control {
  427. color: #fff;
  428. }
  429. .admin-message {
  430. margin-top: 20px;
  431. border: 1px solid var(--dropdown-item-hover-color);
  432. color: var(--text-lighter);
  433. padding: 1rem;
  434. border-radius: 5px;
  435. }
  436. }
  437. }
  438. }
  439. </style>