@mikandev/next-discord-auth 0.0.6 → 0.0.7

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
@@ -1 +1,10 @@
1
- export {};
1
+ let globalConfig = null;
2
+ export function setup(config) {
3
+ globalConfig = config;
4
+ }
5
+ export function getGlobalConfig() {
6
+ if (!globalConfig) {
7
+ throw new Error("Global config has not been set.");
8
+ }
9
+ return globalConfig;
10
+ }
package/dist/redirect.js CHANGED
@@ -24,7 +24,7 @@ export const handleRedirect = async (config, req) => {
24
24
  user: {
25
25
  id: userData.user.id,
26
26
  name: `${userData.user.username}#${userData.user.discriminator}`,
27
- email: userData.user.email || null,
27
+ email: userData.user.email,
28
28
  avatar: `https://cdn.discordapp.com/avatars/${userData.user.id}/${userData.user.avatar}.png`,
29
29
  },
30
30
  expires: new Date(Date.now() + response.expiresIn * 1000).toISOString(),
@@ -1,3 +1,4 @@
1
+ import { NextRequest, NextResponse } from "next/server";
1
2
  import jwt from "jsonwebtoken";
2
3
  export const getSession = async (config, req) => {
3
4
  const token = req.cookies.get("AUTH_SESSION")?.value;
@@ -5,11 +6,27 @@ export const getSession = async (config, req) => {
5
6
  return null;
6
7
  }
7
8
  try {
8
- const decoded = jwt.verify(token, config.jwtSecret);
9
- return decoded;
9
+ return jwt.verify(token, config.jwtSecret);
10
10
  }
11
11
  catch (error) {
12
12
  console.error("Invalid token:", error);
13
13
  return null;
14
14
  }
15
15
  };
16
+ export const signIn = async (config, req) => {
17
+ const session = await getSession(config, req);
18
+ if (session) {
19
+ return NextResponse.json({ message: "Already signed in" }, { status: 200 });
20
+ }
21
+ 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
+ return NextResponse.redirect(signInURL, 302);
23
+ };
24
+ export const signOut = async (config, req) => {
25
+ const session = await getSession(config, req);
26
+ if (!session) {
27
+ return NextResponse.json({ message: "Not signed in" }, { status: 401 });
28
+ }
29
+ const response = NextResponse.json({ message: "Signed out successfully" }, { status: 200 });
30
+ response.cookies.delete("AUTH_SESSION");
31
+ return response;
32
+ };
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "main": "dist/index.js",
6
6
  "files": ["dist"],
7
7
  "license": "WTFPL",
8
- "version": "0.0.6",
8
+ "version": "0.0.7",
9
9
  "type": "module",
10
10
  "repository": {
11
11
  "type": "git",