Refactor middleware to decompose user data object
This commit modifies the middleware in web application to decompose the user object from the returned data of the getUser function. This is a simplification of the code, eliminating unnecessary references to the "data" object when accessing the user's details and role. It's carried out in all places where the getUser function is called and user data is processed.
This commit is contained in:
@@ -96,17 +96,20 @@ async function adminMiddleware(request: NextRequest, response: NextResponse) {
|
|||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { data, error } = await getUser(request, response);
|
const {
|
||||||
|
data: { user },
|
||||||
|
error,
|
||||||
|
} = await getUser(request, response);
|
||||||
|
|
||||||
// If user is not logged in, redirect to sign in page.
|
// If user is not logged in, redirect to sign in page.
|
||||||
// This should never happen, but just in case.
|
// This should never happen, but just in case.
|
||||||
if (!data.user || error) {
|
if (!user || error) {
|
||||||
return NextResponse.redirect(
|
return NextResponse.redirect(
|
||||||
new URL(pathsConfig.auth.signIn, request.nextUrl.origin).href,
|
new URL(pathsConfig.auth.signIn, request.nextUrl.origin).href,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const role = data.user?.app_metadata.role;
|
const role = user?.app_metadata.role;
|
||||||
|
|
||||||
// If user is not an admin, redirect to 404 page.
|
// If user is not an admin, redirect to 404 page.
|
||||||
if (!role || role !== 'super-admin') {
|
if (!role || role !== 'super-admin') {
|
||||||
@@ -129,7 +132,9 @@ function getPatterns() {
|
|||||||
{
|
{
|
||||||
pattern: new URLPattern({ pathname: '/auth*' }),
|
pattern: new URLPattern({ pathname: '/auth*' }),
|
||||||
handler: async (req: NextRequest, res: NextResponse) => {
|
handler: async (req: NextRequest, res: NextResponse) => {
|
||||||
const { data: user } = await getUser(req, res);
|
const {
|
||||||
|
data: { user },
|
||||||
|
} = await getUser(req, res);
|
||||||
|
|
||||||
// the user is logged out, so we don't need to do anything
|
// the user is logged out, so we don't need to do anything
|
||||||
if (!user) {
|
if (!user) {
|
||||||
@@ -151,7 +156,9 @@ function getPatterns() {
|
|||||||
{
|
{
|
||||||
pattern: new URLPattern({ pathname: '/home*' }),
|
pattern: new URLPattern({ pathname: '/home*' }),
|
||||||
handler: async (req: NextRequest, res: NextResponse) => {
|
handler: async (req: NextRequest, res: NextResponse) => {
|
||||||
const { data: user } = await getUser(req, res);
|
const {
|
||||||
|
data: { user },
|
||||||
|
} = await getUser(req, res);
|
||||||
|
|
||||||
const origin = req.nextUrl.origin;
|
const origin = req.nextUrl.origin;
|
||||||
const next = req.nextUrl.pathname;
|
const next = req.nextUrl.pathname;
|
||||||
|
|||||||
Reference in New Issue
Block a user