-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support
cause
in Next.js error signals
- Loading branch information
Showing
15 changed files
with
275 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { redirect } from 'next/navigation' | ||
import { SessionExpiredError, verifyAuth } from './utils' | ||
|
||
export default async function Page() { | ||
try { | ||
await verifyAuth() | ||
} catch (err) { | ||
if (err.cause instanceof SessionExpiredError) { | ||
redirect('/') | ||
} | ||
|
||
throw err | ||
} | ||
|
||
return <p id="page">Cause Page</p> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { cookies } from 'next/headers' | ||
import { forbidden } from 'next/navigation' | ||
|
||
export class AuthError extends Error { | ||
constructor(message) { | ||
super(message) | ||
this.name = 'AuthError' | ||
} | ||
} | ||
|
||
export class SessionExpiredError extends Error { | ||
constructor(message) { | ||
super(message) | ||
this.name = 'AuthError' | ||
} | ||
} | ||
|
||
export async function verifyAuth() { | ||
const cookieStore = await cookies() | ||
const token = cookieStore.get('token') | ||
|
||
if (!token) { | ||
forbidden(new AuthError('No token found')) | ||
} | ||
|
||
if (token.value === 'expired') { | ||
forbidden(new SessionExpiredError('Session expired')) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { redirect } from 'next/navigation' | ||
import { SessionExpiredError, verifyAuth } from './utils' | ||
|
||
export default async function Page() { | ||
try { | ||
await verifyAuth() | ||
} catch (err) { | ||
if (err.cause instanceof SessionExpiredError) { | ||
redirect('/') | ||
} | ||
|
||
throw err | ||
} | ||
|
||
return <p id="page">Cause Page</p> | ||
} |
Oops, something went wrong.