mirror of
https://framagit.org/les/gancio.git
synced 2025-02-01 00:52:01 +01:00
improve user_confirm / recover code flow
This commit is contained in:
parent
f8d1284437
commit
d68b04ff91
5 changed files with 30 additions and 23 deletions
|
@ -37,6 +37,7 @@
|
||||||
v-icon(v-if='item.is_active' color='success') mdi-check
|
v-icon(v-if='item.is_active' color='success') mdi-check
|
||||||
v-icon(v-else color='warning') mdi-close
|
v-icon(v-else color='warning') mdi-close
|
||||||
template(v-slot:item.actions='{item}')
|
template(v-slot:item.actions='{item}')
|
||||||
|
v-btn(v-if='item.recover_code' text small :to='`/user_confirm/${item.recover_code}`') {{$t('common.confirm')}}
|
||||||
v-btn(text small @click='toggle(item)'
|
v-btn(text small @click='toggle(item)'
|
||||||
:color='item.is_active?"warning":"success"') {{item.is_active?$t('common.disable'):$t('common.enable')}}
|
:color='item.is_active?"warning":"success"') {{item.is_active?$t('common.disable'):$t('common.enable')}}
|
||||||
v-btn(text small @click='toggleAdmin(item)'
|
v-btn(text small @click='toggleAdmin(item)'
|
||||||
|
|
|
@ -3,32 +3,32 @@ v-container
|
||||||
v-row.mt-5(align='center' justify='center')
|
v-row.mt-5(align='center' justify='center')
|
||||||
v-col(cols='12' md="6" lg="5" xl="4")
|
v-col(cols='12' md="6" lg="5" xl="4")
|
||||||
v-card
|
v-card
|
||||||
v-card-title {{settings.title}} - {{$t('common.recover_password')}}
|
v-card-title {{$t('common.recover_password')}}
|
||||||
v-card-text
|
template(v-if='user')
|
||||||
div(v-if='valid')
|
v-card-subtitle {{user.email}}
|
||||||
|
v-card-text
|
||||||
v-text-field(type='password'
|
v-text-field(type='password'
|
||||||
:rules="$validators.password"
|
:rules="$validators.password"
|
||||||
autofocus :placeholder='$t("common.new_password")'
|
autofocus :placeholder='$t("common.new_password")'
|
||||||
v-model='new_password')
|
v-model='new_password')
|
||||||
div(v-else) {{$t('recover.not_valid_code')}}
|
div(v-else) {{$t('recover.not_valid_code')}}
|
||||||
|
|
||||||
v-card-actions
|
v-card-actions
|
||||||
v-spacer
|
v-spacer
|
||||||
v-btn(v-if='valid' color='primary' @click='change_password') {{$t('common.send')}}
|
v-btn(v-if='user' text color='primary' @click='change_password') {{$t('common.send')}}
|
||||||
</template>
|
</template>
|
||||||
<script>
|
<script>
|
||||||
import { mapState } from 'vuex'
|
import { mapState } from 'vuex'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'Recover',
|
name: 'Recover',
|
||||||
layout: 'modal',
|
|
||||||
async asyncData ({ params, $axios }) {
|
async asyncData ({ params, $axios }) {
|
||||||
const code = params.code
|
const code = params.code
|
||||||
try {
|
try {
|
||||||
const valid = await $axios.$post('/user/check_recover_code', { recover_code: code })
|
const user = await $axios.$post('/user/check_recover_code', { recover_code: code })
|
||||||
return { valid, code }
|
return { user, code }
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return { valid: false }
|
return { user: false }
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
data () {
|
data () {
|
||||||
|
|
|
@ -3,16 +3,19 @@ v-container
|
||||||
v-row.mt-5(align='center' justify='center')
|
v-row.mt-5(align='center' justify='center')
|
||||||
v-col(cols='12' md="6" lg="5" xl="4")
|
v-col(cols='12' md="6" lg="5" xl="4")
|
||||||
v-card
|
v-card
|
||||||
v-card-title <nuxt-link to='/'><v-img src='/logo.png' max-width="40px" max-height="40px" contain class='mr-4'/></nuxt-link> {{$t('common.set_password')}}
|
v-card-title {{$t('common.set_password')}}
|
||||||
template(v-if='valid')
|
template(v-if='user')
|
||||||
v-card-text(v-if='valid')
|
v-card-subtitle {{user.email}}
|
||||||
v-form(v-if='valid')
|
v-card-text
|
||||||
v-text-field(type='password' v-model='new_password' :label="$t('common.new_password')")
|
v-form
|
||||||
|
v-text-field(type='password' v-model='new_password' :label="$t('common.new_password')" :rules='$validators.password' autofocus)
|
||||||
|
|
||||||
v-card-actions
|
v-card-actions
|
||||||
v-btn(color="success" :disabled='!new_password' @click='change_password') {{$t('common.send')}}
|
v-spacer
|
||||||
|
v-btn(text color="primary" :disabled='!new_password' @click='change_password') {{$t('common.send')}}
|
||||||
|
|
||||||
v-card-text(v-else) {{$t('recover.not_valid_code')}}
|
v-card-text(v-else)
|
||||||
|
v-alert.ma-5(type='error') {{$t('recover.not_valid_code')}}
|
||||||
|
|
||||||
</template>
|
</template>
|
||||||
<script>
|
<script>
|
||||||
|
@ -22,10 +25,10 @@ export default {
|
||||||
async asyncData ({ params, $axios }) {
|
async asyncData ({ params, $axios }) {
|
||||||
const code = params.code
|
const code = params.code
|
||||||
try {
|
try {
|
||||||
const valid = await $axios.$post('/user/check_recover_code', { recover_code: code })
|
const user = await $axios.$post('/user/check_recover_code', { recover_code: code })
|
||||||
return { valid, code }
|
return { user, code }
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return { valid: false }
|
return { user: false }
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
data () {
|
data () {
|
||||||
|
|
|
@ -26,7 +26,7 @@ const userController = {
|
||||||
if (!recover_code) { return res.sendStatus(400) }
|
if (!recover_code) { return res.sendStatus(400) }
|
||||||
const user = await User.findOne({ where: { recover_code: { [Op.eq]: recover_code } } })
|
const user = await User.findOne({ where: { recover_code: { [Op.eq]: recover_code } } })
|
||||||
if (!user) { return res.sendStatus(400) }
|
if (!user) { return res.sendStatus(400) }
|
||||||
res.sendStatus(200)
|
res.json({ email: user.email })
|
||||||
},
|
},
|
||||||
|
|
||||||
async updatePasswordWithRecoverCode (req, res) {
|
async updatePasswordWithRecoverCode (req, res) {
|
||||||
|
@ -50,7 +50,7 @@ const userController = {
|
||||||
},
|
},
|
||||||
|
|
||||||
async getAll (req, res) {
|
async getAll (req, res) {
|
||||||
const users = await User.scope('withoutPassword').findAll({
|
const users = await User.scope(req.user.is_admin ? 'withRecover' : 'withoutPassword').findAll({
|
||||||
order: [['is_admin', 'DESC'], ['createdAt', 'DESC']]
|
order: [['is_admin', 'DESC'], ['createdAt', 'DESC']]
|
||||||
})
|
})
|
||||||
res.json(users)
|
res.json(users)
|
||||||
|
@ -112,7 +112,7 @@ const userController = {
|
||||||
try {
|
try {
|
||||||
req.body.is_active = true
|
req.body.is_active = true
|
||||||
req.body.recover_code = crypto.randomBytes(16).toString('hex')
|
req.body.recover_code = crypto.randomBytes(16).toString('hex')
|
||||||
const user = await User.create(req.body)
|
const user = await User.scope('withRecover').create(req.body)
|
||||||
mail.send(user.email, 'user_confirm', { user, config }, req.settings.locale)
|
mail.send(user.email, 'user_confirm', { user, config }, req.settings.locale)
|
||||||
res.json(user)
|
res.json(user)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -125,7 +125,7 @@ const userController = {
|
||||||
try {
|
try {
|
||||||
const user = await User.findByPk(req.params.id)
|
const user = await User.findByPk(req.params.id)
|
||||||
await user.destroy()
|
await user.destroy()
|
||||||
log.warn(`User ${req.user.email} removed!`)
|
log.warn(`User ${user.email} removed!`)
|
||||||
res.sendStatus(200)
|
res.sendStatus(200)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
log.error('User removal error:"', e)
|
log.error('User removal error:"', e)
|
||||||
|
|
|
@ -30,6 +30,9 @@ User.init({
|
||||||
scopes: {
|
scopes: {
|
||||||
withoutPassword: {
|
withoutPassword: {
|
||||||
attributes: { exclude: ['password', 'recover_code'] }
|
attributes: { exclude: ['password', 'recover_code'] }
|
||||||
|
},
|
||||||
|
withRecover: {
|
||||||
|
attributes: { exclude: ['password'] }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue