Przechowywanie Tokenu z backendu i widok do komponentow tylko dla zalogowanych
This commit is contained in:
parent
5559e8ace6
commit
b28f9a4bf2
@ -110,7 +110,7 @@ export default {
|
|||||||
this.snackbarLogout = true
|
this.snackbarLogout = true
|
||||||
this.isLogged = false
|
this.isLogged = false
|
||||||
console.log("wylogowano")
|
console.log("wylogowano")
|
||||||
window.location.replace("http://localhost:8080");
|
// window.location.replace("http://localhost:8080");
|
||||||
|
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
|
@ -6,11 +6,28 @@ import Cards from '@/views/Cards'
|
|||||||
import Login from '@/views/Login'
|
import Login from '@/views/Login'
|
||||||
import Logout from '@/views/Logout'
|
import Logout from '@/views/Logout'
|
||||||
import Register from '@/views/Register'
|
import Register from '@/views/Register'
|
||||||
|
import store from '@/store/store'
|
||||||
|
|
||||||
|
|
||||||
Vue.use(Router)
|
Vue.use(Router)
|
||||||
|
|
||||||
|
const ifNotAuthenticated = (to, from, next) => {
|
||||||
|
if (!store.getters.isAuthenticated) {
|
||||||
|
next()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
next('/')
|
||||||
|
}
|
||||||
|
|
||||||
|
const ifAuthenticated = (to, from, next) => {
|
||||||
|
if (store.getters.isAuthenticated) {
|
||||||
|
next()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
next('/login')
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
export default new Router({
|
export default new Router({
|
||||||
mode: 'history',
|
mode: 'history',
|
||||||
base: process.env.BASE_URL,
|
base: process.env.BASE_URL,
|
||||||
@ -18,17 +35,20 @@ export default new Router({
|
|||||||
{
|
{
|
||||||
path: '/game',
|
path: '/game',
|
||||||
name: 'game',
|
name: 'game',
|
||||||
component: Game
|
component: Game,
|
||||||
|
// beforeEnter: ifAuthenticated,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: '/dzentelmani',
|
path: '/dzentelmani',
|
||||||
name: 'users',
|
name: 'users',
|
||||||
component: Users
|
component: Users,
|
||||||
|
// beforeEnter: ifAuthenticated,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: '/karty',
|
path: '/karty',
|
||||||
name: 'cards',
|
name: 'cards',
|
||||||
component: Cards
|
component: Cards,
|
||||||
|
// beforeEnter: ifAuthenticated,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: '/register',
|
path: '/register',
|
||||||
@ -40,10 +60,16 @@ export default new Router({
|
|||||||
name: 'login',
|
name: 'login',
|
||||||
component: Login
|
component: Login
|
||||||
},
|
},
|
||||||
|
// {
|
||||||
|
// path: '/login', //doylsnie
|
||||||
|
// name: 'login',
|
||||||
|
// component: Login
|
||||||
|
// },
|
||||||
{
|
{
|
||||||
path: '/logout',
|
path: '/logout',
|
||||||
name: 'logout',
|
name: 'logout',
|
||||||
component: Logout
|
component: Logout,
|
||||||
}
|
// beforeEnter: ifNotAuthenticated,
|
||||||
|
},
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
|
@ -14,8 +14,7 @@ export const store = new Vuex.Store({
|
|||||||
// { icon: 'get_app', text: 'Zaloguj', route: '/login'},
|
// { icon: 'get_app', text: 'Zaloguj', route: '/login'},
|
||||||
// { icon: 'account_circle', text: 'Rejestracja', route: '/register'},
|
// { icon: 'account_circle', text: 'Rejestracja', route: '/register'},
|
||||||
],
|
],
|
||||||
// userToken : null // na przechoywanie tokena z GO dla usera
|
token: null, // na przechoywanie tokena z GO dla usera
|
||||||
token: null,
|
|
||||||
user: null
|
user: null
|
||||||
},
|
},
|
||||||
getters:{
|
getters:{
|
||||||
|
@ -136,6 +136,25 @@ export default {
|
|||||||
//cyklicznie ywkonujaca sie funkcja - do sprawdzania zmian w bazie co 10 s - żeby plynnie wyswietlac nowo dodane karty jesli zmiana zostanie zauwaona
|
//cyklicznie ywkonujaca sie funkcja - do sprawdzania zmian w bazie co 10 s - żeby plynnie wyswietlac nowo dodane karty jesli zmiana zostanie zauwaona
|
||||||
this.timer = setInterval(this.checkDiference, 10000) //na biezaco aktualizuje dane
|
this.timer = setInterval(this.checkDiference, 10000) //na biezaco aktualizuje dane
|
||||||
|
|
||||||
|
},
|
||||||
|
created(){
|
||||||
|
//Zbuduje sie tylko i wylacznie wtedy gdy uzytkownik jest zalogowany, w przeciwnym wypadku przekierowuj do zalogowania
|
||||||
|
var local = localStorage.getItem('token');
|
||||||
|
var localU = localStorage.getItem('user');
|
||||||
|
|
||||||
|
console.log("localStorage.getItem('token')" , localStorage.getItem('token'))
|
||||||
|
|
||||||
|
if (localStorage.getItem('token')){
|
||||||
|
this.isLogged = true
|
||||||
|
console.log("TUTAJ: ", localStorage.getItem('token') === null)
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
this.$router.push('/')
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("LOCAL STOARAGE: ", local)
|
||||||
|
console.log("LOCAL STOARAGE User: ", localU)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,6 +46,7 @@
|
|||||||
export default {
|
export default {
|
||||||
data(){
|
data(){
|
||||||
return{
|
return{
|
||||||
|
isLogged : false,
|
||||||
gentelaman: false,
|
gentelaman: false,
|
||||||
cards: [
|
cards: [
|
||||||
{ text: 'Te majce mniej niż 180cm wzrostu pocieszne istoty które nazywają siebie mężczyznami'},
|
{ text: 'Te majce mniej niż 180cm wzrostu pocieszne istoty które nazywają siebie mężczyznami'},
|
||||||
@ -73,10 +74,10 @@ export default {
|
|||||||
},
|
},
|
||||||
methods:{
|
methods:{
|
||||||
chosenCard(){
|
chosenCard(){
|
||||||
console.log("Wybrano karte")
|
// console.log("Wybrano karte")
|
||||||
},
|
},
|
||||||
visibleCards(){
|
visibleCards(){
|
||||||
console.log("Visible cards function")
|
// console.log("Visible cards function")
|
||||||
if (this.gentelaman===true){
|
if (this.gentelaman===true){
|
||||||
return this.cards
|
return this.cards
|
||||||
}
|
}
|
||||||
@ -84,6 +85,25 @@ export default {
|
|||||||
return this.userCards
|
return this.userCards
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
created(){
|
||||||
|
//Zbuduje sie tylko i wylacznie wtedy gdy uzytkownik jest zalogowany, w przeciwnym wypadku przekierowuj do zalogowania
|
||||||
|
var local = localStorage.getItem('token');
|
||||||
|
var localU = localStorage.getItem('user');
|
||||||
|
|
||||||
|
console.log("localStorage.getItem('token')" , localStorage.getItem('token'))
|
||||||
|
|
||||||
|
if (localStorage.getItem('token')){
|
||||||
|
this.isLogged = true
|
||||||
|
console.log("TUTAJ: ", localStorage.getItem('token') === null)
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
this.$router.push('/')
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("LOCAL STOARAGE: ", local)
|
||||||
|
console.log("LOCAL STOARAGE User: ", localU)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
@ -71,26 +71,39 @@ export default {
|
|||||||
|
|
||||||
if (response.data){
|
if (response.data){
|
||||||
console.log("Dostał tokena")
|
console.log("Dostał tokena")
|
||||||
this.$store.commit('LOGIN_SUCCESS', response.data)
|
|
||||||
console.log("DANE Z VUEXA DLA USERA")
|
console.log("DANE Z VUEXA DLA USERA")
|
||||||
console.log(this.$store.state.token) //pokazuje ze zapisał tokena
|
localStorage.setItem('token',response.data);
|
||||||
window.location.replace("http://localhost:8080/game");
|
localStorage.setItem('user', JSON.stringify(this.userLogin));
|
||||||
|
|
||||||
|
|
||||||
|
var local = localStorage.getItem('token');
|
||||||
|
var localU = localStorage.getItem('user');
|
||||||
|
|
||||||
|
console.log("LOCAL STOARAGE: ", local)
|
||||||
|
console.log("LOCAL STOARAGE User: ", localU)
|
||||||
|
|
||||||
|
this.$router.push('game')
|
||||||
|
|
||||||
|
// router.push({name: "/game"}) //przekierowanie dla one sigle page application window.location.replace("http://localhost:8080/game");
|
||||||
|
|
||||||
}
|
}
|
||||||
else{ //coś poszło nie tak
|
else{ //coś poszło nie tak
|
||||||
console.log("Zły lolgin")
|
console.log("Zły lolgin")
|
||||||
this.snackbar = true
|
this.snackbar = true
|
||||||
this.userLogin = ''
|
this.userLogin = ''
|
||||||
this.userPassword = ''
|
this.userPassword = ''
|
||||||
|
localStorage.setItem('token',null);
|
||||||
|
localStorage.setItem('user', null);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(error=>{
|
.catch(error=>{
|
||||||
console.log("ERROR: ", error.response.data)
|
console.log("ERROR: ", error.response.data)
|
||||||
});
|
});
|
||||||
|
|
||||||
}//dlugi if
|
}else{ //dlugi if
|
||||||
|
|
||||||
this.userLogin = ''
|
this.userLogin = ''
|
||||||
this.userPassword = ''
|
this.userPassword = ''
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,20 @@
|
|||||||
<template>
|
<template>
|
||||||
<h1>Wylogowanie</h1>
|
<h1>Wylogowanie</h1>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
export default {
|
||||||
|
data(){
|
||||||
|
return{
|
||||||
|
|
||||||
|
}
|
||||||
|
},
|
||||||
|
methods:{
|
||||||
|
logout() {
|
||||||
|
// remove user from local storage to log user out
|
||||||
|
localStorage.removeItem('user');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
</script>
|
||||||
|
@ -82,6 +82,26 @@ export default {
|
|||||||
},
|
},
|
||||||
mounted(){
|
mounted(){
|
||||||
this.getAllUsers()
|
this.getAllUsers()
|
||||||
|
},
|
||||||
|
|
||||||
|
created(){
|
||||||
|
//Zbuduje sie tylko i wylacznie wtedy gdy uzytkownik jest zalogowany, w przeciwnym wypadku przekierowuj do zalogowania
|
||||||
|
var local = localStorage.getItem('token');
|
||||||
|
var localU = localStorage.getItem('user');
|
||||||
|
|
||||||
|
console.log("localStorage.getItem('token')" , localStorage.getItem('token'))
|
||||||
|
|
||||||
|
if (localStorage.getItem('token')){
|
||||||
|
this.isLogged = true
|
||||||
|
console.log("TUTAJ: ", localStorage.getItem('token') === null)
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
this.$router.push('/')
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("LOCAL STOARAGE: ", local)
|
||||||
|
console.log("LOCAL STOARAGE User: ", localU)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
Loading…
Reference in New Issue
Block a user