add eslint

This commit is contained in:
Thomas Amland
2020-08-10 11:11:40 +02:00
parent 9f842bcffe
commit 8e0cc715ab
50 changed files with 2493 additions and 1059 deletions
+60 -61
View File
@@ -3,86 +3,85 @@
<b-modal size="sm" hide-header hide-footer no-close-on-esc :visible="showModal">
<form @submit.prevent="login">
<div style="font-size: 4rem; color: #fff;" class="text-center">
<Icon icon="person-circle"/>
<Icon icon="person-circle" />
</div>
<b-form-group label="Server">
<b-form-input name="server" type="text" v-model="server" :state="valid"/>
<b-form-input v-model="server" name="server" type="text" :state="valid" />
</b-form-group>
<b-form-group label="Username">
<b-form-input name="username" type="text" v-model="username" :state="valid"/>
<b-form-input v-model="username" name="username" type="text" :state="valid" />
</b-form-group>
<b-form-group label="Password">
<b-form-input name="password" type="password" v-model="password" :state="valid"/>
<b-form-input v-model="password" name="password" type="password" :state="valid" />
</b-form-group>
<b-alert :show="error != null" variant="danger">
<template v-if="error != null">
Could not log in. ({{ error.message }})
</template>
</b-alert>
<button class="btn btn-primary btn-block" @click="login" :disabled="busy">
<b-spinner v-show="busy" small type="grow"/> Log in
<button class="btn btn-primary btn-block" :disabled="busy" @click="login">
<b-spinner v-show="busy" small type="grow" /> Log in
</button>
</form>
</b-modal>
</div>
</template>>
<script lang="ts">
import Vue from "vue";
import { mapMutations, mapState } from "vuex";
import Vue from 'vue'
export default Vue.extend({
props: {
returnTo: { type: String, required: true },
},
data() {
return {
server: "",
username: "",
password: "",
rememberLogin: true,
busy: false,
error: null,
showModal: false,
};
},
async created() {
this.server = await this.$auth.server;
this.username = await this.$auth.username;
const success = await this.$auth.autoLogin();
if (success) {
this.$store.commit("setLoginSuccess", {
username: this.username,
server: this.server,
});
this.$router.replace(this.returnTo);
} else {
this.showModal = true;
}
},
computed: {
valid(): false | null {
return this.error == null ? null : false
}
},
methods: {
login() {
this.error = null;
this.busy = true;
this.$auth.loginWithPassword(this.server, this.username, this.password, this.rememberLogin)
.then(() => {
this.$store.commit("setLoginSuccess", {
username: this.username,
server: this.server,
});
this.$router.replace(this.returnTo);
export default Vue.extend({
props: {
returnTo: { type: String, required: true },
},
data() {
return {
server: '',
username: '',
password: '',
rememberLogin: true,
busy: false,
error: null,
showModal: false,
}
},
computed: {
valid(): false | null {
return this.error == null ? null : false
}
},
async created() {
this.server = await this.$auth.server
this.username = await this.$auth.username
const success = await this.$auth.autoLogin()
if (success) {
this.$store.commit('setLoginSuccess', {
username: this.username,
server: this.server,
})
.catch(err => {
this.error = err;
})
.finally(() => {
this.busy = false;
});
this.$router.replace(this.returnTo)
} else {
this.showModal = true
}
},
methods: {
login() {
this.error = null
this.busy = true
this.$auth.loginWithPassword(this.server, this.username, this.password, this.rememberLogin)
.then(() => {
this.$store.commit('setLoginSuccess', {
username: this.username,
server: this.server,
})
this.$router.replace(this.returnTo)
})
.catch(err => {
this.error = err
})
.finally(() => {
this.busy = false
})
}
}
}
});
})
</script>
+43 -38
View File
@@ -1,69 +1,74 @@
import axios from 'axios';
import { randomString, md5 } from '@/shared/utils';
import axios from 'axios'
import { randomString, md5 } from '@/shared/utils'
export class AuthService {
public server: string = "";
public username: string = "";
public salt: string = "";
public hash: string = "";
private authenticated: boolean = false;
public server = '';
public username = '';
public salt = '';
public hash = '';
private authenticated = false;
constructor() {
this.server = localStorage.getItem("server") || "/api";
this.username = localStorage.getItem("username") || "guest1";
this.salt = localStorage.getItem("salt") || "";
this.hash = localStorage.getItem("hash") || "";
this.server = localStorage.getItem('server') || '/api'
this.username = localStorage.getItem('username') || 'guest1'
this.salt = localStorage.getItem('salt') || ''
this.hash = localStorage.getItem('hash') || ''
}
private saveSession() {
localStorage.setItem("server", this.server);
localStorage.setItem("username", this.username);
localStorage.setItem("salt", this.salt);
localStorage.setItem("hash", this.hash);
localStorage.setItem('server', this.server)
localStorage.setItem('username', this.username)
localStorage.setItem('salt', this.salt)
localStorage.setItem('hash', this.hash)
}
async autoLogin(): Promise<boolean> {
if (!this.server || !this.username) {
return false;
return false
}
return this.loginWithHash(this.server, this.username, this.salt, this.hash, false)
.then(() => true)
.catch(() => false);
.catch(() => false)
}
async loginWithPassword(server: string, username: string, password: string, remember: boolean) {
const salt = randomString();
const hash = md5(password + salt);
return this.loginWithHash(server, username, salt, hash, remember);
const salt = randomString()
const hash = md5(password + salt)
return this.loginWithHash(server, username, salt, hash, remember)
}
private async loginWithHash(server: string, username: string, salt: string, hash: string, remember: boolean) {
return axios.get(`${server}/rest/ping.view?u=${username}&s=${salt}&t=${hash}&v=1.15.0&c=app&f=json`)
private async loginWithHash(
server: string,
username: string,
salt: string,
hash: string,
remember: boolean
) {
const url = `${server}/rest/ping.view?u=${username}&s=${salt}&t=${hash}&v=1.15.0&c=app&f=json`
return axios.get(url)
.then((response) => {
const subsonicResponse = response.data["subsonic-response"];
if (!subsonicResponse || subsonicResponse.status !== "ok") {
const err = new Error(subsonicResponse.status);
return Promise.reject(err);
const subsonicResponse = response.data['subsonic-response']
if (!subsonicResponse || subsonicResponse.status !== 'ok') {
const err = new Error(subsonicResponse.status)
return Promise.reject(err)
}
this.authenticated = true;
this.server = server;
this.username = username;
this.salt = salt;
this.hash = hash;
this.authenticated = true
this.server = server
this.username = username
this.salt = salt
this.hash = hash
if (remember) {
this.saveSession();
this.saveSession()
}
})
}
logout() {
localStorage.clear();
sessionStorage.clear();
localStorage.clear()
sessionStorage.clear()
}
isAuthenticated() {
return this.authenticated;
return this.authenticated
}
}
}