Skip to content

Commit

Permalink
exmaples formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jayair committed Dec 12, 2024
1 parent c128a83 commit 8eae2f0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 49 deletions.
57 changes: 28 additions & 29 deletions examples/client/nextjs/app/actions.ts
Original file line number Diff line number Diff line change
@@ -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("/");
}
16 changes: 8 additions & 8 deletions examples/client/nextjs/app/api/callback/route.ts
Original file line number Diff line number Diff line change
@@ -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 });
}
}
16 changes: 8 additions & 8 deletions examples/client/nextjs/app/auth.ts
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -17,13 +17,13 @@ export async function setTokens(access: string, refresh: string) {
sameSite: "lax",
path: "/",
maxAge: 34560000,
})
});
cookies.set({
name: "refresh_token",
value: refresh,
httpOnly: true,
sameSite: "lax",
path: "/",
maxAge: 34560000,
})
});
}
9 changes: 5 additions & 4 deletions examples/client/nextjs/app/page.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<div className={styles.page}>
<main className={styles.main}>
Expand Down

0 comments on commit 8eae2f0

Please sign in to comment.