@mikandev/next-discord-auth 0.0.4 → 0.0.5

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.
@@ -1,3 +1,4 @@
1
+ name: Publish to NPM
1
2
  on:
2
3
  push:
3
4
  branches: master
package/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
2
+ Version 2, December 2004
3
+
4
+ Copyright (C) 2025 MikanDev <hello@mikn.dev>
5
+
6
+ Everyone is permitted to copy and distribute verbatim or modified
7
+ copies of this license document, and changing it is allowed as long
8
+ as the name is changed.
9
+
10
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
11
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
12
+
13
+ 0. You just DO WHAT THE FUCK YOU WANT TO.
package/biome.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "$schema": "https://biomejs.dev/schemas/1.5.1/schema.json",
3
+ "organizeImports": {
4
+ "enabled": true
5
+ },
6
+ "linter": {
7
+ "enabled": true,
8
+ "rules": {
9
+ "recommended": true
10
+ }
11
+ },
12
+ "formatter": {
13
+ "enabled": true,
14
+ "indentWidth": 4,
15
+ "indentStyle": "tab"
16
+ },
17
+ "javascript": {
18
+ "formatter": {
19
+ "bracketSpacing": true
20
+ }
21
+ }
22
+ }
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,26 @@
1
+ export const ExchangeCodeForTokens = async (config, code) => {
2
+ const response = await fetch("https://discord.com/api/oauth2/token", {
3
+ method: "POST",
4
+ headers: {
5
+ "Content-Type": "application/x-www-form-urlencoded",
6
+ },
7
+ body: new URLSearchParams({
8
+ client_id: config.clientId,
9
+ client_secret: config.clientSecret,
10
+ grant_type: "authorization_code",
11
+ code: code,
12
+ redirect_uri: config.redirectUri,
13
+ scope: config.scopes.join(" "),
14
+ }),
15
+ });
16
+ if (!response.ok) {
17
+ const error = (await response.json());
18
+ throw new Error(`Failed to exchange code for token: ${error.error_description || error.message}`);
19
+ }
20
+ const data = (await response.json());
21
+ return {
22
+ accessToken: data.access_token,
23
+ refreshToken: data.refresh_token,
24
+ expiresIn: data.expires_in,
25
+ };
26
+ };
@@ -0,0 +1,36 @@
1
+ import { NextResponse } from "next/server";
2
+ import { cookies } from "next/headers";
3
+ import { ExchangeCodeForTokens } from "./lib/oauth";
4
+ import jwt from "jsonwebtoken";
5
+ export const handleRedirect = async (config, req) => {
6
+ const cookieStore = await cookies();
7
+ const params = new URL(req.url).searchParams;
8
+ const code = params.get("code");
9
+ if (!code) {
10
+ return NextResponse.json({ error: "Authorization code not found" }, { status: 400 });
11
+ }
12
+ const response = await ExchangeCodeForTokens(config, code);
13
+ const sessionData = await fetch("https://discord.com/api/users/@me", {
14
+ headers: {
15
+ Authorization: `Bearer ${response.accessToken}`,
16
+ },
17
+ });
18
+ if (!sessionData.ok) {
19
+ const error = (await sessionData.json());
20
+ return NextResponse.json({ error: error.message || "Failed to fetch user data" }, { status: 500 });
21
+ }
22
+ const userData = (await sessionData.json());
23
+ const session = {
24
+ user: {
25
+ id: userData.user.id,
26
+ name: `${userData.user.username}#${userData.user.discriminator}`,
27
+ email: userData.user.email || null,
28
+ avatar: `https://cdn.discordapp.com/avatars/${userData.user.id}/${userData.user.avatar}.png`,
29
+ },
30
+ expires: new Date(Date.now() + response.expiresIn * 1000).toISOString(),
31
+ };
32
+ const token = jwt.sign(session, config.jwtSecret, {
33
+ expiresIn: response.expiresIn,
34
+ });
35
+ await cookieStore.set("AUTH_SESSION", token);
36
+ };
@@ -0,0 +1,15 @@
1
+ import jwt from "jsonwebtoken";
2
+ export const getSession = async (config, req) => {
3
+ const token = req.cookies.get("AUTH_SESSION")?.value;
4
+ if (!token) {
5
+ return null;
6
+ }
7
+ try {
8
+ const decoded = jwt.verify(token, config.jwtSecret);
9
+ return decoded;
10
+ }
11
+ catch (error) {
12
+ console.error("Invalid token:", error);
13
+ return null;
14
+ }
15
+ };
package/package.json CHANGED
@@ -1,28 +1,32 @@
1
1
  {
2
- "name": "@mikandev/next-discord-auth",
3
- "module": "dist/index.ts",
4
- "types": "dist/index.d.ts",
5
- "version": "0.0.4",
6
- "type": "module",
7
- "repository": {
8
- "type": "git",
9
- "url": "https://github.com/mikndotdev/next-discord-auth"
10
- },
11
- "homepage": "https://mikn.dev/solutions/developers/next-discord-auth",
12
- "scripts": {
13
- "build": "tsc",
14
- "format": "biome format --write ."
15
- },
16
- "devDependencies": {
17
- "@biomejs/biome": "^1.9.4",
18
- "@types/bun": "latest",
19
- "@types/jsonwebtoken": "^9.0.9"
20
- },
21
- "peerDependencies": {
22
- "typescript": "^5"
23
- },
24
- "dependencies": {
25
- "jsonwebtoken": "^9.0.2",
26
- "next": "^15.3.2"
27
- }
2
+ "name": "@mikandev/next-discord-auth",
3
+ "module": "dist/index.ts",
4
+ "types": "dist/index.d.ts",
5
+ "main": "dist/index.js",
6
+ "license": "WTFPL",
7
+ "version": "0.0.5",
8
+ "type": "module",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/mikndotdev/next-discord-auth"
12
+ },
13
+ "homepage": "https://mikn.dev/solutions/developers/next-discord-auth",
14
+ "scripts": {
15
+ "build": "tsc",
16
+ "format": "biome format --write .",
17
+ "lint": "biome lint",
18
+ "lintfix": "biome lint --fix"
19
+ },
20
+ "devDependencies": {
21
+ "@biomejs/biome": "^1.9.4",
22
+ "@types/bun": "latest",
23
+ "@types/jsonwebtoken": "^9.0.9"
24
+ },
25
+ "peerDependencies": {
26
+ "typescript": "^5"
27
+ },
28
+ "dependencies": {
29
+ "jsonwebtoken": "^9.0.2",
30
+ "next": "^15.3.2"
31
+ }
28
32
  }
package/src/index.ts CHANGED
@@ -1,17 +1,17 @@
1
1
  export interface Session {
2
- user: {
3
- id: string;
4
- name: string;
5
- email: string | null;
6
- avatar: string;
7
- }
8
- expires: string;
2
+ user: {
3
+ id: string;
4
+ name: string;
5
+ email: string | null;
6
+ avatar: string;
7
+ };
8
+ expires: string;
9
9
  }
10
10
 
11
11
  export interface Config {
12
- clientId: string;
13
- clientSecret: string;
14
- scopes: string[];
15
- redirectUri: string;
16
- jwtSecret: string;
17
- }
12
+ clientId: string;
13
+ clientSecret: string;
14
+ scopes: string[];
15
+ redirectUri: string;
16
+ jwtSecret: string;
17
+ }
package/src/lib/oauth.ts CHANGED
@@ -1,35 +1,40 @@
1
- import type { Config } from '../index';
1
+ import type { Config } from "../index";
2
2
 
3
3
  export const ExchangeCodeForTokens = async (config: Config, code: string) => {
4
- const response = await fetch('https://discord.com/api/oauth2/token', {
5
- method: 'POST',
6
- headers: {
7
- 'Content-Type': 'application/x-www-form-urlencoded',
8
- },
9
- body: new URLSearchParams({
10
- client_id: config.clientId,
11
- client_secret: config.clientSecret,
12
- grant_type: 'authorization_code',
13
- code: code,
14
- redirect_uri: config.redirectUri,
15
- scope: config.scopes.join(' '),
16
- }),
17
- });
4
+ const response = await fetch("https://discord.com/api/oauth2/token", {
5
+ method: "POST",
6
+ headers: {
7
+ "Content-Type": "application/x-www-form-urlencoded",
8
+ },
9
+ body: new URLSearchParams({
10
+ client_id: config.clientId,
11
+ client_secret: config.clientSecret,
12
+ grant_type: "authorization_code",
13
+ code: code,
14
+ redirect_uri: config.redirectUri,
15
+ scope: config.scopes.join(" "),
16
+ }),
17
+ });
18
18
 
19
- if (!response.ok) {
20
- const error = await response.json() as { error_description?: string; message?: string };
21
- throw new Error(`Failed to exchange code for token: ${error.error_description || error.message}`);
22
- }
19
+ if (!response.ok) {
20
+ const error = (await response.json()) as {
21
+ error_description?: string;
22
+ message?: string;
23
+ };
24
+ throw new Error(
25
+ `Failed to exchange code for token: ${error.error_description || error.message}`,
26
+ );
27
+ }
23
28
 
24
- const data = await response.json() as {
25
- access_token: string;
26
- refresh_token: string;
27
- expires_in: number;
28
- };
29
+ const data = (await response.json()) as {
30
+ access_token: string;
31
+ refresh_token: string;
32
+ expires_in: number;
33
+ };
29
34
 
30
- return {
31
- accessToken: data.access_token,
32
- refreshToken: data.refresh_token,
33
- expiresIn: data.expires_in,
34
- };
35
- };
35
+ return {
36
+ accessToken: data.access_token,
37
+ refreshToken: data.refresh_token,
38
+ expiresIn: data.expires_in,
39
+ };
40
+ };
package/src/redirect.ts CHANGED
@@ -1,52 +1,63 @@
1
- import { NextRequest, NextResponse } from "next/server";
1
+ import { type NextRequest, NextResponse } from "next/server";
2
2
  import type { Session, Config } from "./index";
3
3
  import { cookies } from "next/headers";
4
4
  import { ExchangeCodeForTokens } from "./lib/oauth";
5
5
  import jwt from "jsonwebtoken";
6
6
 
7
7
  export const handleRedirect = async (config: Config, req: NextRequest) => {
8
- const cookieStore = await cookies();
9
- const params = new URL(req.url).searchParams;
10
- const code = params.get("code");
11
-
12
- if (!code) {
13
- return NextResponse.json({error: "Authorization code not found"}, {status: 400});
14
- }
15
-
16
- const response = await ExchangeCodeForTokens(config, code);
17
-
18
- const sessionData = await fetch('https://discord.com/api/users/@me', {
19
- headers: {
20
- 'Authorization': `Bearer ${response.accessToken}`,
21
- },
22
- });
23
-
24
- if (!sessionData.ok) {
25
- const error = await sessionData.json() as { error?: string; message?: string };
26
- return NextResponse.json({error: error.message || "Failed to fetch user data"}, {status: 500});
27
- }
28
-
29
- const userData = await sessionData.json() as {
30
- user: {
31
- id: string;
32
- username: string;
33
- discriminator: string;
34
- avatar: string | null;
35
- email?: string;
36
- }
37
- };
38
-
39
- const session: Session = {
40
- user: {
41
- id: userData.user.id,
42
- name: `${userData.user.username}#${userData.user.discriminator}`,
43
- email: userData.user.email || null,
44
- avatar: `https://cdn.discordapp.com/avatars/${userData.user.id}/${userData.user.avatar}.png`,
45
- },
46
- expires: new Date(Date.now() + response.expiresIn * 1000).toISOString(),
47
- };
48
-
49
- const token = jwt.sign(session, config.jwtSecret, { expiresIn: response.expiresIn });
50
-
51
- await cookieStore.set("AUTH_SESSION", token);
52
- }
8
+ const cookieStore = await cookies();
9
+ const params = new URL(req.url).searchParams;
10
+ const code = params.get("code");
11
+
12
+ if (!code) {
13
+ return NextResponse.json(
14
+ { error: "Authorization code not found" },
15
+ { status: 400 },
16
+ );
17
+ }
18
+
19
+ const response = await ExchangeCodeForTokens(config, code);
20
+
21
+ const sessionData = await fetch("https://discord.com/api/users/@me", {
22
+ headers: {
23
+ Authorization: `Bearer ${response.accessToken}`,
24
+ },
25
+ });
26
+
27
+ if (!sessionData.ok) {
28
+ const error = (await sessionData.json()) as {
29
+ error?: string;
30
+ message?: string;
31
+ };
32
+ return NextResponse.json(
33
+ { error: error.message || "Failed to fetch user data" },
34
+ { status: 500 },
35
+ );
36
+ }
37
+
38
+ const userData = (await sessionData.json()) as {
39
+ user: {
40
+ id: string;
41
+ username: string;
42
+ discriminator: string;
43
+ avatar: string | null;
44
+ email?: string;
45
+ };
46
+ };
47
+
48
+ const session: Session = {
49
+ user: {
50
+ id: userData.user.id,
51
+ name: `${userData.user.username}#${userData.user.discriminator}`,
52
+ email: userData.user.email || null,
53
+ avatar: `https://cdn.discordapp.com/avatars/${userData.user.id}/${userData.user.avatar}.png`,
54
+ },
55
+ expires: new Date(Date.now() + response.expiresIn * 1000).toISOString(),
56
+ };
57
+
58
+ const token = jwt.sign(session, config.jwtSecret, {
59
+ expiresIn: response.expiresIn,
60
+ });
61
+
62
+ await cookieStore.set("AUTH_SESSION", token);
63
+ };
@@ -2,17 +2,20 @@ import type { NextRequest, NextResponse } from "next/server";
2
2
  import type { Session, Config } from "./index";
3
3
  import jwt from "jsonwebtoken";
4
4
 
5
- export const getSession = async (config: Config, req: NextRequest): Promise<Session | null> => {
6
- const token = req.cookies.get("AUTH_SESSION")?.value;
7
- if (!token) {
8
- return null;
9
- }
5
+ export const getSession = async (
6
+ config: Config,
7
+ req: NextRequest,
8
+ ): Promise<Session | null> => {
9
+ const token = req.cookies.get("AUTH_SESSION")?.value;
10
+ if (!token) {
11
+ return null;
12
+ }
10
13
 
11
- try {
12
- const decoded = jwt.verify(token, config.jwtSecret) as Session;
13
- return decoded;
14
- } catch (error) {
15
- console.error("Invalid token:", error);
16
- return null;
17
- }
18
- }
14
+ try {
15
+ const decoded = jwt.verify(token, config.jwtSecret) as Session;
16
+ return decoded;
17
+ } catch (error) {
18
+ console.error("Invalid token:", error);
19
+ return null;
20
+ }
21
+ };
package/tsconfig.json CHANGED
@@ -1,28 +1,28 @@
1
1
  {
2
- "compilerOptions": {
3
- // Environment setup & latest features
4
- "lib": ["ESNext"],
5
- "target": "ESNext",
6
- "module": "ESNext",
7
- "moduleDetection": "force",
8
- "jsx": "react-jsx",
9
- "allowJs": true,
10
- "outDir": "./dist",
11
- "rootDir": "./src",
2
+ "compilerOptions": {
3
+ // Environment setup & latest features
4
+ "lib": ["ESNext"],
5
+ "target": "ESNext",
6
+ "module": "ESNext",
7
+ "moduleDetection": "force",
8
+ "jsx": "react-jsx",
9
+ "allowJs": true,
10
+ "outDir": "./dist",
11
+ "rootDir": "./src",
12
12
 
13
- // Bundler mode
14
- "moduleResolution": "bundler",
15
- "verbatimModuleSyntax": true,
13
+ // Bundler mode
14
+ "moduleResolution": "bundler",
15
+ "verbatimModuleSyntax": true,
16
16
 
17
- // Best practices
18
- "strict": true,
19
- "skipLibCheck": true,
20
- "noFallthroughCasesInSwitch": true,
21
- "noUncheckedIndexedAccess": true,
17
+ // Best practices
18
+ "strict": true,
19
+ "skipLibCheck": true,
20
+ "noFallthroughCasesInSwitch": true,
21
+ "noUncheckedIndexedAccess": true,
22
22
 
23
- // Some stricter flags (disabled by default)
24
- "noUnusedLocals": false,
25
- "noUnusedParameters": false,
26
- "noPropertyAccessFromIndexSignature": false
27
- }
23
+ // Some stricter flags (disabled by default)
24
+ "noUnusedLocals": false,
25
+ "noUnusedParameters": false,
26
+ "noPropertyAccessFromIndexSignature": false
27
+ }
28
28
  }