Skip to content

Commit

Permalink
auto: format code
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Dec 12, 2024
1 parent 8eae2f0 commit 2b5f475
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 45 deletions.
55 changes: 29 additions & 26 deletions examples/client/nextjs/app/actions.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,62 @@
"use server"

import { redirect } from "next/navigation";
import { headers as getHeaders, cookies as getCookies } from "next/headers";
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");
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);
redirect(redirectUrl)
}

export async function logout() {
const cookies = await getCookies();
cookies.delete("access_token");
cookies.delete("refresh_token");
const cookies = await getCookies()
cookies.delete("access_token")
cookies.delete("refresh_token")

redirect("/");
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 })
}
}
14 changes: 7 additions & 7 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: "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,
});
})
}
8 changes: 4 additions & 4 deletions examples/client/nextjs/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
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}>
Expand Down

0 comments on commit 2b5f475

Please sign in to comment.