generated from toakiryu/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from scratch-auth/fix-nextjs
Fix nextjs
- Loading branch information
Showing
10 changed files
with
145 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
"use client"; | ||
|
||
import React from "react"; | ||
import { useUser } from "../hooks/useUser"; | ||
|
||
type ConfigProps = { | ||
noLogin: { | ||
redirect?: string; | ||
function?: () => void; | ||
children?: React.ReactNode; | ||
}; | ||
}; | ||
|
||
export interface ScratchAuthProviderProps { | ||
children: React.ReactNode; | ||
config?: ConfigProps; | ||
} | ||
|
||
const ScratchAuthProvider = ({ | ||
children, | ||
config = { | ||
noLogin: { | ||
redirect: "/", | ||
}, | ||
}, | ||
}: ScratchAuthProviderProps) => { | ||
const { user, loading, refreshUser } = useUser(); | ||
|
||
React.useEffect(() => { | ||
const handleCookieChange = () => { | ||
refreshUser(); // クッキーが変化した際にユーザー情報を更新 | ||
}; | ||
|
||
if (typeof window !== undefined) { | ||
window.addEventListener("sah-sessionchange", handleCookieChange); // クッキーの変化を監視 | ||
|
||
return () => { | ||
window.removeEventListener("sah-sessionchange", handleCookieChange); // クリーンアップ | ||
}; | ||
} | ||
}, [refreshUser]); | ||
|
||
if (loading) { | ||
return <>{config?.noLogin.children}</> || null; | ||
} | ||
|
||
if (!user) { | ||
if (config?.noLogin.redirect && typeof window !== undefined) { | ||
window.location.href = config?.noLogin.redirect; | ||
return null; | ||
} | ||
if (config?.noLogin.function) { | ||
config?.noLogin.function(); | ||
} | ||
return <>{config?.noLogin.children}</> || null; | ||
} | ||
return <>{children}</>; | ||
}; | ||
ScratchAuthProvider.displayName = "ScratchAuthProvider"; | ||
|
||
export default ScratchAuthProvider; |
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 @@ | ||
# COMING SOON |
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,9 @@ | ||
import ScratchAuthProvider from "@scratch-auth/nextjs/src/components/provider"; | ||
|
||
export default function DashboardLayout({ | ||
children, | ||
}: Readonly<{ | ||
children: React.ReactNode; | ||
}>) { | ||
return <ScratchAuthProvider>{children}</ScratchAuthProvider>; | ||
} |
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,9 @@ | ||
import React from 'react' | ||
|
||
function page() { | ||
return ( | ||
<div>page</div> | ||
) | ||
} | ||
|
||
export default page |