mirror of
https://github.com/YunoHost/yunohost-admin.git
synced 2024-09-03 20:06:15 +02:00
add protection to routes and login/logout behavior
This commit is contained in:
parent
0334515a43
commit
9232961b61
4 changed files with 81 additions and 13 deletions
|
@ -9,8 +9,8 @@
|
||||||
{{ $t('user_interface_link') }} <icon iname="user" class="sm"/>
|
{{ $t('user_interface_link') }} <icon iname="user" class="sm"/>
|
||||||
</b-button>
|
</b-button>
|
||||||
</li>
|
</li>
|
||||||
<li class="nav-item">
|
<li class="nav-item" v-bind:hidden="!connected">
|
||||||
<b-button to="/logout" variant="outline-dark" block size="sm">
|
<b-button @click.prevent="logout" to="/logout" variant="outline-dark" block size="sm" >
|
||||||
{{ $t('logout') }} <icon iname="sign-out" class="sm"/>
|
{{ $t('logout') }} <icon iname="sign-out" class="sm"/>
|
||||||
</b-button>
|
</b-button>
|
||||||
</li>
|
</li>
|
||||||
|
@ -41,6 +41,29 @@
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
import api from '@/helpers/api'
|
||||||
|
|
||||||
|
|
||||||
|
export default {
|
||||||
|
name: 'App',
|
||||||
|
computed: {
|
||||||
|
connected: function () {
|
||||||
|
return this.$store.state.connected
|
||||||
|
}
|
||||||
|
},
|
||||||
|
methods: {
|
||||||
|
async logout() {
|
||||||
|
const disconnected = await api.logout()
|
||||||
|
if (disconnected) {
|
||||||
|
this.$store.commit('CONNECTED', false);
|
||||||
|
this.$router.push('/login')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
<style lang="scss">
|
<style lang="scss">
|
||||||
@import '@/scss/main.scss';
|
@import '@/scss/main.scss';
|
||||||
|
|
||||||
|
@ -50,7 +73,7 @@ header {
|
||||||
margin-bottom: 2rem;
|
margin-bottom: 2rem;
|
||||||
|
|
||||||
.navbar {
|
.navbar {
|
||||||
padding: 1rem;
|
padding: 1rem 0;
|
||||||
|
|
||||||
img {
|
img {
|
||||||
width: 70px;
|
width: 70px;
|
||||||
|
|
|
@ -1,12 +1,26 @@
|
||||||
import Vue from 'vue'
|
import Vue from 'vue'
|
||||||
import VueRouter from 'vue-router'
|
import VueRouter from 'vue-router'
|
||||||
import routes from '../routes'
|
import routes from '../routes'
|
||||||
|
import store from './store'
|
||||||
|
|
||||||
|
|
||||||
Vue.use(VueRouter)
|
Vue.use(VueRouter)
|
||||||
|
|
||||||
export default new VueRouter({
|
const router = new VueRouter({
|
||||||
mode: 'history',
|
// mode: 'history', // this allow all routes to be real ones (without '#')
|
||||||
base: process.env.BASE_URL,
|
base: process.env.BASE_URL,
|
||||||
routes
|
routes
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Before each route request hook
|
||||||
|
router.beforeEach((to, from, next) => {
|
||||||
|
// Allow if connected or route is not protected
|
||||||
|
if (store.state.connected || to.meta.noAuth) {
|
||||||
|
next()
|
||||||
|
} else {
|
||||||
|
next({path: '/login', query: {redirect: to.path}})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
export default router;
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
import Home from './views/Home'
|
import Home from './views/Home'
|
||||||
import Login from './views/Login'
|
import Login from './views/Login'
|
||||||
|
|
||||||
|
|
||||||
const routes = [
|
const routes = [
|
||||||
{path: '/', component: Home},
|
{path: '/', component: Home},
|
||||||
{path: '/login', component: Login},
|
{path: '/login', component: Login, meta: {noAuth: true}},
|
||||||
]
|
]
|
||||||
|
|
||||||
export default routes
|
export default routes
|
||||||
|
|
|
@ -8,26 +8,56 @@
|
||||||
<icon iname="lock" class="sm"/>
|
<icon iname="lock" class="sm"/>
|
||||||
</b-input-group-text>
|
</b-input-group-text>
|
||||||
</template>
|
</template>
|
||||||
<b-form-input required type="password" v-model="password" id="input-password" :placeholder="$t('administration_password')"></b-form-input>
|
<b-form-input required id="input-password"
|
||||||
<b-input-group-append>
|
v-model="password"
|
||||||
|
type="password"
|
||||||
|
:placeholder="$t('administration_password')"
|
||||||
|
:state="state"
|
||||||
|
></b-form-input>
|
||||||
|
<template v-slot:append>
|
||||||
<b-button type="submit" variant="success">{{ $t('login') }}</b-button>
|
<b-button type="submit" variant="success">{{ $t('login') }}</b-button>
|
||||||
</b-input-group-append>
|
</template>
|
||||||
</b-input-group>
|
</b-input-group>
|
||||||
|
<b-form-invalid-feedback :state="state">
|
||||||
|
{{ $t('wrong_password') }}
|
||||||
|
</b-form-invalid-feedback>
|
||||||
</b-form>
|
</b-form>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
import api from '@/helpers/api'
|
||||||
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'Login',
|
name: 'Login',
|
||||||
data: () => {
|
data: () => {
|
||||||
return {
|
return {
|
||||||
password: ''
|
password: '',
|
||||||
|
state: null,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
login: function () {
|
async login() {
|
||||||
console.log(this.password);
|
const connected = await api.login(this.password)
|
||||||
|
if (connected) {
|
||||||
|
this.$store.commit('CONNECTED', true);
|
||||||
|
this.$router.push(this.$route.query.redirect || '/')
|
||||||
|
} else {
|
||||||
|
this.$store.commit('CONNECTED', false);
|
||||||
|
this.state = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
// TODO checkInstall
|
||||||
|
// beforeRouteEnter (to, from, next) {
|
||||||
|
// },
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
<style lang="scss" scoped>
|
||||||
|
.input-group.is-invalid {
|
||||||
|
~ .invalid-feedback {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
Loading…
Add table
Reference in a new issue