implement remember me

This commit is contained in:
LouisLam 2021-07-01 22:16:02 +08:00
parent 55bd6b6d7a
commit d4a651e211
2 changed files with 19 additions and 7 deletions

View File

@ -17,7 +17,7 @@
<div class="form-check mb-3 mt-3" > <div class="form-check mb-3 mt-3" >
<label> <label>
<input type="checkbox" value="remember-me" class="form-check-input" id="remember" v-model="remember"> <input type="checkbox" value="remember-me" class="form-check-input" id="remember" v-model="$root.remember">
<label class="form-check-label" for="remember"> <label class="form-check-label" for="remember">
Remember me Remember me
@ -41,7 +41,7 @@ export default {
processing: false, processing: false,
username: "", username: "",
password: "", password: "",
remember: true,
res: null, res: null,
} }
}, },

View File

@ -3,7 +3,6 @@ import { useToast } from 'vue-toastification'
import dayjs from "dayjs"; import dayjs from "dayjs";
const toast = useToast() const toast = useToast()
let storage = localStorage;
let socket; let socket;
export default { export default {
@ -16,6 +15,7 @@ export default {
connected: false, connected: false,
connectCount: 0, connectCount: 0,
}, },
remember: (localStorage.remember !== "0"),
userTimezone: localStorage.timezone || "auto", userTimezone: localStorage.timezone || "auto",
allowLoginDialog: false, // Allowed to show login dialog, but "loggedIn" have to be true too. This exists because prevent the login dialog show 0.1s in first before the socket server auth-ed. allowLoginDialog: false, // Allowed to show login dialog, but "loggedIn" have to be true too. This exists because prevent the login dialog show 0.1s in first before the socket server auth-ed.
loggedIn: false, loggedIn: false,
@ -107,8 +107,8 @@ export default {
this.clearData() this.clearData()
} }
if (storage.token) { if (this.storage().token) {
this.loginByToken(storage.token) this.loginByToken(this.storage().token)
} else { } else {
this.allowLoginDialog = true; this.allowLoginDialog = true;
} }
@ -120,6 +120,10 @@ export default {
methods: { methods: {
storage() {
return (this.remember) ? localStorage : sessionStorage;
},
getSocket() { getSocket() {
return socket; return socket;
}, },
@ -139,7 +143,7 @@ export default {
}, (res) => { }, (res) => {
if (res.ok) { if (res.ok) {
storage.token = res.token; this.storage().token = res.token;
this.socket.token = res.token; this.socket.token = res.token;
this.loggedIn = true; this.loggedIn = true;
@ -164,7 +168,7 @@ export default {
}, },
logout() { logout() {
storage.removeItem("token"); this.storage().removeItem("token");
this.socket.token = null; this.socket.token = null;
this.loggedIn = false; this.loggedIn = false;
@ -240,6 +244,14 @@ export default {
return result; return result;
} }
},
watch: {
remember() {
localStorage.remember = (this.remember) ? "1" : "0"
}
} }
} }