Authorization is now a cookie so removed localStorage code
Update some structure in how we request and save settings. Updated Settings & Profile to reflect these changes.
This commit is contained in:
@@ -43,7 +43,7 @@ import Settings from "@/components/Settings";
|
|||||||
import Activity from "@/components/ActivityPage";
|
import Activity from "@/components/ActivityPage";
|
||||||
import SeasonedButton from "@/components/ui/SeasonedButton";
|
import SeasonedButton from "@/components/ui/SeasonedButton";
|
||||||
|
|
||||||
import { getEmoji, getUserRequests, getSettings } from "@/api";
|
import { getEmoji, getUserRequests, getSettings, logout } from "@/api";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
components: { ListHeader, ResultsList, Settings, Activity, SeasonedButton },
|
components: { ListHeader, ResultsList, Settings, Activity, SeasonedButton },
|
||||||
@@ -67,7 +67,7 @@ export default {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
...mapActions("user", ["logout", "updateSettings"]),
|
...mapActions("user", ["logout", "setSettings"]),
|
||||||
toggleSettings() {
|
toggleSettings() {
|
||||||
this.showSettings = this.showSettings ? false : true;
|
this.showSettings = this.showSettings ? false : true;
|
||||||
|
|
||||||
@@ -98,15 +98,17 @@ export default {
|
|||||||
this.updateQueryParams("activity", this.showActivity);
|
this.updateQueryParams("activity", this.showActivity);
|
||||||
},
|
},
|
||||||
_logout() {
|
_logout() {
|
||||||
|
logout().then(() => {
|
||||||
this.logout();
|
this.logout();
|
||||||
this.$router.push("home");
|
this.$router.push("home");
|
||||||
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
created() {
|
created() {
|
||||||
if (!this.settings) {
|
if (!this.settings) {
|
||||||
getSettings().then(resp => {
|
getSettings().then(resp => {
|
||||||
const { settings } = resp;
|
const { settings } = resp;
|
||||||
if (settings) updateSettings(settings);
|
if (settings) this.setSettings(settings);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,16 +13,16 @@
|
|||||||
<form class="form">
|
<form class="form">
|
||||||
<seasoned-input
|
<seasoned-input
|
||||||
placeholder="plex username"
|
placeholder="plex username"
|
||||||
icon="Email"
|
type="email"
|
||||||
:value.sync="plexUsername"
|
:value.sync="plexUsername"
|
||||||
/>
|
/>
|
||||||
<seasoned-input
|
<seasoned-input
|
||||||
placeholder="plex password"
|
placeholder="plex password"
|
||||||
icon="Keyhole"
|
|
||||||
type="password"
|
type="password"
|
||||||
:value.sync="plexPassword"
|
:value.sync="plexPassword"
|
||||||
@submit="authenticatePlex"
|
@enter="authenticatePlex"
|
||||||
/>
|
>
|
||||||
|
</seasoned-input>
|
||||||
|
|
||||||
<seasoned-button @click="authenticatePlex"
|
<seasoned-button @click="authenticatePlex"
|
||||||
>link plex account</seasoned-button
|
>link plex account</seasoned-button
|
||||||
@@ -80,7 +80,7 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { mapGetters, mapState, mapActions } from "vuex";
|
import { mapGetters, mapActions } from "vuex";
|
||||||
import storage from "@/storage";
|
import storage from "@/storage";
|
||||||
import SeasonedInput from "@/components/ui/SeasonedInput";
|
import SeasonedInput from "@/components/ui/SeasonedInput";
|
||||||
import SeasonedButton from "@/components/ui/SeasonedButton";
|
import SeasonedButton from "@/components/ui/SeasonedButton";
|
||||||
@@ -104,18 +104,18 @@ export default {
|
|||||||
...mapGetters("user", ["loggedIn", "plexId", "settings"])
|
...mapGetters("user", ["loggedIn", "plexId", "settings"])
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
...mapActions("user", ["login", "updateSettings"]),
|
...mapActions("user", ["setSettings"]),
|
||||||
changePassword() {
|
changePassword() {
|
||||||
return;
|
return;
|
||||||
},
|
},
|
||||||
created() {
|
created() {
|
||||||
if (!this.settings) {
|
if (!this.settings) this.reloadSettings();
|
||||||
console.log("settings does not exists.", this.settings);
|
},
|
||||||
getSettings().then(resp => {
|
reloadSettings() {
|
||||||
const { settings } = resp;
|
return getSettings().then(response => {
|
||||||
if (settings) updateSettings(settings);
|
const { settings } = response;
|
||||||
|
if (settings) this.setSettings(settings);
|
||||||
});
|
});
|
||||||
}
|
|
||||||
},
|
},
|
||||||
async authenticatePlex() {
|
async authenticatePlex() {
|
||||||
let username = this.plexUsername;
|
let username = this.plexUsername;
|
||||||
@@ -123,6 +123,12 @@ export default {
|
|||||||
|
|
||||||
const { success, message } = await linkPlexAccount(username, password);
|
const { success, message } = await linkPlexAccount(username, password);
|
||||||
|
|
||||||
|
if (success) {
|
||||||
|
this.reloadSettings();
|
||||||
|
this.plexUsername = "";
|
||||||
|
this.plexPassword = "";
|
||||||
|
}
|
||||||
|
|
||||||
this.messages.push({
|
this.messages.push({
|
||||||
type: success ? "success" : "error",
|
type: success ? "success" : "error",
|
||||||
title: success ? "Authenticated with plex" : "Something went wrong",
|
title: success ? "Authenticated with plex" : "Something went wrong",
|
||||||
@@ -132,6 +138,8 @@ export default {
|
|||||||
async unauthenticatePlex() {
|
async unauthenticatePlex() {
|
||||||
const response = await unlinkPlexAccount();
|
const response = await unlinkPlexAccount();
|
||||||
|
|
||||||
|
if (response.success) this.reloadSettings();
|
||||||
|
|
||||||
this.messages.push({
|
this.messages.push({
|
||||||
type: response.success ? "success" : "error",
|
type: response.success ? "success" : "error",
|
||||||
title: response.success
|
title: response.success
|
||||||
@@ -154,8 +162,14 @@ a {
|
|||||||
|
|
||||||
// DUPLICATE CODE
|
// DUPLICATE CODE
|
||||||
.form {
|
.form {
|
||||||
> div:last-child {
|
> div,
|
||||||
margin-top: 1rem;
|
input,
|
||||||
|
button {
|
||||||
|
margin-bottom: 1rem;
|
||||||
|
|
||||||
|
&:last-child {
|
||||||
|
margin-bottom: 0px;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
&__group {
|
&__group {
|
||||||
|
|||||||
@@ -1,28 +1,51 @@
|
|||||||
import { refreshToken } from "@/api";
|
import { refreshToken } from "@/api";
|
||||||
import { parseJwt } from "@/utils";
|
import { parseJwt } from "@/utils";
|
||||||
|
|
||||||
function setLocalStorageByKey(key, value) {
|
function getCookie(name) {
|
||||||
if (value instanceof Object || value instanceof Array) {
|
console.debug("getting cookie with name:", name);
|
||||||
value = JSON.stringify(value);
|
|
||||||
}
|
var arrayb = document.cookie.split(";");
|
||||||
const buff = Buffer.from(value);
|
for (const item of arrayb) {
|
||||||
const encodedValue = buff.toString("base64");
|
const query = `${name}=`;
|
||||||
localStorage.setItem(key, encodedValue);
|
|
||||||
|
if (!item.startsWith(query)) continue;
|
||||||
|
console.debug("found from cookies:", item);
|
||||||
|
return item.substr(query.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getLocalStorageByKey(key) {
|
console.debug("no token found");
|
||||||
const encodedValue = localStorage.getItem(key);
|
|
||||||
if (encodedValue == null) {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
const buff = new Buffer(encodedValue, "base64");
|
|
||||||
const value = buff.toString("utf-8");
|
|
||||||
|
|
||||||
try {
|
function setCookie(name, value, options = {}) {
|
||||||
return JSON.parse(value);
|
options = {
|
||||||
} catch {
|
path: "/",
|
||||||
return value;
|
// add other defaults here if necessary
|
||||||
|
...options
|
||||||
|
};
|
||||||
|
|
||||||
|
if (options.expires instanceof Date) {
|
||||||
|
options.expires = options.expires.toUTCString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let updatedCookie =
|
||||||
|
encodeURIComponent(name) + "=" + encodeURIComponent(value);
|
||||||
|
|
||||||
|
for (let optionKey in options) {
|
||||||
|
updatedCookie += "; " + optionKey;
|
||||||
|
let optionValue = options[optionKey];
|
||||||
|
if (optionValue !== true) {
|
||||||
|
updatedCookie += "=" + optionValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
document.cookie = updatedCookie;
|
||||||
|
}
|
||||||
|
|
||||||
|
function deleteCookie(name) {
|
||||||
|
setCookie(name, "", {
|
||||||
|
"max-age": Date.now()
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
@@ -38,7 +61,7 @@ export default {
|
|||||||
settings: state => state.settings,
|
settings: state => state.settings,
|
||||||
token: state => state.token,
|
token: state => state.token,
|
||||||
loggedIn: state => state && state.username !== null,
|
loggedIn: state => state && state.username !== null,
|
||||||
admin: state => state && state.admin !== null,
|
admin: state => state.admin,
|
||||||
plexId: state => {
|
plexId: state => {
|
||||||
if (state && state.settings && state.settings.plex_userid)
|
if (state && state.settings && state.settings.plex_userid)
|
||||||
return state.settings.plex_userid;
|
return state.settings.plex_userid;
|
||||||
@@ -55,32 +78,40 @@ export default {
|
|||||||
state.username = null;
|
state.username = null;
|
||||||
state.settings = null;
|
state.settings = null;
|
||||||
state.admin = false;
|
state.admin = false;
|
||||||
localStorage.removeItem("token");
|
// deleteCookie('authorization');
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
actions: {
|
actions: {
|
||||||
initFromLocalStorage: async ({ dispatch }) => {
|
initUserFromCookie: async ({ dispatch }) => {
|
||||||
const token = getLocalStorageByKey("token");
|
const jwtToken = getCookie("authorization");
|
||||||
if (token) await dispatch("setupStateFromToken", token);
|
if (!jwtToken) return null;
|
||||||
|
|
||||||
const settings = getLocalStorageByKey("settings");
|
const token = parseJwt(jwtToken);
|
||||||
if (settings) await dispatch("setSettings", settings);
|
console.debug("has token: ", token);
|
||||||
|
return await dispatch("setupStateFromToken", token);
|
||||||
},
|
},
|
||||||
setupStateFromToken: ({ commit }, token) => {
|
setupStateFromToken: ({ commit }, token) => {
|
||||||
try {
|
try {
|
||||||
const jwtData = parseJwt(token);
|
const { username, admin, settings } = token;
|
||||||
const { username, admin } = jwtData;
|
|
||||||
|
|
||||||
if (!username) {
|
if (!username) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.debug("setting:", {
|
||||||
|
username,
|
||||||
|
admin: admin != undefined,
|
||||||
|
settings,
|
||||||
|
token
|
||||||
|
});
|
||||||
|
|
||||||
commit("SET_TOKEN", token);
|
commit("SET_TOKEN", token);
|
||||||
commit("SET_USERNAME", username);
|
commit("SET_USERNAME", username);
|
||||||
commit("SET_ADMIN", admin != undefined ? true : false);
|
commit("SET_SETTINGS", settings);
|
||||||
|
commit("SET_ADMIN", admin != undefined);
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log("Unable to parse JWT, failed with error:", error);
|
console.error("Unable to parse JWT, failed with error:", error);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -90,14 +121,8 @@ export default {
|
|||||||
}
|
}
|
||||||
|
|
||||||
commit("SET_SETTINGS", settings);
|
commit("SET_SETTINGS", settings);
|
||||||
setLocalStorageByKey("settings", settings);
|
|
||||||
},
|
},
|
||||||
logout: ({ commit }) => commit("LOGOUT"),
|
logout: ({ commit }) => commit("LOGOUT"),
|
||||||
login: async ({ dispatch }, token) => {
|
login: async ({ dispatch }) => await dispatch("initUserFromCookie")
|
||||||
const loggedIn = await dispatch("setupStateFromToken", token);
|
|
||||||
if (loggedIn) setLocalStorageByKey("token", token);
|
|
||||||
|
|
||||||
return loggedIn;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user