Skip to content

Commit

Permalink
added check for mobile device
Browse files Browse the repository at this point in the history
  • Loading branch information
nimit9 committed Jun 17, 2024
1 parent 2541797 commit 79e8f99
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
16 changes: 15 additions & 1 deletion src/lib/middleware-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { importJWK, jwtVerify } from 'jose';
import { NextRequestWithAuth, withAuth } from 'next-auth/middleware';
import { NextResponse } from 'next/server';
import { NextRequest, NextResponse, userAgent } from 'next/server';

const PRIVATE_MOBILE_ROUTES = ['/api/auth/mobile/logout'];
const SINGLE_USER_ROUTES = ['/courses', '/questions', '/bookmarks'];
Expand Down Expand Up @@ -52,3 +52,17 @@ export const nextAuthMiddleware = withAuth(async (req) => {
}
}
});

export const getDeviceType = (request: NextRequest) => {
const { device } = userAgent(request);
return device.type;
};

export const isMobile = (request: NextRequest) => {
const deviceType = getDeviceType(request);
return deviceType === 'mobile' || deviceType === 'tablet';
};

export const isDesktop = (request: NextRequest) => {
return !isMobile(request);
};
10 changes: 8 additions & 2 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { NextRequestWithAuth } from 'next-auth/middleware';
import { NextResponse } from 'next/server';
import { handleMobileAuth, nextAuthMiddleware } from './lib/middleware-utils';
import {
handleMobileAuth,
isMobile,
nextAuthMiddleware,
} from './lib/middleware-utils';

const PUBLIC_ROUTES = [
'/',
Expand All @@ -20,7 +24,9 @@ export async function middleware(request: NextRequestWithAuth) {
}

// Mobile Auth
await handleMobileAuth(request);
if (isMobile(request)) {
return await handleMobileAuth(request);
}

/*
NextJS Auth - Other public routes
Expand Down

0 comments on commit 79e8f99

Please sign in to comment.