@mikandev/next-discord-auth 0.0.7 → 0.0.8
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/index.js +18 -1
- package/dist/redirect.js +3 -1
- package/dist/server-actions.js +9 -5
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,6 +1,23 @@
|
|
|
1
1
|
let globalConfig = null;
|
|
2
2
|
export function setup(config) {
|
|
3
|
-
|
|
3
|
+
if (!config || !config.redirectUri || !config.jwtSecret) {
|
|
4
|
+
throw new Error("Invalid configuration. 'redirectUri' and 'jwtSecret' are required.");
|
|
5
|
+
}
|
|
6
|
+
if (!config.clientId || !config.clientSecret) {
|
|
7
|
+
if (!process.env.AUTH_DISCORD_ID || !process.env.AUTH_DISCORD_SECRET) {
|
|
8
|
+
throw new Error("You must provide 'clientId' and 'clientSecret' in the config or set them as environment variables AUTH_DISCORD_ID and AUTH_DISCORD_SECRET.");
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
if (globalConfig) {
|
|
12
|
+
console.warn("Global config is already set. Overwriting existing config.");
|
|
13
|
+
}
|
|
14
|
+
globalConfig = {
|
|
15
|
+
clientId: config.clientId ?? process.env.AUTH_DISCORD_ID ?? (() => { throw new Error("clientId is required"); })(),
|
|
16
|
+
clientSecret: config.clientSecret ?? process.env.AUTH_DISCORD_SECRET ?? (() => { throw new Error("clientSecret is required"); })(),
|
|
17
|
+
scopes: config.scopes ?? ["identify", "email"],
|
|
18
|
+
redirectUri: config.redirectUri,
|
|
19
|
+
jwtSecret: config.jwtSecret,
|
|
20
|
+
};
|
|
4
21
|
}
|
|
5
22
|
export function getGlobalConfig() {
|
|
6
23
|
if (!globalConfig) {
|
package/dist/redirect.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { NextResponse } from "next/server";
|
|
2
|
+
import { getGlobalConfig } from "./index";
|
|
2
3
|
import { cookies } from "next/headers";
|
|
3
4
|
import { ExchangeCodeForTokens } from "./lib/oauth";
|
|
4
5
|
import jwt from "jsonwebtoken";
|
|
5
|
-
export const handleRedirect = async (
|
|
6
|
+
export const handleRedirect = async (req) => {
|
|
7
|
+
const config = getGlobalConfig();
|
|
6
8
|
const cookieStore = await cookies();
|
|
7
9
|
const params = new URL(req.url).searchParams;
|
|
8
10
|
const code = params.get("code");
|
package/dist/server-actions.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { NextRequest, NextResponse } from "next/server";
|
|
2
|
+
import { getGlobalConfig } from "./index";
|
|
2
3
|
import jwt from "jsonwebtoken";
|
|
3
|
-
export const getSession = async (
|
|
4
|
+
export const getSession = async (req) => {
|
|
5
|
+
const config = getGlobalConfig();
|
|
4
6
|
const token = req.cookies.get("AUTH_SESSION")?.value;
|
|
5
7
|
if (!token) {
|
|
6
8
|
return null;
|
|
@@ -13,16 +15,18 @@ export const getSession = async (config, req) => {
|
|
|
13
15
|
return null;
|
|
14
16
|
}
|
|
15
17
|
};
|
|
16
|
-
export const signIn = async (
|
|
17
|
-
const
|
|
18
|
+
export const signIn = async (req) => {
|
|
19
|
+
const config = getGlobalConfig();
|
|
20
|
+
const session = await getSession(req);
|
|
18
21
|
if (session) {
|
|
19
22
|
return NextResponse.json({ message: "Already signed in" }, { status: 200 });
|
|
20
23
|
}
|
|
21
24
|
const signInURL = `https://discord.com/api/oauth2/authorize?client_id=${config.clientId}&redirect_uri=${encodeURIComponent(config.redirectUri)}&response_type=code&scope=${config.scopes.join(" ")}`;
|
|
22
25
|
return NextResponse.redirect(signInURL, 302);
|
|
23
26
|
};
|
|
24
|
-
export const signOut = async (
|
|
25
|
-
const
|
|
27
|
+
export const signOut = async (req) => {
|
|
28
|
+
const config = getGlobalConfig();
|
|
29
|
+
const session = await getSession(req);
|
|
26
30
|
if (!session) {
|
|
27
31
|
return NextResponse.json({ message: "Not signed in" }, { status: 401 });
|
|
28
32
|
}
|