Skip to content

Commit cb0dc9b

Browse files
committed
fix: Server Actions compatibility
1 parent 9a64995 commit cb0dc9b

3 files changed

Lines changed: 24 additions & 21 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"main": "dist/index.js",
77
"files": ["dist"],
88
"license": "WTFPL",
9-
"version": "0.1.1",
9+
"version": "0.1.2",
1010
"type": "module",
1111
"repository": {
1212
"type": "git",

src/server-actions.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { NextRequest, NextResponse } from "next/server";
2+
import { cookies } from "next/headers";
23
import { type Session, getGlobalConfig } from "./index";
34
import jwt from "jsonwebtoken";
45

5-
export const getSession = async (req: NextRequest): Promise<Session | null> => {
6+
export const getSession = async (): Promise<Session | null> => {
67
const config = getGlobalConfig();
7-
const token = req.cookies.get("AUTH_SESSION")?.value;
8-
if (!token) {
9-
return null;
10-
}
8+
const cookieStore = await cookies();
9+
const token = cookieStore.get("AUTH_SESSION")?.value;
10+
if (!token) return null;
1111

1212
try {
1313
return jwt.verify(token, config.jwtSecret) as Session;
@@ -19,29 +19,32 @@ export const getSession = async (req: NextRequest): Promise<Session | null> => {
1919

2020
export const signIn = async (req: NextRequest): Promise<NextResponse> => {
2121
const config = getGlobalConfig();
22-
const session = await getSession(req);
22+
const session = await getSession();
2323
if (session) {
24-
return NextResponse.json(
25-
{ message: "Already signed in" },
26-
{ status: 200 },
27-
);
24+
return NextResponse.json({ message: "Already signed in" }, { status: 200 });
2825
}
29-
3026
const signInURL = `https://discord.com/api/oauth2/authorize?client_id=${config.clientId}&redirect_uri=${encodeURIComponent(config.redirectUri)}&response_type=code&scope=${config.scopes.join(" ")}`;
3127
return NextResponse.redirect(signInURL, 302);
3228
};
3329

34-
export const signOut = async (req: NextRequest): Promise<NextResponse> => {
30+
export const signOut = async (): Promise<NextResponse> => {
3531
const config = getGlobalConfig();
36-
const session = await getSession(req);
37-
if (!session) {
32+
const cookieStore = await cookies();
33+
const token = cookieStore.get("AUTH_SESSION")?.value;
34+
if (!token) {
3835
return NextResponse.json({ message: "Not signed in" }, { status: 401 });
3936
}
4037

38+
try {
39+
jwt.verify(token, config.jwtSecret);
40+
} catch {
41+
return NextResponse.json({ message: "Invalid session" }, { status: 401 });
42+
}
43+
4144
const response = NextResponse.json(
4245
{ message: "Signed out successfully" },
43-
{ status: 200 },
46+
{ status: 200 }
4447
);
4548
response.cookies.delete("AUTH_SESSION");
4649
return response;
47-
};
50+
};

types/server-actions.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import type { NextRequest, NextResponse } from "next/server";
1+
import type { NextResponse } from "next/server";
22
import type { Session } from "./index";
33

4-
export declare function getSession(req: NextRequest): Promise<Session | null>;
4+
export declare function getSession(): Promise<Session | null>;
55

6-
export declare function signIn(req: NextRequest): Promise<NextResponse>;
6+
export declare function signIn(): Promise<NextResponse>;
77

8-
export declare function signOut(req: NextRequest): Promise<NextResponse>;
8+
export declare function signOut(): Promise<NextResponse>;

0 commit comments

Comments
 (0)