Refactor authentication handling and update loading components

The authentication handling logic in the middleware.ts file was refactored to improve clarity and control flow. In addition, the loading component, previously located in the main app directory, has been deleted and recreated specifically for admin use. The list of private path prefixes has been updated in the use-auth-change-listener.ts file to reflect these changes.
This commit is contained in:
giancarlo
2024-04-28 21:09:12 +07:00
parent 1009685efd
commit b65e1dacc7
4 changed files with 11 additions and 10 deletions

View File

@@ -0,0 +1,3 @@
import { GlobalLoader } from '@kit/ui/global-loader';
export default GlobalLoader;

View File

@@ -1,5 +0,0 @@
import { GlobalLoader } from '@kit/ui/global-loader';
export default function Loading() {
return <GlobalLoader displaySpinner={false} />;
}

View File

@@ -135,12 +135,15 @@ function getPatterns() {
},
{
pattern: new URLPattern({ pathname: '/auth*' }),
handler: async (req: NextRequest, res: NextResponse) => {
handler: async (
req: NextRequest,
res: NextResponse,
userResponse: UserResponse,
) => {
const supabase = createMiddlewareClient(req, res);
const { data: user, error } = await supabase.auth.getUser();
// the user is logged out, so we don't need to do anything
if (error) {
if (!userResponse.data) {
await supabase.auth.signOut();
return;
@@ -151,7 +154,7 @@ function getPatterns() {
// If user is logged in and does not need to verify MFA,
// redirect to home page.
if (user && !isVerifyMfa) {
if (!isVerifyMfa) {
return NextResponse.redirect(
new URL(pathsConfig.app.home, req.nextUrl.origin).href,
);

View File

@@ -11,7 +11,7 @@ import { useRevalidateUserSession, useUserSession } from './use-user-session';
* @name PRIVATE_PATH_PREFIXES
* @description A list of private path prefixes
*/
const PRIVATE_PATH_PREFIXES = ['/home', '/admin', '/update-password'];
const PRIVATE_PATH_PREFIXES = ['/home', '/admin', '/join', '/update-password'];
/**
* @name useAuthChangeListener