Skip to content

Commit 5204c16

Browse files
author
oguzhan.onder
committed
added token expiry date
1 parent e83d39c commit 5204c16

File tree

13 files changed

+94
-89
lines changed

13 files changed

+94
-89
lines changed

package-lock.json

Lines changed: 2 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/App.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
return {}
2020
},
2121
created() {
22-
if (this.$store.state.isLogin == null) {
22+
if (this.$store.getters.getIsLogin == null) {
2323
this.$store.dispatch("initIsLogin");
2424
}
25-
if (this.$store.state.isLogin) {
25+
if (this.$store.getters.getIsLogin) {
2626
this.$store.dispatch("getTradeResult");
2727
this.$store.dispatch("initApp");
2828
}

src/components/Footer.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<nav v-if="this.$store.state.isLogin" class="navbar fixed-bottom navbar-light" style="background-color: #e3f2fd;">
2+
<nav v-if="getIsLogin" class="navbar fixed-bottom navbar-light" style="background-color: #e3f2fd;">
33
<span class="navbar-text mr-auto">
44
<strong>Vuejs Çalışmaları</strong> | Oğuzhan ÖNDER
55
</span>
@@ -23,7 +23,7 @@
2323
2424
export default {
2525
name: "Footer",
26-
computed: mapGetters(["getTradeResult"])
26+
computed: mapGetters(["getTradeResult","getIsLogin"])
2727
}
2828
</script>
2929

src/components/Header.vue

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<nav v-if="this.$store.state.isLogin" class="navbar navbar-expand-lg navbar-dark" style="background-color: #563F7A">
2+
<nav v-if="getIsLogin" class="navbar navbar-expand-lg navbar-dark" style="background-color: #563F7A">
33
<router-link to="/" class="navbar-brand">Ürün Listesi</router-link>
44
<div class="collapse navbar-collapse" id="navbarSupportedContent">
55
<ul class="navbar-nav mr-auto">
@@ -10,6 +10,7 @@
1010
<router-link to="/sell" class="nav-link">Ürün Çıkışı</router-link>
1111
</li>
1212
</ul>
13+
1314
<ul class="navbar-nav my-2 my-lg-0">
1415
<li class="nav-item">
1516
<a href="#" @click="logout" class="nav-link">Çıkış Yap</a>
@@ -20,15 +21,17 @@
2021
</template>
2122

2223
<script>
24+
import {mapGetters} from 'vuex';
25+
2326
export default {
2427
name: "Header",
2528
methods:{
2629
logout(){
27-
this.$store.dispatch("logout")
28-
.then(res => {
29-
this.$router.push("/login");
30-
})
30+
this.$store.dispatch("logout");
3131
}
32+
},
33+
computed:{
34+
...mapGetters(["getIsLogin"]),
3235
}
3336
}
3437
</script>

src/components/Login.vue

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,6 @@
3838
methods: {
3939
login() {
4040
this.$store.dispatch("login", this.loginRequest)
41-
.then(res => {
42-
if (res) {
43-
this.$store.dispatch("getTradeResult");
44-
this.$store.dispatch("initApp");
45-
this.$router.push("/");
46-
}
47-
})
4841
},
4942
},
5043
}

src/components/ProductList.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<div v-if="this.$store.state.isLogin" class="container">
2+
<div v-if="getIsLogin" class="container">
33
<div class="row">
44
<div class="col-12 mb-5 card mt-5 shadow">
55
<div class="card-body">
@@ -43,7 +43,7 @@
4343
export default {
4444
name: "ProductList",
4545
computed: {
46-
...mapGetters(["getProductList"]),
46+
...mapGetters(["getProductList","getIsLogin"]),
4747
},
4848
methods: {
4949
getCountClass(count) {

src/components/ProductPurchase.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<div v-if="this.$store.state.isLogin" class="container">
2+
<div v-if="getIsLogin" class="container">
33
<div class="loading" :style="isLoading">
44
<div class="lds-ripple">
55
<div></div>
@@ -38,6 +38,7 @@
3838

3939
<script>
4040
import {productMixin} from '../util/productMixin';
41+
import {mapGetters} from 'vuex';
4142
4243
export default {
4344
name: "ProductPurchase",
@@ -67,6 +68,7 @@
6768
return true;
6869
}
6970
},
71+
...mapGetters(["getIsLogin"]),
7072
},
7173
beforeRouteLeave(to, from, next) {
7274
if ((this.product.title.length > 0 || this.product.description.length > 0 || this.product.price > 0 || this.product.count > 0) && !this.saveButtonClicked) {

src/components/ProductSell.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<div v-if="this.$store.state.isLogin" class="container">
2+
<div v-if="getIsLogin" class="container">
33
<div class="loading" :style="isLoading">
44
<div class="lds-ripple">
55
<div></div>
@@ -63,7 +63,7 @@
6363
}
6464
},
6565
computed: {
66-
...mapGetters(["getProductList"]),
66+
...mapGetters(["getProductList","getIsLogin"]),
6767
saveEnabled() {
6868
if (this.sellCount > 0 && this.selected != null) {
6969
return false;

src/store/actions.js

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import * as util from '../util/util'
1+
import * as util from '../util/util';
2+
import router from '../util/router';
23

34
export const setTradeResult = ({state, commit}, tradeResult) => {
45
util.service.post("trade/createOrUpdate", tradeResult)
@@ -16,7 +17,7 @@ export const getTradeResult = ({commit}) => {
1617
commit("updateTradeResult", response.data);
1718
}
1819
}).catch(error => {
19-
util.notify.control(commit, error)
20+
util.notify.control(error)
2021
})
2122
};
2223

@@ -30,7 +31,11 @@ export const login = (vueContext, loginData) => {
3031
if (response) {
3132
vueContext.commit("setIsLogin", true);
3233
localStorage.setItem(util.token, response.data.token);
33-
return response;
34+
localStorage.setItem(util.expiry, response.data.expirationDate);
35+
vueContext.dispatch("setTimeOutTimerExpiry", response.data.expirationDate);
36+
vueContext.dispatch("initApp");
37+
vueContext.dispatch("getTradeResult");
38+
router.push("/");
3439
}
3540
}).catch(error => {
3641
loginData.password = null;
@@ -44,10 +49,12 @@ export const logout = (vueContext) => {
4449
if (res) {
4550
vueContext.commit("setIsLogin", false);
4651
localStorage.removeItem(util.token);
47-
return res;
52+
localStorage.removeItem(util.expiry);
53+
router.push("/login");
4854
}
55+
}).catch(err=>{
56+
util.notify.control(err);
4957
});
50-
5158
};
5259

5360
export const userRegister = (vueContext, registerData) => {
@@ -56,15 +63,32 @@ export const userRegister = (vueContext, registerData) => {
5663
if (response) {
5764
return response;
5865
}
59-
}).catch(error=>{
66+
}).catch(error => {
6067
registerData = {};
6168
})
6269
};
6370

71+
export const setTimeOutTimerExpiry = (vueContext, expiry) => {
72+
setTimeout(() => {
73+
vueContext.dispatch("logout");
74+
}, expiry);
75+
}
76+
6477
export const initIsLogin = (vueContext) => {
78+
let obj= {};
79+
obj.status = 401;
6580
if (localStorage.getItem(util.token)) {
66-
vueContext.commit("setIsLogin", true)
81+
let now = new Date().getTime();
82+
let expiry = localStorage.getItem(util.expiry);
83+
if (now >= expiry) {
84+
util.notify.control(obj);
85+
} else {
86+
let timer = expiry - now;
87+
vueContext.commit("setIsLogin", true);
88+
vueContext.dispatch("setTimeOutTimerExpiry", timer)
89+
}
6790
} else {
68-
vueContext.commit("setIsLogin", false)
91+
util.notify.control(obj);
6992
}
7093
};
94+

src/store/getters.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ export const getTradeResult = (state) => {
55
balance : state.balance
66
}
77
};
8+
9+
export const getIsLogin = (state) =>{
10+
return state.isLogin;
11+
}

0 commit comments

Comments
 (0)