0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

速習App Router#7 ミドルウェア

Last updated at Posted at 2024-02-27

はじめに

この記事は下記の講座で学んだ内容をまとめています。
理解が怪しい所があるので教えてくれるととても嬉しいです。

目次

1.速習App Router#1
2.速習App Router#2
3.速習App Router#3
4.速習App Router#4
5.速習App Router#5
6.速習App Router#6
7.速習App Router#7 <-この記事

ミドルウェアについて

ブラウザからのアクセスがpage.tsxに辿り付くまでに一旦経由される部分のこと。

/middleware.ts
export async function middleware(req: NextRequest) {
    const res = NextResponse.next()
    const supabase = createMiddlewareSupabaseClient({ req, res })

    const { data: { session }} = await supabase.auth.getSession()
    
    if (!session && req.nextUrl.pathname.startsWith('/auth/todo-crud')) {
        const redirectUrl = req.nextUrl.clone()
        redirectUrl.pathname = '/auth'
        return NextResponse.redirect(redirectUrl)
    }
    
    return res
}

これは /auth/todo-crudにアクセスした時にログインしているかどうかによって /auth/todo-crudに通すか決めるミドルウェアです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?