From 8eae2f018f3ae7c2d93a84c83cff2e5484512885 Mon Sep 17 00:00:00 2001 From: Jay V Date: Thu, 12 Dec 2024 17:16:01 -0500 Subject: [PATCH] exmaples formatting --- examples/client/nextjs/app/actions.ts | 57 +++++++++---------- .../client/nextjs/app/api/callback/route.ts | 16 +++--- examples/client/nextjs/app/auth.ts | 16 +++--- examples/client/nextjs/app/page.tsx | 9 +-- 4 files changed, 49 insertions(+), 49 deletions(-) diff --git a/examples/client/nextjs/app/actions.ts b/examples/client/nextjs/app/actions.ts index ca5134d5..9ee76f71 100644 --- a/examples/client/nextjs/app/actions.ts +++ b/examples/client/nextjs/app/actions.ts @@ -1,60 +1,59 @@ "use server" -import { headers as getHeaders, cookies as getCookies } from "next/headers" -import { redirect } from "next/navigation" -import { client, subjects, setTokens } from "./auth" +import { redirect } from "next/navigation"; +import { headers as getHeaders, cookies as getCookies } from "next/headers"; +import { client, subjects, setTokens } from "./auth"; export async function auth() { - const cookies = await getCookies() - const accessToken = cookies.get("access_token") - const refreshToken = cookies.get("refresh_token") + const cookies = await getCookies(); + const accessToken = cookies.get("access_token"); + const refreshToken = cookies.get("refresh_token"); if (!accessToken) { - return false + return false; } const verified = await client.verify(subjects, accessToken.value, { refresh: refreshToken?.value, - }) + }); if (verified.err) { - return false + return false; } if (verified.tokens) { - await setTokens(verified.tokens.access, verified.tokens.refresh) + await setTokens(verified.tokens.access, verified.tokens.refresh); } - return verified.subject + return verified.subject; } export async function login() { - const cookies = await getCookies() - const accessToken = cookies.get("access_token") - const refreshToken = cookies.get("refresh_token") + const cookies = await getCookies(); + const accessToken = cookies.get("access_token"); + const refreshToken = cookies.get("refresh_token"); if (accessToken) { const verified = await client.verify(subjects, accessToken.value, { refresh: refreshToken?.value, - }) + }); if (!verified.err && verified.tokens) { - await setTokens(verified.tokens.access, verified.tokens.refresh) - redirect("/") + await setTokens(verified.tokens.access, verified.tokens.refresh); + redirect("/"); } } - const headers = await getHeaders() - const host = headers.get("host") - const protocol = host?.includes("localhost") ? "http" : "https" - const redirectUrl = client.authorize( - `${protocol}://${host}/api/callback`, - "code", - ) - redirect(redirectUrl) + const headers = await getHeaders(); + const host = headers.get("host"); + const protocol = host?.includes("localhost") ? "http" : "https"; + const redirectUrl = client.authorize(`${protocol}://${host}/api/callback`, "code"); + + redirect(redirectUrl); } export async function logout() { - const cookies = await getCookies() - cookies.delete("access_token") - cookies.delete("refresh_token") - redirect("/") + const cookies = await getCookies(); + cookies.delete("access_token"); + cookies.delete("refresh_token"); + + redirect("/"); } diff --git a/examples/client/nextjs/app/api/callback/route.ts b/examples/client/nextjs/app/api/callback/route.ts index 31773848..acbc6be1 100644 --- a/examples/client/nextjs/app/api/callback/route.ts +++ b/examples/client/nextjs/app/api/callback/route.ts @@ -1,16 +1,16 @@ -import { client, setTokens } from "../../auth" -import { type NextRequest, NextResponse } from "next/server" +import { client, setTokens } from "../../auth"; +import { type NextRequest, NextResponse } from "next/server"; export async function GET(req: NextRequest) { - const url = new URL(req.url) - const code = url.searchParams.get("code") + const url = new URL(req.url); + const code = url.searchParams.get("code"); try { - const tokens = await client.exchange(code!, `${url.origin}/api/callback`) - await setTokens(tokens.access, tokens.refresh) + const tokens = await client.exchange(code!, `${url.origin}/api/callback`); + await setTokens(tokens.access, tokens.refresh); - return NextResponse.redirect(url.origin + "/") + return NextResponse.redirect(`${url.origin}/`); } catch (e) { - return NextResponse.json(e, { status: 500 }) + return NextResponse.json(e, { status: 500 }); } } diff --git a/examples/client/nextjs/app/auth.ts b/examples/client/nextjs/app/auth.ts index 3654e416..48943f91 100644 --- a/examples/client/nextjs/app/auth.ts +++ b/examples/client/nextjs/app/auth.ts @@ -1,14 +1,14 @@ -import { createClient } from "@openauthjs/openauth/client" -import { cookies as getCookies } from "next/headers" -export { subjects } from "../../../subjects" +import { createClient } from "@openauthjs/openauth/client"; +import { cookies as getCookies } from "next/headers"; +export { subjects } from "../../../subjects"; export const client = createClient({ - clientID: "astro", + clientID: "nextjs", issuer: "http://localhost:3000", -}) +}); export async function setTokens(access: string, refresh: string) { - const cookies = await getCookies() + const cookies = await getCookies(); cookies.set({ name: "access_token", @@ -17,7 +17,7 @@ export async function setTokens(access: string, refresh: string) { sameSite: "lax", path: "/", maxAge: 34560000, - }) + }); cookies.set({ name: "refresh_token", value: refresh, @@ -25,5 +25,5 @@ export async function setTokens(access: string, refresh: string) { sameSite: "lax", path: "/", maxAge: 34560000, - }) + }); } diff --git a/examples/client/nextjs/app/page.tsx b/examples/client/nextjs/app/page.tsx index d505f68f..73b10fd5 100644 --- a/examples/client/nextjs/app/page.tsx +++ b/examples/client/nextjs/app/page.tsx @@ -1,9 +1,10 @@ -import Image from "next/image" -import { auth, login, logout } from "./actions" -import styles from "./page.module.css" +import Image from "next/image"; +import { auth, login, logout } from "./actions"; +import styles from "./page.module.css"; export default async function Home() { - const subject = await auth() + const subject = await auth(); + return (