-
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 basic
server/
folder for server component utils
- Loading branch information
Showing
12 changed files
with
222 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ yarn-error.log* | |
|
||
# build | ||
/main | ||
/server | ||
/hooks | ||
/utils | ||
/generators | ||
|
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 |
---|---|---|
@@ -1,28 +1,26 @@ | ||
"use client"; | ||
|
||
import { useSearchParams } from "next/navigation"; | ||
|
||
import LightningAuth from "@/app/components/LightningAuth"; | ||
|
||
export default function SignIn() { | ||
const searchParams = useSearchParams(); | ||
const redirectUri = searchParams?.get("redirect_uri"); | ||
const state = searchParams?.get("state"); | ||
import { createLightningAuth } from "next-auth-lightning-provider/server"; | ||
|
||
export default async function SignIn({ | ||
searchParams, | ||
}: { | ||
searchParams: Record<string, string | string[]>; | ||
}) { | ||
let session, error; | ||
try { | ||
session = await createLightningAuth(searchParams); | ||
} catch (e: any) { | ||
error = e.message || "Something went wrong"; | ||
} | ||
|
||
if (!redirectUri || !state) { | ||
return ( | ||
<div style={{ textAlign: "center", color: "red" }}> | ||
Missing query params | ||
</div> | ||
); | ||
if (error || !session) { | ||
return <div style={{ textAlign: "center", color: "red" }}>{error}</div>; | ||
} | ||
|
||
return ( | ||
<div> | ||
<LightningAuth | ||
redirectUri={redirectUri as string} | ||
state={state as string} | ||
/> | ||
<LightningAuth session={session} /> | ||
</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
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./useLightningAuth"; | ||
export * from "./useLightningPolling"; |
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,125 @@ | ||
"use client"; | ||
|
||
import { useEffect } from "react"; | ||
import { useRouter } from "next/navigation"; | ||
|
||
import { hardConfig } from "../main/config/hard"; | ||
import { LightningAuthClientSession } from "../server"; | ||
import { formatLightningAuth } from "../utils/lnurl"; | ||
|
||
const maxNetworkRequestsFailures = 3; | ||
|
||
/** | ||
* A React hook that, on mount, will poll the API and checks if the Lightning auth QR has been scanned. | ||
* If enough time elapses without a sign in attempt, the page will be refreshed. | ||
* Once a success status is received from polling, the user will be redirected to the `next-auth` redirect url. | ||
* | ||
* @param {LightningAuthClientSession} session - a session object generated by invoking the `createLightningAuth` method | ||
* | ||
* @returns {Object} | ||
* @returns {String} lnurl - the raw LNURL, should be made available for copy-pasting | ||
* @returns {String} qr - a url pointing the lnurl-auth QR Code image, should be used in the src prop of img tags | ||
* @returns {String} button - a deep-link that will open in Lightning enabled wallets, should be used in the href prop of anchor tags | ||
*/ | ||
export function useLightningPolling(session: LightningAuthClientSession): { | ||
lnurl: string | null; | ||
qr: string; | ||
button: string; | ||
} { | ||
const router = useRouter(); | ||
|
||
useEffect(() => { | ||
let pollTimeoutId: NodeJS.Timeout | undefined; | ||
let createIntervalId: NodeJS.Timeout | undefined; | ||
let networkRequestCount: number = 0; | ||
let errorUrl: string | undefined; | ||
const pollController = new AbortController(); | ||
|
||
// cleanup when the hook unmounts of polling is successful | ||
const cleanup = () => { | ||
clearTimeout(pollTimeoutId); | ||
clearInterval(createIntervalId); | ||
pollController.abort(); | ||
}; | ||
|
||
// redirect user to error page if something goes wrong | ||
function error(e: any) { | ||
console.error(e); | ||
if (errorUrl) { | ||
window.location.replace(errorUrl); | ||
} else { | ||
// if no errorUrl exists send to defaul `next-auth` error page | ||
const params = new URLSearchParams(); | ||
params.append("error", "OAuthSignin"); | ||
window.location.replace(`/api/auth/error?${params.toString()}`); | ||
} | ||
} | ||
|
||
// poll the api to see if the user has successfully authenticated | ||
function poll() { | ||
if (!session?.data?.k1) return; | ||
const k1 = session.data.k1; | ||
|
||
const params = new URLSearchParams({ k1 }); | ||
|
||
return fetch(hardConfig.apis.poll, { | ||
method: "POST", | ||
headers: { "content-type": "application/x-www-form-urlencoded" }, | ||
body: params, | ||
cache: "default", | ||
signal: pollController.signal, | ||
}) | ||
.then((r) => { | ||
if (r.status === 410) { | ||
// if resource not found throw error immediately, | ||
// this means the user's auth session has been deleted. | ||
networkRequestCount = maxNetworkRequestsFailures; | ||
} | ||
return r.json(); | ||
}) | ||
.catch((e) => { | ||
// if there are more than X network errors, then trigger redirect | ||
networkRequestCount++; | ||
if (networkRequestCount >= maxNetworkRequestsFailures) { | ||
pollTimeoutId = setTimeout(poll, session.intervals.poll); | ||
throw e; | ||
} | ||
}) | ||
.then((d) => { | ||
if (d && d.error) { | ||
if (d.url) errorUrl = d.url; | ||
throw new Error(d.message || d.error); | ||
} | ||
|
||
if (d) networkRequestCount = 0; | ||
if (d && d.message) throw new Error(d.message); | ||
pollTimeoutId = setTimeout(poll, session.intervals.poll); | ||
|
||
if (d && d.success) { | ||
cleanup(); | ||
let url = new URL(session.query.redirectUri); | ||
url.searchParams.append("state", session.query.state); | ||
url.searchParams.append("code", k1); | ||
router.replace(url.toString()); | ||
} | ||
}) | ||
.catch((e) => { | ||
if (!pollController.signal.aborted) { | ||
error(e); | ||
} | ||
}); | ||
} | ||
|
||
function create() { | ||
router.refresh(); | ||
} | ||
|
||
pollTimeoutId = setTimeout(poll, session.intervals.poll); | ||
createIntervalId = setInterval(create, session.intervals.create); | ||
|
||
return () => cleanup(); | ||
}, []); | ||
|
||
const { lnurl, qr, button } = formatLightningAuth(session.data.lnurl); | ||
return { lnurl, qr, button }; | ||
} |
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,53 @@ | ||
import { formatLightningAuth } from "../utils/lnurl"; | ||
import { hardConfig } from "../main/config"; | ||
|
||
export type LightningAuthClientSession = { | ||
data: { | ||
k1: string; | ||
lnurl: string; | ||
}; | ||
intervals: { | ||
poll: number; | ||
create: number; | ||
}; | ||
query: { | ||
state: string; | ||
redirectUri: string; | ||
}; | ||
}; | ||
|
||
export default async function createLightningAuth( | ||
searchParams: any | ||
): Promise<LightningAuthClientSession> { | ||
const siteUrl = searchParams.client_id; | ||
const state = searchParams.state; | ||
const redirectUri = searchParams.redirect_uri; | ||
if (!redirectUri || !state) { | ||
throw new Error("Missing query params"); | ||
} | ||
|
||
const params = new URLSearchParams({ state }); | ||
const response = await fetch(siteUrl + hardConfig.apis.create, { | ||
method: "POST", | ||
headers: { "content-type": "application/x-www-form-urlencoded" }, | ||
body: params, | ||
cache: "no-cache", | ||
}); | ||
const data = await response.json(); | ||
|
||
if (!data.lnurl) { | ||
throw new Error("Missing lnurl"); | ||
} | ||
|
||
return { | ||
data: { | ||
k1: data.k1, | ||
lnurl: data.lnurl, | ||
}, | ||
intervals: { | ||
poll: data.pollInterval, | ||
create: data.createInterval, | ||
}, | ||
query: { state, redirectUri }, | ||
}; | ||
} |
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,4 @@ | ||
export { | ||
default as createLightningAuth, | ||
LightningAuthClientSession, | ||
} from "./createLightningAuth"; |
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 |
---|---|---|
|
@@ -35,6 +35,7 @@ | |
"main", | ||
"generators", | ||
"hooks", | ||
"server", | ||
"utils", | ||
"examples", | ||
"node_modules" | ||
|