@mikandev/next-discord-auth 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/server-actions.js +19 -10
- package/dist/types/server-actions.d.ts +4 -4
- package/package.json +1 -1
package/dist/server-actions.js
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { NextRequest, NextResponse } from "next/server";
|
|
2
|
+
import { cookies } from "next/headers";
|
|
3
|
+
import { redirect } from "next/navigation";
|
|
2
4
|
import { getGlobalConfig } from "./index";
|
|
3
5
|
import jwt from "jsonwebtoken";
|
|
4
|
-
export const getSession = async (
|
|
6
|
+
export const getSession = async () => {
|
|
5
7
|
const config = getGlobalConfig();
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
+
const cookieStore = await cookies();
|
|
9
|
+
const token = cookieStore.get("AUTH_SESSION")?.value;
|
|
10
|
+
if (!token)
|
|
8
11
|
return null;
|
|
9
|
-
}
|
|
10
12
|
try {
|
|
11
13
|
return jwt.verify(token, config.jwtSecret);
|
|
12
14
|
}
|
|
@@ -15,21 +17,28 @@ export const getSession = async (req) => {
|
|
|
15
17
|
return null;
|
|
16
18
|
}
|
|
17
19
|
};
|
|
18
|
-
export const signIn = async (
|
|
20
|
+
export const signIn = async () => {
|
|
19
21
|
const config = getGlobalConfig();
|
|
20
|
-
const session = await getSession(
|
|
22
|
+
const session = await getSession();
|
|
21
23
|
if (session) {
|
|
22
24
|
return NextResponse.json({ message: "Already signed in" }, { status: 200 });
|
|
23
25
|
}
|
|
24
26
|
const signInURL = `https://discord.com/api/oauth2/authorize?client_id=${config.clientId}&redirect_uri=${encodeURIComponent(config.redirectUri)}&response_type=code&scope=${config.scopes.join(" ")}`;
|
|
25
|
-
return
|
|
27
|
+
return redirect(signInURL);
|
|
26
28
|
};
|
|
27
|
-
export const signOut = async (
|
|
29
|
+
export const signOut = async () => {
|
|
28
30
|
const config = getGlobalConfig();
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
+
const cookieStore = await cookies();
|
|
32
|
+
const token = cookieStore.get("AUTH_SESSION")?.value;
|
|
33
|
+
if (!token) {
|
|
31
34
|
return NextResponse.json({ message: "Not signed in" }, { status: 401 });
|
|
32
35
|
}
|
|
36
|
+
try {
|
|
37
|
+
jwt.verify(token, config.jwtSecret);
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
return NextResponse.json({ message: "Invalid session" }, { status: 401 });
|
|
41
|
+
}
|
|
33
42
|
const response = NextResponse.json({ message: "Signed out successfully" }, { status: 200 });
|
|
34
43
|
response.cookies.delete("AUTH_SESSION");
|
|
35
44
|
return response;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { NextResponse } from "next/server";
|
|
2
2
|
import type { Session } from "./index";
|
|
3
3
|
|
|
4
|
-
export declare function getSession(
|
|
4
|
+
export declare function getSession(): Promise<Session | null>;
|
|
5
5
|
|
|
6
|
-
export declare function signIn(
|
|
6
|
+
export declare function signIn(): Promise<NextResponse>;
|
|
7
7
|
|
|
8
|
-
export declare function signOut(
|
|
8
|
+
export declare function signOut(): Promise<NextResponse>;
|