File tree Expand file tree Collapse file tree 2 files changed +28
-11
lines changed Expand file tree Collapse file tree 2 files changed +28
-11
lines changed Original file line number Diff line number Diff line change @@ -16,10 +16,18 @@ import { useSession } from 'next-auth/react';
16
16
import { useState } from 'react' ;
17
17
18
18
export default function UserDropdown ( ) {
19
+ const [ isLoggingOut , setIsLoggingOut ] = useState ( false ) ;
19
20
const { data : session } = useSession ( ) ;
20
21
const { username, name, email, image } = session ?. user ?? { } ;
21
22
22
- const [ isLoggingOut , setIsLoggingOut ] = useState ( false ) ;
23
+ async function handleLogout ( ) {
24
+ try {
25
+ setIsLoggingOut ( true ) ;
26
+ await signOutAction ( { redirectTo : '/login' } ) ;
27
+ } finally {
28
+ setIsLoggingOut ( false ) ;
29
+ }
30
+ }
23
31
24
32
return (
25
33
< DropdownMenu >
@@ -45,14 +53,7 @@ export default function UserDropdown() {
45
53
</ DropdownMenuShortcut >
46
54
</ DropdownMenuItem >
47
55
< DropdownMenuSeparator />
48
- < DropdownMenuItem
49
- className = "cursor-pointer"
50
- disabled = { isLoggingOut }
51
- onClick = { async ( ) => {
52
- setIsLoggingOut ( true ) ;
53
- await signOutAction ( { redirectTo : '/login' } ) ;
54
- } }
55
- >
56
+ < DropdownMenuItem className = "cursor-pointer" onClick = { handleLogout } disabled = { isLoggingOut } >
56
57
Log out
57
58
< DropdownMenuShortcut >
58
59
{ isLoggingOut ? (
Original file line number Diff line number Diff line change 1
- export { auth as middleware } from '@/auth' ;
1
+ import { auth } from '@/auth' ;
2
+
3
+ const PROTECTED_ROUTES = [
4
+ { path : '/new' , exact : true } ,
5
+ { path : '/@' , exact : false } ,
6
+ ] ;
7
+
8
+ export default auth ( ( req ) => {
9
+ const { pathname } = req . nextUrl ;
10
+ const isProtected = PROTECTED_ROUTES . some ( ( { path, exact } ) =>
11
+ exact ? pathname === path : pathname . startsWith ( path ) ,
12
+ ) ;
13
+
14
+ if ( ! req . auth && isProtected ) {
15
+ const loginUrl = new URL ( '/login' , req . url ) ;
16
+ return Response . redirect ( loginUrl ) ;
17
+ }
18
+ } ) ;
2
19
3
- // optionally, don't invoke middleware on some paths
4
20
export const config = {
5
21
matcher : [ '/((?!api|_next/static|_next/image|favicon.ico).*)' ] ,
6
22
} ;
You can’t perform that action at this time.
0 commit comments