-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8eae2f0
commit 2b5f475
Showing
4 changed files
with
48 additions
and
45 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 |
---|---|---|
@@ -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("/") | ||
} |
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,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 }) | ||
} | ||
} |
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