mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2024-12-20 19:06:06 +01:00
Fix dynamic route requests, add auth middleware
This commit is contained in:
parent
7ef977b783
commit
db2f2d6660
@ -10,3 +10,4 @@ npm-debug.log
|
|||||||
dev.js
|
dev.js
|
||||||
/test/
|
/test/
|
||||||
/client/.nuxt/
|
/client/.nuxt/
|
||||||
|
/client/dist/
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -7,3 +7,4 @@ node_modules/
|
|||||||
/metadata/
|
/metadata/
|
||||||
/test/
|
/test/
|
||||||
/client/.nuxt/
|
/client/.nuxt/
|
||||||
|
/client/dist/
|
@ -10,6 +10,7 @@
|
|||||||
|
|
||||||
<script>
|
<script>
|
||||||
export default {
|
export default {
|
||||||
|
middleware: 'authenticated',
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
socket: null
|
socket: null
|
||||||
@ -140,11 +141,6 @@ export default {
|
|||||||
this.socket.on('scan_progress', this.scanProgress)
|
this.socket.on('scan_progress', this.scanProgress)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
beforeMount() {
|
|
||||||
if (!this.$store.state.user.user) {
|
|
||||||
this.$router.replace(`/login?redirect=${this.$route.path}`)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
mounted() {
|
mounted() {
|
||||||
this.initializeSocket()
|
this.initializeSocket()
|
||||||
}
|
}
|
||||||
|
6
client/middleware/authenticated.js
Normal file
6
client/middleware/authenticated.js
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
export default function ({ store, redirect, route }) {
|
||||||
|
// If the user is not authenticated
|
||||||
|
if (!store.state.user.user) {
|
||||||
|
return redirect(`/login?redirect=${route.path}`)
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "audiobookshelf-client",
|
"name": "audiobookshelf-client",
|
||||||
"version": "0.9.7-beta",
|
"version": "0.9.71-beta",
|
||||||
"description": "Audiobook manager and player",
|
"description": "Audiobook manager and player",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
@ -34,16 +34,11 @@ export default {
|
|||||||
watch: {
|
watch: {
|
||||||
user(newVal) {
|
user(newVal) {
|
||||||
if (newVal) {
|
if (newVal) {
|
||||||
// if (process.env.NODE_ENV !== 'production') {
|
|
||||||
if (this.$route.query.redirect) {
|
if (this.$route.query.redirect) {
|
||||||
this.$router.replace(this.$route.query.redirect)
|
this.$router.replace(this.$route.query.redirect)
|
||||||
} else {
|
} else {
|
||||||
this.$router.replace('/')
|
this.$router.replace('/')
|
||||||
}
|
}
|
||||||
|
|
||||||
// } else {
|
|
||||||
// window.location.reload()
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -56,7 +51,7 @@ export default {
|
|||||||
async submitForm() {
|
async submitForm() {
|
||||||
this.error = null
|
this.error = null
|
||||||
this.processing = true
|
this.processing = true
|
||||||
// var uri = `${process.env.serverUrl}/auth`
|
|
||||||
var payload = {
|
var payload = {
|
||||||
username: this.username,
|
username: this.username,
|
||||||
password: this.password || ''
|
password: this.password || ''
|
||||||
|
@ -39,7 +39,11 @@ export const getters = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const actions = {
|
export const actions = {
|
||||||
load({ commit }) {
|
load({ commit, rootState }) {
|
||||||
|
if (!rootState.user || !rootState.user.user) {
|
||||||
|
console.error('audiobooks/load - User not set')
|
||||||
|
return
|
||||||
|
}
|
||||||
this.$axios
|
this.$axios
|
||||||
.$get(`/api/audiobooks`)
|
.$get(`/api/audiobooks`)
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
|
@ -48,10 +48,12 @@ export const actions = {
|
|||||||
export const mutations = {
|
export const mutations = {
|
||||||
setUser(state, user) {
|
setUser(state, user) {
|
||||||
state.user = user
|
state.user = user
|
||||||
if (user && user.token) {
|
if (user) {
|
||||||
localStorage.setItem('token', user.token)
|
if (user.token) localStorage.setItem('token', user.token)
|
||||||
} else if (user) {
|
console.log('setUser', user.username)
|
||||||
|
} else {
|
||||||
localStorage.removeItem('token')
|
localStorage.removeItem('token')
|
||||||
|
console.warn('setUser cleared')
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
setSettings(state, settings) {
|
setSettings(state, settings) {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "audiobookshelf",
|
"name": "audiobookshelf",
|
||||||
"version": "0.9.7-beta",
|
"version": "0.9.71-beta",
|
||||||
"description": "Self-hosted audiobook server for managing and playing audiobooks.",
|
"description": "Self-hosted audiobook server for managing and playing audiobooks.",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
@ -42,7 +42,7 @@ class Auth {
|
|||||||
const authHeader = req.headers['authorization']
|
const authHeader = req.headers['authorization']
|
||||||
const token = authHeader && authHeader.split(' ')[1]
|
const token = authHeader && authHeader.split(' ')[1]
|
||||||
if (token == null) {
|
if (token == null) {
|
||||||
Logger.error('Api called without a token')
|
Logger.error('Api called without a token', req.path)
|
||||||
return res.sendStatus(401)
|
return res.sendStatus(401)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,8 +106,8 @@ class Server {
|
|||||||
app.use(this.auth.cors)
|
app.use(this.auth.cors)
|
||||||
|
|
||||||
// Static path to generated nuxt
|
// Static path to generated nuxt
|
||||||
|
const distPath = Path.join(global.appRoot, '/client/dist')
|
||||||
if (process.env.NODE_ENV === 'production') {
|
if (process.env.NODE_ENV === 'production') {
|
||||||
const distPath = Path.join(global.appRoot, '/client/dist')
|
|
||||||
app.use(express.static(distPath))
|
app.use(express.static(distPath))
|
||||||
app.use('/local', express.static(this.AudiobookPath))
|
app.use('/local', express.static(this.AudiobookPath))
|
||||||
} else {
|
} else {
|
||||||
@ -119,14 +119,13 @@ class Server {
|
|||||||
app.use(express.urlencoded({ extended: true }));
|
app.use(express.urlencoded({ extended: true }));
|
||||||
app.use(express.json())
|
app.use(express.json())
|
||||||
|
|
||||||
|
// Dynamic routes are not generated on client
|
||||||
|
app.get('/audiobook/:id', (req, res) => res.sendFile(Path.join(distPath, 'index.html')))
|
||||||
|
|
||||||
app.use('/api', this.authMiddleware.bind(this), this.apiController.router)
|
app.use('/api', this.authMiddleware.bind(this), this.apiController.router)
|
||||||
app.use('/hls', this.authMiddleware.bind(this), this.hlsController.router)
|
app.use('/hls', this.authMiddleware.bind(this), this.hlsController.router)
|
||||||
app.use('/feeds', this.rssFeeds.router)
|
app.use('/feeds', this.rssFeeds.router)
|
||||||
|
|
||||||
app.get('/', (req, res) => {
|
|
||||||
res.sendFile('/index.html')
|
|
||||||
})
|
|
||||||
|
|
||||||
app.post('/login', (req, res) => this.auth.login(req, res))
|
app.post('/login', (req, res) => this.auth.login(req, res))
|
||||||
app.post('/logout', this.logout.bind(this))
|
app.post('/logout', this.logout.bind(this))
|
||||||
app.get('/ping', (req, res) => {
|
app.get('/ping', (req, res) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user