Middleware authConfig is preventing .mp3 files to play
Answered
Spotted Rail posted this in #help-forum
Spotted RailOP
My /app/authConfig.js
export const authConfig = {
providers: [],
pages: {
signIn: "/signin"
},
secret: process.env.AUTH_SECRET,
callbacks: {
authorized({ auth, request }) {
const isLoggedIn = !!auth?.user;
const isOnDashboard = request.nextUrl.pathname.startsWith("/dashboard");
if (isOnDashboard) {
if (isLoggedIn) return true;
return false;
} else if (isLoggedIn) {
return Response.redirect(new URL("/dashboard", request.nextUrl))
}
return true;
}
}
}Answered by B33fb0n3
exclude the public folder from your middleware, because the public folder is public
9 Replies
Spotted RailOP
/middleware.js
import NextAuth from 'next-auth';
import { authConfig } from './app/authConfig';
export default NextAuth(authConfig).auth;
export const config = {
matcher: ["/((?!api|_next/static|_next/image|.*\\.png$).*)"],
};@Spotted Rail My /app/authConfig.js
js
export const authConfig = {
providers: [],
pages: {
signIn: "/signin"
},
secret: process.env.AUTH_SECRET,
callbacks: {
authorized({ auth, request }) {
const isLoggedIn = !!auth?.user;
const isOnDashboard = request.nextUrl.pathname.startsWith("/dashboard");
if (isOnDashboard) {
if (isLoggedIn) return true;
return false;
} else if (isLoggedIn) {
return Response.redirect(new URL("/dashboard", request.nextUrl))
}
return true;
}
}
}
Spotted RailOP
I am playing sounds on user click, when I comment out the callbacks in this config, the audio plays fine, otherwise it won't play. Not sure why it's behaving this way.
Spotted RailOP
I am loading .mp3 files from public/sounds/
@Spotted Rail /middleware.js
js
import NextAuth from 'next-auth';
import { authConfig } from './app/authConfig';
export default NextAuth(authConfig).auth;
export const config = {
matcher: ["/((?!api|_next/static|_next/image|.*\\.png$).*)"],
};
exclude the public folder from your middleware, because the public folder is public
Answer
@B33fb0n3 exclude the public folder from your middleware, because the public folder is public
Spotted RailOP
oh the matcher was causing the issue
exactly. Happy to help
@B33fb0n3 exclude the public folder from your middleware, because the public folder is public
Polar bear
How to exclude it?
This excludes the public folder
export const config = {
matcher: "/((?!api|static|.*\\..*|_next).*)",
};