Initial commit
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
<template>
|
||||
<div>
|
||||
<b-modal title="Login" hide-footer no-close-on-esc :visible="showModal">
|
||||
<form @submit.prevent="login">
|
||||
<fieldset :disabled="busy">
|
||||
<b-form-group label="Server">
|
||||
<b-form-input name="server" type="text" v-model="server" required></b-form-input>
|
||||
</b-form-group>
|
||||
<b-form-group label="Username">
|
||||
<b-form-input name="username" type="text" v-model="username" required></b-form-input>
|
||||
</b-form-group>
|
||||
<b-form-group label="Password">
|
||||
<b-form-input name="password" type="password" v-model="password" required></b-form-input>
|
||||
</b-form-group>
|
||||
<b-form-group>
|
||||
<b-form-checkbox v-model="rememberLogin">Remember</b-form-checkbox>
|
||||
</b-form-group>
|
||||
<b-alert :show="!!error" variant="danger">
|
||||
Login failed.
|
||||
</b-alert>
|
||||
<button class="btn btn-primary" @click="login">
|
||||
<b-spinner v-show="busy" small type="grow"/>
|
||||
Login
|
||||
</button>
|
||||
</fieldset>
|
||||
</form>
|
||||
</b-modal>
|
||||
</div>
|
||||
</template>>
|
||||
<script lang="ts">
|
||||
import Vue from "vue";
|
||||
import { mapMutations, mapState } from "vuex";
|
||||
|
||||
export default Vue.extend({
|
||||
props: {
|
||||
returnTo: { type: String, required: true },
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
server: "",
|
||||
username: "",
|
||||
password: "",
|
||||
rememberLogin: false,
|
||||
busy: false,
|
||||
error: false,
|
||||
showModal: false,
|
||||
};
|
||||
},
|
||||
async created() {
|
||||
this.server = await this.$auth.server;
|
||||
this.username = await this.$auth.username;
|
||||
this.password = await this.$auth.password;
|
||||
const success = await this.$auth.autoLogin();
|
||||
if (success) {
|
||||
this.$store.commit("setLoginSuccess", { username: this.username});
|
||||
this.$router.push(this.returnTo);
|
||||
} else {
|
||||
this.showModal = true;
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
login() {
|
||||
this.busy = true;
|
||||
this.$auth.login(this.server, this.username, this.password, this.rememberLogin)
|
||||
.then(() => {
|
||||
this.$store.commit("setLoginSuccess", { username: this.username });
|
||||
this.$router.push(this.returnTo);
|
||||
})
|
||||
.catch(err => {
|
||||
this.error = true;
|
||||
})
|
||||
.finally(() => {
|
||||
this.busy = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
</script>
|
||||
@@ -0,0 +1,58 @@
|
||||
import axios from 'axios';
|
||||
|
||||
|
||||
export class AuthService {
|
||||
public server: string = "";
|
||||
public username: string = "";
|
||||
public password: string = "";
|
||||
private authenticated: boolean = false;
|
||||
|
||||
constructor() {
|
||||
this.server = localStorage.getItem("server") || "";
|
||||
this.username = localStorage.getItem("username") || "";
|
||||
this.password = localStorage.getItem("password") || "";
|
||||
}
|
||||
|
||||
private saveSession() {
|
||||
localStorage.setItem("server", this.server);
|
||||
localStorage.setItem("username", this.username);
|
||||
localStorage.setItem("password", this.password);
|
||||
}
|
||||
|
||||
async autoLogin(): Promise<boolean> {
|
||||
if (!this.server || !this.username) {
|
||||
return false;
|
||||
}
|
||||
return this.login(this.server, this.username, this.password, false)
|
||||
.then(() => true)
|
||||
.catch(() => false);
|
||||
}
|
||||
|
||||
async login(server: string, username: string, password: string, remember: boolean) {
|
||||
return axios.get(`${server}/rest/ping.view?u=${username}&p=${password}&v=1.15.0&c=app&f=json`)
|
||||
.then((response) => {
|
||||
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.password = password;
|
||||
if (remember) {
|
||||
this.saveSession();
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
logout() {
|
||||
localStorage.clear();
|
||||
sessionStorage.clear();
|
||||
}
|
||||
|
||||
isAuthenticated() {
|
||||
return this.authenticated;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user