@mikandev/next-discord-auth 0.1.0 → 0.1.2

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 CHANGED
@@ -12,8 +12,16 @@ export function setup(config) {
12
12
  console.warn("Global config is already set. Overwriting existing config.");
13
13
  }
14
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"); })(),
15
+ clientId: config.clientId ??
16
+ process.env.AUTH_DISCORD_ID ??
17
+ (() => {
18
+ throw new Error("clientId is required");
19
+ })(),
20
+ clientSecret: config.clientSecret ??
21
+ process.env.AUTH_DISCORD_SECRET ??
22
+ (() => {
23
+ throw new Error("clientSecret is required");
24
+ })(),
17
25
  scopes: config.scopes ?? ["identify", "email"],
18
26
  redirectUri: config.redirectUri,
19
27
  jwtSecret: config.jwtSecret,
@@ -1,12 +1,13 @@
1
1
  import { NextRequest, NextResponse } from "next/server";
2
+ import { cookies } from "next/headers";
2
3
  import { getGlobalConfig } from "./index";
3
4
  import jwt from "jsonwebtoken";
4
- export const getSession = async (req) => {
5
+ export const getSession = async () => {
5
6
  const config = getGlobalConfig();
6
- const token = req.cookies.get("AUTH_SESSION")?.value;
7
- if (!token) {
7
+ const cookieStore = await cookies();
8
+ const token = cookieStore.get("AUTH_SESSION")?.value;
9
+ if (!token)
8
10
  return null;
9
- }
10
11
  try {
11
12
  return jwt.verify(token, config.jwtSecret);
12
13
  }
@@ -17,19 +18,26 @@ export const getSession = async (req) => {
17
18
  };
18
19
  export const signIn = async (req) => {
19
20
  const config = getGlobalConfig();
20
- const session = await getSession(req);
21
+ const session = await getSession();
21
22
  if (session) {
22
23
  return NextResponse.json({ message: "Already signed in" }, { status: 200 });
23
24
  }
24
25
  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
26
  return NextResponse.redirect(signInURL, 302);
26
27
  };
27
- export const signOut = async (req) => {
28
+ export const signOut = async () => {
28
29
  const config = getGlobalConfig();
29
- const session = await getSession(req);
30
- if (!session) {
30
+ const cookieStore = await cookies();
31
+ const token = cookieStore.get("AUTH_SESSION")?.value;
32
+ if (!token) {
31
33
  return NextResponse.json({ message: "Not signed in" }, { status: 401 });
32
34
  }
35
+ try {
36
+ jwt.verify(token, config.jwtSecret);
37
+ }
38
+ catch {
39
+ return NextResponse.json({ message: "Invalid session" }, { status: 401 });
40
+ }
33
41
  const response = NextResponse.json({ message: "Signed out successfully" }, { status: 200 });
34
42
  response.cookies.delete("AUTH_SESSION");
35
43
  return response;
@@ -0,0 +1,21 @@
1
+ export interface Session {
2
+ user: {
3
+ id: string;
4
+ name: string;
5
+ email?: string;
6
+ avatar: string;
7
+ };
8
+ expires: string;
9
+ accessToken?: string;
10
+ }
11
+
12
+ export interface Config {
13
+ clientId: string;
14
+ clientSecret: string;
15
+ scopes: string[];
16
+ redirectUri: string;
17
+ jwtSecret: string;
18
+ }
19
+
20
+ export function setup(config: Config): void;
21
+ export function getGlobalConfig(): Config;
@@ -0,0 +1,5 @@
1
+ import type { NextRequest, NextResponse } from "next/server";
2
+
3
+ export declare function handleRedirect(
4
+ req: NextRequest,
5
+ ): Promise<NextResponse | void>;
@@ -0,0 +1,8 @@
1
+ import type { NextResponse } from "next/server";
2
+ import type { Session } from "./index";
3
+
4
+ export declare function getSession(): Promise<Session | null>;
5
+
6
+ export declare function signIn(): Promise<NextResponse>;
7
+
8
+ export declare function signOut(): Promise<NextResponse>;
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "@mikandev/next-discord-auth",
3
+ "description": "Discord OAuth for Next.js that doesn't suck",
3
4
  "module": "dist/index.ts",
4
5
  "types": "dist/index.d.ts",
5
6
  "main": "dist/index.js",
6
7
  "files": ["dist"],
7
8
  "license": "WTFPL",
8
- "version": "0.1.0",
9
+ "version": "0.1.2",
9
10
  "type": "module",
10
11
  "repository": {
11
12
  "type": "git",
@@ -29,5 +30,28 @@
29
30
  "dependencies": {
30
31
  "jsonwebtoken": "^9.0.2",
31
32
  "next": "^15.3.2"
33
+ },
34
+ "exports": {
35
+ ".": {
36
+ "import": "./dist/index.js",
37
+ "require": "./dist/index.js",
38
+ "types": "./dist/types/index.d.ts"
39
+ },
40
+ "./redirect": {
41
+ "import": "./dist/redirect.js",
42
+ "require": "./dist/redirect.js",
43
+ "types": "./dist/types/redirect.d.ts"
44
+ },
45
+ "./server-actions": {
46
+ "import": "./dist/server-actions.js",
47
+ "require": "./dist/server-actions.js",
48
+ "types": "./dist/types/server-actions.d.ts"
49
+ }
50
+ },
51
+ "typesVersions": {
52
+ "*": {
53
+ "redirect": ["dist/types/redirect.d.ts"],
54
+ "server-actions": ["dist/types/server-actions.d.ts"]
55
+ }
32
56
  }
33
57
  }
package/dist/index.d.ts DELETED
@@ -1 +0,0 @@
1
- declare module '@mikandev/next-discord-auth';