-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding app router example for custom page ui
- Loading branch information
Showing
39 changed files
with
4,663 additions
and
15 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,7 @@ | ||
import { AuthOptions } from "next-auth"; | ||
|
||
import { lightningProvider } from "../../lnauth/[...lnauth]/config"; | ||
|
||
export const authOptions: AuthOptions = { | ||
providers: [lightningProvider], | ||
}; |
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,6 @@ | ||
import NextAuth from "next-auth"; | ||
import { authOptions } from "./config"; | ||
|
||
const handler = NextAuth(authOptions); | ||
|
||
export { handler as GET, handler as POST }; |
56 changes: 56 additions & 0 deletions
56
examples/ui-app-router/app/api/lnauth/[...lnauth]/config.ts
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,56 @@ | ||
import NextAuthLightning, { | ||
NextAuthLightningConfig, | ||
} from "next-auth-lightning-provider"; | ||
import generateQr from "next-auth-lightning-provider/generators/qr"; | ||
import generateName from "next-auth-lightning-provider/generators/name"; | ||
import generateAvatar from "next-auth-lightning-provider/generators/avatar"; | ||
|
||
import { env } from "@/env.mjs"; | ||
|
||
import storage from "node-persist"; // ⚠️ WARNING using node-persist is not recommended in lambda or edge environments. | ||
|
||
await storage.init(); | ||
|
||
const config: NextAuthLightningConfig = { | ||
// required | ||
siteUrl: env.NEXTAUTH_URL, | ||
secret: env.NEXTAUTH_SECRET, | ||
storage: { | ||
async set({ k1, session }) { | ||
await storage.setItem(`k1:${k1}`, session); | ||
}, | ||
async get({ k1 }) { | ||
return await storage.getItem(`k1:${k1}`); | ||
}, | ||
async update({ k1, session }) { | ||
const old = await storage.getItem(`k1:${k1}`); | ||
await storage.updateItem(`k1:${k1}`, { ...old, ...session }); | ||
}, | ||
async delete({ k1 }) { | ||
await storage.removeItem(`k1:${k1}`); | ||
}, | ||
}, | ||
generateQr, | ||
|
||
// optional | ||
generateName, | ||
generateAvatar, | ||
|
||
pages: { | ||
signIn: "/signin", | ||
error: "/error", | ||
}, | ||
flags: { | ||
diagnostics: true, | ||
logs: true, | ||
}, | ||
theme: { | ||
colorScheme: "dark", | ||
}, | ||
}; | ||
|
||
const { provider, handler } = NextAuthLightning(config); | ||
|
||
export const lightningProvider = provider; | ||
|
||
export { handler }; |
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,3 @@ | ||
import { handler } from "./config"; | ||
|
||
export { handler as GET, handler as POST }; |
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,89 @@ | ||
"use client"; | ||
|
||
import { useLightningAuth } from "next-auth-lightning-provider/hooks"; | ||
|
||
export default function LightningAuth({ | ||
redirectUri, | ||
state, | ||
}: { | ||
redirectUri: string; | ||
state: string; | ||
}) { | ||
const { lnurl, qr, button } = useLightningAuth({ redirectUri, state }); | ||
|
||
if (!lnurl) { | ||
return ( | ||
<div style={{ textAlign: "center", color: "black" }}>loading...</div> | ||
); | ||
} | ||
|
||
return ( | ||
<div | ||
style={{ | ||
margin: "auto", | ||
maxWidth: 400, | ||
textAlign: "center", | ||
background: "#fff", | ||
color: "#000", | ||
padding: "20px 30px", | ||
borderRadius: 20, | ||
}} | ||
> | ||
<h1 | ||
style={{ | ||
fontSize: 20, | ||
color: "#000", | ||
marginTop: 0, | ||
marginBottom: 15, | ||
}} | ||
> | ||
Plain JSX | ||
</h1> | ||
<img | ||
width={500} | ||
height={500} | ||
alt="Login with Lightning - QRCode" | ||
style={{ | ||
display: "block", | ||
overflow: "hidden", | ||
borderRadius: 5, | ||
width: "100%", | ||
height: "auto", | ||
}} | ||
src={qr} | ||
/> | ||
<pre | ||
style={{ | ||
wordBreak: "break-all", | ||
whiteSpace: "pre-wrap", | ||
userSelect: "all", | ||
marginTop: 10, | ||
marginBottom: 10, | ||
}} | ||
> | ||
{lnurl} | ||
</pre> | ||
|
||
<a | ||
style={{ | ||
alignItems: "center", | ||
backgroundColor: "#f2f2f2", | ||
textDecoration: "none", | ||
border: `2px solid rgba(110, 110, 110, 0.3)`, | ||
borderRadius: 10, | ||
color: "#000", | ||
display: "flex", | ||
fontSize: "1.1rem", | ||
fontWeight: "500", | ||
justifyContent: "center", | ||
minHeight: "30px", | ||
padding: ".75rem 1rem", | ||
position: "relative", | ||
}} | ||
href={button} | ||
> | ||
Open Lightning Wallet | ||
</a> | ||
</div> | ||
); | ||
} |
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,20 @@ | ||
"use client"; | ||
|
||
import { Session } from "next-auth"; | ||
import { signOut, signIn } from "next-auth/react"; | ||
|
||
export const SignInButton = async ({ | ||
session, | ||
}: { | ||
session: Session | null; | ||
}) => { | ||
return ( | ||
<div> | ||
{session ? ( | ||
<button onClick={() => signOut()}>Sign out</button> | ||
) : ( | ||
<button onClick={() => signIn()}>Sign in</button> | ||
)} | ||
</div> | ||
); | ||
}; |
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,40 @@ | ||
"use client"; | ||
|
||
import { useSearchParams } from "next/navigation"; | ||
import { ErrorCodes } from "next-auth-lightning-provider"; | ||
|
||
function formatErrorCode(value: any): keyof typeof ErrorCodes { | ||
if (Array.isArray(value)) value = value[0]; | ||
if (value && ErrorCodes.hasOwnProperty(value)) { | ||
return value as keyof typeof ErrorCodes; | ||
} | ||
return "Default"; | ||
} | ||
|
||
export default function Error() { | ||
const searchParams = useSearchParams(); | ||
const query = { | ||
message: searchParams?.get("message"), | ||
error: searchParams?.get("error"), | ||
}; | ||
|
||
const errorCode = formatErrorCode(query.error); | ||
|
||
// access an error message from the query parameters | ||
const queryMessage = query.message || ErrorCodes.Default; | ||
|
||
// access an error message from the `ErrorCodes` enum | ||
const hardCodedMessage = ErrorCodes[errorCode]; | ||
|
||
return ( | ||
<div style={{ textAlign: "center", color: "red" }}> | ||
<b>Query param message:</b> | ||
<br /> | ||
{queryMessage} | ||
<br /> | ||
<b>Hard coded message:</b> | ||
<br /> | ||
{hardCodedMessage} | ||
</div> | ||
); | ||
} |
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,27 @@ | ||
import { ReactNode } from "react"; | ||
import { Metadata } from "next"; | ||
|
||
/** | ||
* Default metadata. | ||
* | ||
* @see https://nextjs.org/docs/app/api-reference/file-conventions/metadata | ||
*/ | ||
export const metadata: Metadata = { | ||
title: "Login with Lightning", | ||
}; | ||
|
||
/** | ||
* The homepage root layout. | ||
* | ||
* @see https://nextjs.org/docs/app/api-reference/file-conventions/layout | ||
*/ | ||
export default function RootLayout({ children }: { children: ReactNode }) { | ||
return ( | ||
<html lang="en"> | ||
<head /> | ||
<body> | ||
<main>{children}</main> | ||
</body> | ||
</html> | ||
); | ||
} |
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,32 @@ | ||
import { getServerSession } from "next-auth"; | ||
|
||
import { authOptions } from "@/app/api/auth/[...nextauth]/config"; | ||
import { SignInButton } from "./components/SignInButton"; | ||
|
||
const Home = async () => { | ||
const session = await getServerSession(authOptions); | ||
|
||
return ( | ||
<div> | ||
<h3>Auth session:</h3> | ||
|
||
{session?.user?.image && ( | ||
<> | ||
<img | ||
width="50px" | ||
height="50px" | ||
src={session.user?.image} | ||
alt={`Avatar of ${session.user?.name}`} | ||
/> | ||
<br /> | ||
</> | ||
)} | ||
|
||
<pre>{JSON.stringify({ session }, null, 2)}</pre> | ||
|
||
<SignInButton session={session} /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Home; |
Oops, something went wrong.