logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://hacktivis.me/git/pleroma-fe.git

login_form.js (2685B)


  1. import { mapState, mapGetters, mapActions, mapMutations } from 'vuex'
  2. import oauthApi from '../../services/new_api/oauth.js'
  3. import { library } from '@fortawesome/fontawesome-svg-core'
  4. import {
  5. faTimes
  6. } from '@fortawesome/free-solid-svg-icons'
  7. library.add(
  8. faTimes
  9. )
  10. const LoginForm = {
  11. data: () => ({
  12. user: {},
  13. error: false
  14. }),
  15. computed: {
  16. isPasswordAuth () { return this.requiredPassword },
  17. isTokenAuth () { return this.requiredToken },
  18. ...mapState({
  19. registrationOpen: state => state.instance.registrationOpen,
  20. instance: state => state.instance,
  21. loggingIn: state => state.users.loggingIn,
  22. oauth: state => state.oauth
  23. }),
  24. ...mapGetters(
  25. 'authFlow', ['requiredPassword', 'requiredToken', 'requiredMFA']
  26. )
  27. },
  28. methods: {
  29. ...mapMutations('authFlow', ['requireMFA']),
  30. ...mapActions({ login: 'authFlow/login' }),
  31. submit () {
  32. this.isTokenAuth ? this.submitToken() : this.submitPassword()
  33. },
  34. submitToken () {
  35. const { clientId, clientSecret } = this.oauth
  36. const data = {
  37. clientId,
  38. clientSecret,
  39. instance: this.instance.server,
  40. commit: this.$store.commit
  41. }
  42. oauthApi.getOrCreateApp(data)
  43. .then((app) => { oauthApi.login({ ...app, ...data }) })
  44. },
  45. submitPassword () {
  46. const { clientId } = this.oauth
  47. const data = {
  48. clientId,
  49. oauth: this.oauth,
  50. instance: this.instance.server,
  51. commit: this.$store.commit
  52. }
  53. this.error = false
  54. oauthApi.getOrCreateApp(data).then((app) => {
  55. oauthApi.getTokenWithCredentials(
  56. {
  57. ...app,
  58. instance: data.instance,
  59. username: this.user.username,
  60. password: this.user.password
  61. }
  62. ).then((result) => {
  63. if (result.error) {
  64. if (result.error === 'mfa_required') {
  65. this.requireMFA({ settings: result })
  66. } else if (result.identifier === 'password_reset_required') {
  67. this.$router.push({ name: 'password-reset', params: { passwordResetRequested: true } })
  68. } else {
  69. this.error = result.error
  70. this.focusOnPasswordInput()
  71. }
  72. return
  73. }
  74. this.login(result).then(() => {
  75. this.$router.push({ name: 'friends' })
  76. })
  77. })
  78. })
  79. },
  80. clearError () { this.error = false },
  81. focusOnPasswordInput () {
  82. const passwordInput = this.$refs.passwordInput
  83. passwordInput.focus()
  84. passwordInput.setSelectionRange(0, passwordInput.value.length)
  85. }
  86. }
  87. }
  88. export default LoginForm