[refactoring] login/register as dialog

This commit is contained in:
les
2019-10-22 23:47:41 +02:00
parent b4d288b132
commit d468ef33f7
4 changed files with 99 additions and 50 deletions

View File

@@ -1,78 +0,0 @@
<template lang='pug'>
el-card
nuxt-link.float-right(to='/')
el-button(circle icon='el-icon-close' type='danger' size='small' plain)
h5 {{$t('common.login')}}
el-form(v-loading='loading')
p(v-html="$t('login.description')")
el-input.mb-2(v-model='email' type='email' name='email'
:placeholder='$t("common.email")' autocomplete='email' ref='email')
v-icon(name='envelope' slot='prepend')
el-input.mb-1(v-model='password' @keyup.enter.native="submit" name='password'
type='password' :placeholder='$t("common.password")')
v-icon(name='lock' slot='prepend')
el-button.mr-1(plain type="success"
:disabled='disabled' @click='submit') {{$t('common.login')}}
nuxt-link(to='/register' v-if='settings.allow_registration')
el-button.mt-1(plain type="primary") {{$t('login.not_registered')}}
a.float-right(href='#' @click='forgot') {{$t('login.forgot_password')}}
</template>
<script>
import { mapActions, mapState } from 'vuex'
import { Message } from 'element-ui'
import get from 'lodash/get'
export default {
name: 'Login',
data () {
return {
password: '',
email: '',
loading: false
}
},
computed: {
...mapState(['settings']),
disabled () {
if (process.server) { return false }
return !this.email || !this.password
}
},
methods: {
...mapActions(['login']),
async forgot () {
if (!this.email) {
Message({ message: this.$t('login.insert_email'), showClose: true, type: 'error' })
this.$refs.email.focus()
return
}
this.loading = true
await this.$axios.$post('/user/recover', { email: this.email })
this.loading = false
Message({ message: this.$t('login.check_email'), type: 'success' })
},
async submit (e) {
e.preventDefault()
try {
this.loading = true
await this.$auth.loginWith('local', { data: { email: this.email, password: this.password } })
this.loading = false
Message({ message: this.$t('login.ok'), showClose: true, type: 'success' })
} catch (e) {
e = get(e, 'response.data.message', e)
Message({ message: this.$t('login.error') + this.$t(e), showClose: true, type: 'error' })
this.loading = false
return
}
this.email = this.password = ''
}
}
}
</script>

View File

@@ -1,81 +0,0 @@
<template lang='pug'>
el-card
nuxt-link.float-right(to='/')
el-button(circle icon='el-icon-close' type='danger' size='small' plain)
h5 {{$t('common.register')}}
el-form(@submit.native.prevent='register' method='POST' action='')
p(v-html="$t('register.description')")
el-input.mb-2(v-model='user.username' type='text' name='username'
:placeholder='$t("common.username")')
v-icon(name='user' slot='prepend')
el-input.mb-2(ref='email' v-model='user.email' type='email' required
:placeholder='$t("common.email")' autocomplete='email' name='email')
v-icon(name='envelope' slot='prepend')
el-input.mb-2(v-model='user.password' type="password" placeholder="Password" name='password' required)
v-icon(name='lock' slot='prepend')
el-input.mb-2(v-model='user.description' type="textarea" rows='3' :placeholder="$t('common.description')")
v-icon(name='envelope-open-text')
el-button(plain type="success" native-type='submit'
:disabled='disabled') {{$t('common.send')}} <v-icon :name='loading?"circle-notch":"chevron-right"' :spin='loading'/>
</template>
<script>
import { mapActions, mapState } from 'vuex'
import { Message } from 'element-ui'
import get from 'lodash/get'
export default {
name: 'Register',
data () {
return {
loading: false,
user: {}
}
},
head () {
return {
title: this.settings.title + ' - ' + this.$t('common.register')
}
},
validate ({ store }) {
return store.state.settings.allow_registration
},
computed: {
...mapState(['settings']),
disabled () {
if (process.server) { return false }
return !this.user.password || !this.user.email || !this.user.description
}
},
methods: {
...mapActions(['login']),
async register () {
this.loading = true
try {
const { user } = await this.$axios.$post('/user/register', this.user)
Message({
showClose: true,
message: this.$t(`register.${user.is_admin ? 'admin_' : ''}complete`),
type: 'success'
})
this.$router.replace('/')
} catch (e) {
const error = get(e, 'response.data.errors[0].message', String(e))
Message({
showClose: true,
message: this.$t(error),
type: 'error'
})
}
this.loading = false
}
}
}
</script>