add basic login
This commit is contained in:
parent
74dfd0a42a
commit
592de0a550
|
@ -18,13 +18,15 @@
|
|||
</b-collapse>
|
||||
|
||||
<b-navbar-nav class="ml-auto">
|
||||
<b-nav-item-dropdown right>
|
||||
<template slot="button-content"><div class="btn btn-sm btn-secondary">nemunaire</div></template>
|
||||
<b-nav-item-dropdown right v-if="loggedUser">
|
||||
<template slot="button-content"><div class="btn btn-sm btn-secondary">{{ loggedUser.email }}</div></template>
|
||||
<b-dropdown-item>Some example text that's free-flowing within the dropdown menu.</b-dropdown-item>
|
||||
<b-dropdown-item href="#">Action</b-dropdown-item>
|
||||
<b-dropdown-item href="#">Another action</b-dropdown-item>
|
||||
<b-dropdown-item href="#">Something else here</b-dropdown-item>
|
||||
<b-dropdown-item @click="logout()">Logout</b-dropdown-item>
|
||||
</b-nav-item-dropdown>
|
||||
<b-button v-if="!loggedUser" variant="success" @click="signup()"><span class="glyphicon glyphicon-user" aria-hidden="true"></span> Sign up</b-button>
|
||||
<b-button v-if="!loggedUser" variant="primary" class="ml-2" @click="signin()"><span class="glyphicon glyphicon-user" aria-hidden="true"></span> Sign in</b-button>
|
||||
</b-navbar-nav>
|
||||
</b-navbar>
|
||||
<div class="progress" style="background-color: #aee64e; height: 3px; border-radius: 0;">
|
||||
|
@ -34,3 +36,79 @@
|
|||
<router-view/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import axios from 'axios'
|
||||
|
||||
function updateSession (t) {
|
||||
if (sessionStorage.token !== undefined) {
|
||||
t.session = sessionStorage.token
|
||||
axios.get('/api/users/auth', {
|
||||
headers: {
|
||||
'Authorization': 'Bearer '.concat(t.session)
|
||||
}
|
||||
})
|
||||
.then(
|
||||
(response) => {
|
||||
t.loggedUser = response.data
|
||||
},
|
||||
(error) => {
|
||||
console.error('Invalid session, your have been logged out:', error.response.errmsg)
|
||||
t.session = null
|
||||
t.loggedUser = null
|
||||
sessionStorage.token = undefined
|
||||
t.$router.push('/')
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
|
||||
data: function () {
|
||||
return {
|
||||
loggedUser: null,
|
||||
session: null
|
||||
}
|
||||
},
|
||||
|
||||
mounted () {
|
||||
this.$on('login', this.login)
|
||||
updateSession(this)
|
||||
},
|
||||
|
||||
methods: {
|
||||
signin () {
|
||||
this.$router.push('/login')
|
||||
},
|
||||
signup () {
|
||||
this.$router.push('/join')
|
||||
},
|
||||
|
||||
logout () {
|
||||
sessionStorage.token = undefined
|
||||
updateSession(this)
|
||||
},
|
||||
|
||||
login (email, password) {
|
||||
axios
|
||||
.post('/api/users/auth', {
|
||||
'email': email,
|
||||
'password': password
|
||||
})
|
||||
.then(
|
||||
(response) => {
|
||||
if (response.data.id_session) {
|
||||
sessionStorage.token = response.data.id_session
|
||||
}
|
||||
updateSession(this)
|
||||
this.$router.push('/')
|
||||
},
|
||||
(error) => {
|
||||
alert('An error occurs when trying to login: ' + error.response.data.errmsg)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
|
|
@ -10,18 +10,32 @@ const routes = [
|
|||
name: 'home',
|
||||
component: Home
|
||||
},
|
||||
{
|
||||
path: '/login',
|
||||
name: 'login',
|
||||
component: function () {
|
||||
return import(/* webpackChunkName: "login" */ '../views/login.vue')
|
||||
}
|
||||
},
|
||||
{
|
||||
path: '/join',
|
||||
name: 'signup',
|
||||
component: function () {
|
||||
return import(/* webpackChunkName: "signup" */ '../views/signup.vue')
|
||||
}
|
||||
},
|
||||
{
|
||||
path: '/zones',
|
||||
name: 'zones',
|
||||
component: function () {
|
||||
return import(/* webpackChunkName: "about" */ '../views/zone-list.vue')
|
||||
return import(/* webpackChunkName: "zone-list" */ '../views/zone-list.vue')
|
||||
}
|
||||
},
|
||||
{
|
||||
path: '/zones/:zone',
|
||||
name: 'zone',
|
||||
component: function () {
|
||||
return import(/* webpackChunkName: "about" */ '../views/zone.vue')
|
||||
return import(/* webpackChunkName: "zone" */ '../views/zone.vue')
|
||||
}
|
||||
}
|
||||
]
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
<template>
|
||||
<div class="container mt-2">
|
||||
<b-button variant="success" @click="testlogin()"><span class="glyphicon glyphicon-user" aria-hidden="true"></span> Go!</b-button>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
|
||||
methods: {
|
||||
testlogin () {
|
||||
this.$parent.$emit('login', 'nemunaire@nemunai.re', 'ciphuj5O')
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
|
@ -0,0 +1,4 @@
|
|||
<template>
|
||||
<div class="container mt-2">
|
||||
</div>
|
||||
</template>
|
24
static.go
24
static.go
|
@ -31,6 +31,30 @@ func init() {
|
|||
fwd_request(w, r, DevProxy)
|
||||
}
|
||||
})
|
||||
api.Router().GET("/join", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
if DevProxy == "" {
|
||||
if data, err := Asset("htdocs/dist/index.html"); err != nil {
|
||||
fmt.Fprintf(w, "{\"errmsg\":%q}", err)
|
||||
} else {
|
||||
w.Write(data)
|
||||
}
|
||||
} else {
|
||||
r.URL.Path = "/"
|
||||
fwd_request(w, r, DevProxy)
|
||||
}
|
||||
})
|
||||
api.Router().GET("/login", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
if DevProxy == "" {
|
||||
if data, err := Asset("htdocs/dist/index.html"); err != nil {
|
||||
fmt.Fprintf(w, "{\"errmsg\":%q}", err)
|
||||
} else {
|
||||
w.Write(data)
|
||||
}
|
||||
} else {
|
||||
r.URL.Path = "/"
|
||||
fwd_request(w, r, DevProxy)
|
||||
}
|
||||
})
|
||||
api.Router().GET("/zones/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
if DevProxy == "" {
|
||||
if data, err := Asset("htdocs/dist/index.html"); err != nil {
|
||||
|
|
Loading…
Reference in New Issue