naystack 1.4.3 → 1.4.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.
- package/dist/auth/constants.cjs.js +30 -0
- package/dist/auth/constants.d.mts +3 -0
- package/dist/auth/constants.d.ts +3 -0
- package/dist/auth/constants.esm.js +5 -0
- package/dist/auth/email/index.cjs.js +20 -4
- package/dist/auth/email/index.d.mts +1 -0
- package/dist/auth/email/index.d.ts +1 -0
- package/dist/auth/email/index.esm.js +19 -4
- package/dist/auth/email/routes/delete.cjs.js +10 -3
- package/dist/auth/email/routes/delete.esm.js +10 -3
- package/dist/auth/email/routes/get.cjs.js +8 -3
- package/dist/auth/email/routes/get.esm.js +8 -3
- package/dist/auth/email/routes/post.cjs.js +9 -2
- package/dist/auth/email/routes/post.esm.js +9 -2
- package/dist/auth/email/routes/put.cjs.js +9 -2
- package/dist/auth/email/routes/put.esm.js +9 -2
- package/dist/auth/email/token.cjs.js +18 -2
- package/dist/auth/email/token.d.mts +2 -1
- package/dist/auth/email/token.d.ts +2 -1
- package/dist/auth/email/token.esm.js +17 -2
- package/dist/auth/email/utils.cjs.js +8 -1
- package/dist/auth/email/utils.esm.js +8 -1
- package/dist/auth/google/get.cjs.js +25 -17
- package/dist/auth/google/get.d.mts +3 -1
- package/dist/auth/google/get.d.ts +3 -1
- package/dist/auth/google/get.esm.js +25 -17
- package/dist/auth/google/index.cjs.js +25 -17
- package/dist/auth/google/index.d.mts +6 -4
- package/dist/auth/google/index.d.ts +6 -4
- package/dist/auth/google/index.esm.js +25 -17
- package/dist/auth/index.cjs.js +38 -21
- package/dist/auth/index.d.mts +1 -0
- package/dist/auth/index.d.ts +1 -0
- package/dist/auth/index.esm.js +37 -21
- package/dist/auth/instagram/index.cjs.js +2 -0
- package/dist/auth/instagram/index.esm.js +2 -0
- package/dist/auth/instagram/route.cjs.js +2 -0
- package/dist/auth/instagram/route.esm.js +2 -0
- package/dist/file/index.cjs.js +8 -1
- package/dist/file/index.esm.js +8 -1
- package/dist/file/put.cjs.js +8 -1
- package/dist/file/put.esm.js +8 -1
- package/dist/file/setup.cjs.js +8 -1
- package/dist/file/setup.esm.js +8 -1
- package/package.json +1 -1
|
@@ -4,7 +4,14 @@ import { verify as verify2 } from "jsonwebtoken";
|
|
|
4
4
|
// src/auth/email/token.ts
|
|
5
5
|
import { compare } from "bcryptjs";
|
|
6
6
|
import { JsonWebTokenError, sign, verify } from "jsonwebtoken";
|
|
7
|
+
import { cookies } from "next/headers";
|
|
8
|
+
import { redirect } from "next/navigation";
|
|
7
9
|
import { NextResponse } from "next/server";
|
|
10
|
+
|
|
11
|
+
// src/auth/constants.ts
|
|
12
|
+
var REFRESH_COOKIE_NAME = "refresh";
|
|
13
|
+
|
|
14
|
+
// src/auth/email/token.ts
|
|
8
15
|
function getUserIdFromRefreshToken(refreshKey, refreshToken) {
|
|
9
16
|
if (refreshToken)
|
|
10
17
|
try {
|
|
@@ -74,7 +81,7 @@ async function verifyCaptcha(token, secret) {
|
|
|
74
81
|
var getContext = (keys, req) => {
|
|
75
82
|
const bearer = req.headers.get("authorization");
|
|
76
83
|
if (!bearer) {
|
|
77
|
-
const refresh = req.cookies.get(
|
|
84
|
+
const refresh = req.cookies.get(REFRESH_COOKIE_NAME)?.value;
|
|
78
85
|
const userId = getUserIdFromRefreshToken(keys.refresh, refresh);
|
|
79
86
|
if (userId) return { userId, isRefreshID: true };
|
|
80
87
|
return { userId: null };
|
|
@@ -30,7 +30,14 @@ var import_uuid = require("uuid");
|
|
|
30
30
|
// src/auth/email/token.ts
|
|
31
31
|
var import_bcryptjs = require("bcryptjs");
|
|
32
32
|
var import_jsonwebtoken = require("jsonwebtoken");
|
|
33
|
+
var import_headers = require("next/headers");
|
|
34
|
+
var import_navigation = require("next/navigation");
|
|
33
35
|
var import_server = require("next/server");
|
|
36
|
+
|
|
37
|
+
// src/auth/constants.ts
|
|
38
|
+
var REFRESH_COOKIE_NAME = "refresh";
|
|
39
|
+
|
|
40
|
+
// src/auth/email/token.ts
|
|
34
41
|
function generateRefreshToken(id, refreshKey) {
|
|
35
42
|
return (0, import_jsonwebtoken.sign)({ id }, refreshKey);
|
|
36
43
|
}
|
|
@@ -38,18 +45,14 @@ function generateRefreshToken(id, refreshKey) {
|
|
|
38
45
|
// src/auth/google/get.ts
|
|
39
46
|
var getGoogleGetRoute = ({
|
|
40
47
|
getUserIdFromEmail,
|
|
41
|
-
|
|
48
|
+
redirectURL,
|
|
42
49
|
errorRedirectURL,
|
|
43
|
-
|
|
50
|
+
url,
|
|
44
51
|
clientId,
|
|
45
52
|
clientSecret,
|
|
46
|
-
|
|
53
|
+
keys
|
|
47
54
|
}) => {
|
|
48
|
-
const oauth2Client = new import_googleapis.google.auth.OAuth2(
|
|
49
|
-
clientId,
|
|
50
|
-
clientSecret,
|
|
51
|
-
authRoute
|
|
52
|
-
);
|
|
55
|
+
const oauth2Client = new import_googleapis.google.auth.OAuth2(clientId, clientSecret, url);
|
|
53
56
|
return async (req) => {
|
|
54
57
|
const code = req.nextUrl.searchParams.get("code");
|
|
55
58
|
const error = req.nextUrl.searchParams.get("error");
|
|
@@ -63,7 +66,7 @@ var getGoogleGetRoute = ({
|
|
|
63
66
|
state: state2,
|
|
64
67
|
include_granted_scopes: true,
|
|
65
68
|
prompt: "consent",
|
|
66
|
-
redirect_uri:
|
|
69
|
+
redirect_uri: url
|
|
67
70
|
});
|
|
68
71
|
const res = import_server2.NextResponse.redirect(authorizationUrl);
|
|
69
72
|
res.cookies.set("state", state2, {
|
|
@@ -72,13 +75,14 @@ var getGoogleGetRoute = ({
|
|
|
72
75
|
});
|
|
73
76
|
return res;
|
|
74
77
|
}
|
|
78
|
+
const errorURL = errorRedirectURL || redirectURL;
|
|
75
79
|
if (error) {
|
|
76
|
-
return import_server2.NextResponse.redirect(
|
|
80
|
+
return import_server2.NextResponse.redirect(errorURL);
|
|
77
81
|
}
|
|
78
82
|
const state = req.nextUrl.searchParams.get("state") || void 0;
|
|
79
83
|
if (code && state) {
|
|
80
84
|
const localState = req.cookies.get("state")?.value;
|
|
81
|
-
if (localState !== state) return import_server2.NextResponse.redirect(
|
|
85
|
+
if (localState !== state) return import_server2.NextResponse.redirect(errorURL);
|
|
82
86
|
const { tokens } = await oauth2Client.getToken(code);
|
|
83
87
|
oauth2Client.setCredentials(tokens);
|
|
84
88
|
const userInfoRequest = await import_googleapis.google.oauth2({
|
|
@@ -88,12 +92,16 @@ var getGoogleGetRoute = ({
|
|
|
88
92
|
const user = userInfoRequest.data;
|
|
89
93
|
if (user.email) {
|
|
90
94
|
const id = await getUserIdFromEmail(user);
|
|
91
|
-
const res = import_server2.NextResponse.redirect(
|
|
95
|
+
const res = import_server2.NextResponse.redirect(redirectURL);
|
|
92
96
|
if (id) {
|
|
93
|
-
res.cookies.set(
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
+
res.cookies.set(
|
|
98
|
+
REFRESH_COOKIE_NAME,
|
|
99
|
+
generateRefreshToken(id, keys.refresh),
|
|
100
|
+
{
|
|
101
|
+
httpOnly: true,
|
|
102
|
+
secure: true
|
|
103
|
+
}
|
|
104
|
+
);
|
|
97
105
|
}
|
|
98
106
|
res.cookies.set("state", "", {
|
|
99
107
|
httpOnly: true,
|
|
@@ -103,7 +111,7 @@ var getGoogleGetRoute = ({
|
|
|
103
111
|
return res;
|
|
104
112
|
}
|
|
105
113
|
}
|
|
106
|
-
return import_server2.NextResponse.redirect(
|
|
114
|
+
return import_server2.NextResponse.redirect(errorURL);
|
|
107
115
|
};
|
|
108
116
|
};
|
|
109
117
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { NextRequest, NextResponse } from 'next/server';
|
|
2
2
|
import { InitGoogleAuthOptions } from './index.mjs';
|
|
3
3
|
import 'googleapis';
|
|
4
|
+
import '../email/types.mjs';
|
|
5
|
+
import '../types.mjs';
|
|
4
6
|
|
|
5
|
-
declare const getGoogleGetRoute: ({ getUserIdFromEmail,
|
|
7
|
+
declare const getGoogleGetRoute: ({ getUserIdFromEmail, redirectURL, errorRedirectURL, url, clientId, clientSecret, keys, }: InitGoogleAuthOptions) => (req: NextRequest) => Promise<NextResponse<unknown>>;
|
|
6
8
|
|
|
7
9
|
export { getGoogleGetRoute };
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { NextRequest, NextResponse } from 'next/server';
|
|
2
2
|
import { InitGoogleAuthOptions } from './index.js';
|
|
3
3
|
import 'googleapis';
|
|
4
|
+
import '../email/types.js';
|
|
5
|
+
import '../types.js';
|
|
4
6
|
|
|
5
|
-
declare const getGoogleGetRoute: ({ getUserIdFromEmail,
|
|
7
|
+
declare const getGoogleGetRoute: ({ getUserIdFromEmail, redirectURL, errorRedirectURL, url, clientId, clientSecret, keys, }: InitGoogleAuthOptions) => (req: NextRequest) => Promise<NextResponse<unknown>>;
|
|
6
8
|
|
|
7
9
|
export { getGoogleGetRoute };
|
|
@@ -6,7 +6,14 @@ import { v4 } from "uuid";
|
|
|
6
6
|
// src/auth/email/token.ts
|
|
7
7
|
import { compare } from "bcryptjs";
|
|
8
8
|
import { JsonWebTokenError, sign, verify } from "jsonwebtoken";
|
|
9
|
+
import { cookies } from "next/headers";
|
|
10
|
+
import { redirect } from "next/navigation";
|
|
9
11
|
import { NextResponse } from "next/server";
|
|
12
|
+
|
|
13
|
+
// src/auth/constants.ts
|
|
14
|
+
var REFRESH_COOKIE_NAME = "refresh";
|
|
15
|
+
|
|
16
|
+
// src/auth/email/token.ts
|
|
10
17
|
function generateRefreshToken(id, refreshKey) {
|
|
11
18
|
return sign({ id }, refreshKey);
|
|
12
19
|
}
|
|
@@ -14,18 +21,14 @@ function generateRefreshToken(id, refreshKey) {
|
|
|
14
21
|
// src/auth/google/get.ts
|
|
15
22
|
var getGoogleGetRoute = ({
|
|
16
23
|
getUserIdFromEmail,
|
|
17
|
-
|
|
24
|
+
redirectURL,
|
|
18
25
|
errorRedirectURL,
|
|
19
|
-
|
|
26
|
+
url,
|
|
20
27
|
clientId,
|
|
21
28
|
clientSecret,
|
|
22
|
-
|
|
29
|
+
keys
|
|
23
30
|
}) => {
|
|
24
|
-
const oauth2Client = new google.auth.OAuth2(
|
|
25
|
-
clientId,
|
|
26
|
-
clientSecret,
|
|
27
|
-
authRoute
|
|
28
|
-
);
|
|
31
|
+
const oauth2Client = new google.auth.OAuth2(clientId, clientSecret, url);
|
|
29
32
|
return async (req) => {
|
|
30
33
|
const code = req.nextUrl.searchParams.get("code");
|
|
31
34
|
const error = req.nextUrl.searchParams.get("error");
|
|
@@ -39,7 +42,7 @@ var getGoogleGetRoute = ({
|
|
|
39
42
|
state: state2,
|
|
40
43
|
include_granted_scopes: true,
|
|
41
44
|
prompt: "consent",
|
|
42
|
-
redirect_uri:
|
|
45
|
+
redirect_uri: url
|
|
43
46
|
});
|
|
44
47
|
const res = NextResponse2.redirect(authorizationUrl);
|
|
45
48
|
res.cookies.set("state", state2, {
|
|
@@ -48,13 +51,14 @@ var getGoogleGetRoute = ({
|
|
|
48
51
|
});
|
|
49
52
|
return res;
|
|
50
53
|
}
|
|
54
|
+
const errorURL = errorRedirectURL || redirectURL;
|
|
51
55
|
if (error) {
|
|
52
|
-
return NextResponse2.redirect(
|
|
56
|
+
return NextResponse2.redirect(errorURL);
|
|
53
57
|
}
|
|
54
58
|
const state = req.nextUrl.searchParams.get("state") || void 0;
|
|
55
59
|
if (code && state) {
|
|
56
60
|
const localState = req.cookies.get("state")?.value;
|
|
57
|
-
if (localState !== state) return NextResponse2.redirect(
|
|
61
|
+
if (localState !== state) return NextResponse2.redirect(errorURL);
|
|
58
62
|
const { tokens } = await oauth2Client.getToken(code);
|
|
59
63
|
oauth2Client.setCredentials(tokens);
|
|
60
64
|
const userInfoRequest = await google.oauth2({
|
|
@@ -64,12 +68,16 @@ var getGoogleGetRoute = ({
|
|
|
64
68
|
const user = userInfoRequest.data;
|
|
65
69
|
if (user.email) {
|
|
66
70
|
const id = await getUserIdFromEmail(user);
|
|
67
|
-
const res = NextResponse2.redirect(
|
|
71
|
+
const res = NextResponse2.redirect(redirectURL);
|
|
68
72
|
if (id) {
|
|
69
|
-
res.cookies.set(
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
+
res.cookies.set(
|
|
74
|
+
REFRESH_COOKIE_NAME,
|
|
75
|
+
generateRefreshToken(id, keys.refresh),
|
|
76
|
+
{
|
|
77
|
+
httpOnly: true,
|
|
78
|
+
secure: true
|
|
79
|
+
}
|
|
80
|
+
);
|
|
73
81
|
}
|
|
74
82
|
res.cookies.set("state", "", {
|
|
75
83
|
httpOnly: true,
|
|
@@ -79,7 +87,7 @@ var getGoogleGetRoute = ({
|
|
|
79
87
|
return res;
|
|
80
88
|
}
|
|
81
89
|
}
|
|
82
|
-
return NextResponse2.redirect(
|
|
90
|
+
return NextResponse2.redirect(errorURL);
|
|
83
91
|
};
|
|
84
92
|
};
|
|
85
93
|
export {
|
|
@@ -32,7 +32,14 @@ var import_uuid = require("uuid");
|
|
|
32
32
|
// src/auth/email/token.ts
|
|
33
33
|
var import_bcryptjs = require("bcryptjs");
|
|
34
34
|
var import_jsonwebtoken = require("jsonwebtoken");
|
|
35
|
+
var import_headers = require("next/headers");
|
|
36
|
+
var import_navigation = require("next/navigation");
|
|
35
37
|
var import_server = require("next/server");
|
|
38
|
+
|
|
39
|
+
// src/auth/constants.ts
|
|
40
|
+
var REFRESH_COOKIE_NAME = "refresh";
|
|
41
|
+
|
|
42
|
+
// src/auth/email/token.ts
|
|
36
43
|
function generateRefreshToken(id, refreshKey) {
|
|
37
44
|
return (0, import_jsonwebtoken.sign)({ id }, refreshKey);
|
|
38
45
|
}
|
|
@@ -40,18 +47,14 @@ function generateRefreshToken(id, refreshKey) {
|
|
|
40
47
|
// src/auth/google/get.ts
|
|
41
48
|
var getGoogleGetRoute = ({
|
|
42
49
|
getUserIdFromEmail,
|
|
43
|
-
|
|
50
|
+
redirectURL,
|
|
44
51
|
errorRedirectURL,
|
|
45
|
-
|
|
52
|
+
url,
|
|
46
53
|
clientId,
|
|
47
54
|
clientSecret,
|
|
48
|
-
|
|
55
|
+
keys
|
|
49
56
|
}) => {
|
|
50
|
-
const oauth2Client = new import_googleapis.google.auth.OAuth2(
|
|
51
|
-
clientId,
|
|
52
|
-
clientSecret,
|
|
53
|
-
authRoute
|
|
54
|
-
);
|
|
57
|
+
const oauth2Client = new import_googleapis.google.auth.OAuth2(clientId, clientSecret, url);
|
|
55
58
|
return async (req) => {
|
|
56
59
|
const code = req.nextUrl.searchParams.get("code");
|
|
57
60
|
const error = req.nextUrl.searchParams.get("error");
|
|
@@ -65,7 +68,7 @@ var getGoogleGetRoute = ({
|
|
|
65
68
|
state: state2,
|
|
66
69
|
include_granted_scopes: true,
|
|
67
70
|
prompt: "consent",
|
|
68
|
-
redirect_uri:
|
|
71
|
+
redirect_uri: url
|
|
69
72
|
});
|
|
70
73
|
const res = import_server2.NextResponse.redirect(authorizationUrl);
|
|
71
74
|
res.cookies.set("state", state2, {
|
|
@@ -74,13 +77,14 @@ var getGoogleGetRoute = ({
|
|
|
74
77
|
});
|
|
75
78
|
return res;
|
|
76
79
|
}
|
|
80
|
+
const errorURL = errorRedirectURL || redirectURL;
|
|
77
81
|
if (error) {
|
|
78
|
-
return import_server2.NextResponse.redirect(
|
|
82
|
+
return import_server2.NextResponse.redirect(errorURL);
|
|
79
83
|
}
|
|
80
84
|
const state = req.nextUrl.searchParams.get("state") || void 0;
|
|
81
85
|
if (code && state) {
|
|
82
86
|
const localState = req.cookies.get("state")?.value;
|
|
83
|
-
if (localState !== state) return import_server2.NextResponse.redirect(
|
|
87
|
+
if (localState !== state) return import_server2.NextResponse.redirect(errorURL);
|
|
84
88
|
const { tokens } = await oauth2Client.getToken(code);
|
|
85
89
|
oauth2Client.setCredentials(tokens);
|
|
86
90
|
const userInfoRequest = await import_googleapis.google.oauth2({
|
|
@@ -90,12 +94,16 @@ var getGoogleGetRoute = ({
|
|
|
90
94
|
const user = userInfoRequest.data;
|
|
91
95
|
if (user.email) {
|
|
92
96
|
const id = await getUserIdFromEmail(user);
|
|
93
|
-
const res = import_server2.NextResponse.redirect(
|
|
97
|
+
const res = import_server2.NextResponse.redirect(redirectURL);
|
|
94
98
|
if (id) {
|
|
95
|
-
res.cookies.set(
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
+
res.cookies.set(
|
|
100
|
+
REFRESH_COOKIE_NAME,
|
|
101
|
+
generateRefreshToken(id, keys.refresh),
|
|
102
|
+
{
|
|
103
|
+
httpOnly: true,
|
|
104
|
+
secure: true
|
|
105
|
+
}
|
|
106
|
+
);
|
|
99
107
|
}
|
|
100
108
|
res.cookies.set("state", "", {
|
|
101
109
|
httpOnly: true,
|
|
@@ -105,7 +113,7 @@ var getGoogleGetRoute = ({
|
|
|
105
113
|
return res;
|
|
106
114
|
}
|
|
107
115
|
}
|
|
108
|
-
return import_server2.NextResponse.redirect(
|
|
116
|
+
return import_server2.NextResponse.redirect(errorURL);
|
|
109
117
|
};
|
|
110
118
|
};
|
|
111
119
|
|
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
import * as next_server from 'next/server';
|
|
2
2
|
import { oauth2_v2 } from 'googleapis';
|
|
3
|
+
import { AuthKeys } from '../email/types.mjs';
|
|
4
|
+
import '../types.mjs';
|
|
3
5
|
|
|
4
6
|
type Schema$Userinfo = oauth2_v2.Schema$Userinfo;
|
|
5
7
|
interface InitGoogleAuthOptions {
|
|
6
8
|
getUserIdFromEmail: (email: Schema$Userinfo) => Promise<number | null>;
|
|
7
|
-
|
|
8
|
-
errorRedirectURL
|
|
9
|
-
|
|
9
|
+
redirectURL: string;
|
|
10
|
+
errorRedirectURL?: string;
|
|
11
|
+
url: string;
|
|
10
12
|
clientId: string;
|
|
11
13
|
clientSecret: string;
|
|
12
|
-
|
|
14
|
+
keys: AuthKeys;
|
|
13
15
|
}
|
|
14
16
|
declare function initGoogleAuth(props: InitGoogleAuthOptions): {
|
|
15
17
|
GET: (req: next_server.NextRequest) => Promise<next_server.NextResponse<unknown>>;
|
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
import * as next_server from 'next/server';
|
|
2
2
|
import { oauth2_v2 } from 'googleapis';
|
|
3
|
+
import { AuthKeys } from '../email/types.js';
|
|
4
|
+
import '../types.js';
|
|
3
5
|
|
|
4
6
|
type Schema$Userinfo = oauth2_v2.Schema$Userinfo;
|
|
5
7
|
interface InitGoogleAuthOptions {
|
|
6
8
|
getUserIdFromEmail: (email: Schema$Userinfo) => Promise<number | null>;
|
|
7
|
-
|
|
8
|
-
errorRedirectURL
|
|
9
|
-
|
|
9
|
+
redirectURL: string;
|
|
10
|
+
errorRedirectURL?: string;
|
|
11
|
+
url: string;
|
|
10
12
|
clientId: string;
|
|
11
13
|
clientSecret: string;
|
|
12
|
-
|
|
14
|
+
keys: AuthKeys;
|
|
13
15
|
}
|
|
14
16
|
declare function initGoogleAuth(props: InitGoogleAuthOptions): {
|
|
15
17
|
GET: (req: next_server.NextRequest) => Promise<next_server.NextResponse<unknown>>;
|
|
@@ -6,7 +6,14 @@ import { v4 } from "uuid";
|
|
|
6
6
|
// src/auth/email/token.ts
|
|
7
7
|
import { compare } from "bcryptjs";
|
|
8
8
|
import { JsonWebTokenError, sign, verify } from "jsonwebtoken";
|
|
9
|
+
import { cookies } from "next/headers";
|
|
10
|
+
import { redirect } from "next/navigation";
|
|
9
11
|
import { NextResponse } from "next/server";
|
|
12
|
+
|
|
13
|
+
// src/auth/constants.ts
|
|
14
|
+
var REFRESH_COOKIE_NAME = "refresh";
|
|
15
|
+
|
|
16
|
+
// src/auth/email/token.ts
|
|
10
17
|
function generateRefreshToken(id, refreshKey) {
|
|
11
18
|
return sign({ id }, refreshKey);
|
|
12
19
|
}
|
|
@@ -14,18 +21,14 @@ function generateRefreshToken(id, refreshKey) {
|
|
|
14
21
|
// src/auth/google/get.ts
|
|
15
22
|
var getGoogleGetRoute = ({
|
|
16
23
|
getUserIdFromEmail,
|
|
17
|
-
|
|
24
|
+
redirectURL,
|
|
18
25
|
errorRedirectURL,
|
|
19
|
-
|
|
26
|
+
url,
|
|
20
27
|
clientId,
|
|
21
28
|
clientSecret,
|
|
22
|
-
|
|
29
|
+
keys
|
|
23
30
|
}) => {
|
|
24
|
-
const oauth2Client = new google.auth.OAuth2(
|
|
25
|
-
clientId,
|
|
26
|
-
clientSecret,
|
|
27
|
-
authRoute
|
|
28
|
-
);
|
|
31
|
+
const oauth2Client = new google.auth.OAuth2(clientId, clientSecret, url);
|
|
29
32
|
return async (req) => {
|
|
30
33
|
const code = req.nextUrl.searchParams.get("code");
|
|
31
34
|
const error = req.nextUrl.searchParams.get("error");
|
|
@@ -39,7 +42,7 @@ var getGoogleGetRoute = ({
|
|
|
39
42
|
state: state2,
|
|
40
43
|
include_granted_scopes: true,
|
|
41
44
|
prompt: "consent",
|
|
42
|
-
redirect_uri:
|
|
45
|
+
redirect_uri: url
|
|
43
46
|
});
|
|
44
47
|
const res = NextResponse2.redirect(authorizationUrl);
|
|
45
48
|
res.cookies.set("state", state2, {
|
|
@@ -48,13 +51,14 @@ var getGoogleGetRoute = ({
|
|
|
48
51
|
});
|
|
49
52
|
return res;
|
|
50
53
|
}
|
|
54
|
+
const errorURL = errorRedirectURL || redirectURL;
|
|
51
55
|
if (error) {
|
|
52
|
-
return NextResponse2.redirect(
|
|
56
|
+
return NextResponse2.redirect(errorURL);
|
|
53
57
|
}
|
|
54
58
|
const state = req.nextUrl.searchParams.get("state") || void 0;
|
|
55
59
|
if (code && state) {
|
|
56
60
|
const localState = req.cookies.get("state")?.value;
|
|
57
|
-
if (localState !== state) return NextResponse2.redirect(
|
|
61
|
+
if (localState !== state) return NextResponse2.redirect(errorURL);
|
|
58
62
|
const { tokens } = await oauth2Client.getToken(code);
|
|
59
63
|
oauth2Client.setCredentials(tokens);
|
|
60
64
|
const userInfoRequest = await google.oauth2({
|
|
@@ -64,12 +68,16 @@ var getGoogleGetRoute = ({
|
|
|
64
68
|
const user = userInfoRequest.data;
|
|
65
69
|
if (user.email) {
|
|
66
70
|
const id = await getUserIdFromEmail(user);
|
|
67
|
-
const res = NextResponse2.redirect(
|
|
71
|
+
const res = NextResponse2.redirect(redirectURL);
|
|
68
72
|
if (id) {
|
|
69
|
-
res.cookies.set(
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
+
res.cookies.set(
|
|
74
|
+
REFRESH_COOKIE_NAME,
|
|
75
|
+
generateRefreshToken(id, keys.refresh),
|
|
76
|
+
{
|
|
77
|
+
httpOnly: true,
|
|
78
|
+
secure: true
|
|
79
|
+
}
|
|
80
|
+
);
|
|
73
81
|
}
|
|
74
82
|
res.cookies.set("state", "", {
|
|
75
83
|
httpOnly: true,
|
|
@@ -79,7 +87,7 @@ var getGoogleGetRoute = ({
|
|
|
79
87
|
return res;
|
|
80
88
|
}
|
|
81
89
|
}
|
|
82
|
-
return NextResponse2.redirect(
|
|
90
|
+
return NextResponse2.redirect(errorURL);
|
|
83
91
|
};
|
|
84
92
|
};
|
|
85
93
|
|
package/dist/auth/index.cjs.js
CHANGED
|
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/auth/index.ts
|
|
21
21
|
var auth_exports = {};
|
|
22
22
|
__export(auth_exports, {
|
|
23
|
+
checkAuthStatus: () => checkAuthStatus,
|
|
23
24
|
getEmailAuthRoutes: () => getEmailAuthRoutes,
|
|
24
25
|
initGoogleAuth: () => initGoogleAuth,
|
|
25
26
|
initInstagramAuth: () => initInstagramAuth
|
|
@@ -32,7 +33,14 @@ var import_jsonwebtoken2 = require("jsonwebtoken");
|
|
|
32
33
|
// src/auth/email/token.ts
|
|
33
34
|
var import_bcryptjs = require("bcryptjs");
|
|
34
35
|
var import_jsonwebtoken = require("jsonwebtoken");
|
|
36
|
+
var import_headers = require("next/headers");
|
|
37
|
+
var import_navigation = require("next/navigation");
|
|
35
38
|
var import_server = require("next/server");
|
|
39
|
+
|
|
40
|
+
// src/auth/constants.ts
|
|
41
|
+
var REFRESH_COOKIE_NAME = "refresh";
|
|
42
|
+
|
|
43
|
+
// src/auth/email/token.ts
|
|
36
44
|
function generateAccessToken(id, signingKey) {
|
|
37
45
|
return (0, import_jsonwebtoken.sign)({ id }, signingKey, {
|
|
38
46
|
expiresIn: "2h"
|
|
@@ -47,14 +55,14 @@ function getTokenizedResponse(accessToken, refreshToken) {
|
|
|
47
55
|
status: 200
|
|
48
56
|
});
|
|
49
57
|
if (!accessToken) {
|
|
50
|
-
response.cookies.set(
|
|
58
|
+
response.cookies.set(REFRESH_COOKIE_NAME, "", {
|
|
51
59
|
secure: false,
|
|
52
60
|
httpOnly: true,
|
|
53
61
|
expires: 0
|
|
54
62
|
});
|
|
55
63
|
}
|
|
56
64
|
if (refreshToken !== void 0) {
|
|
57
|
-
response.cookies.set(
|
|
65
|
+
response.cookies.set(REFRESH_COOKIE_NAME, refreshToken, {
|
|
58
66
|
secure: false,
|
|
59
67
|
httpOnly: true,
|
|
60
68
|
expires: refreshToken === "" ? 0 : new Date(Date.now() + 60 * 60 * 24 * 365 * 1e3)
|
|
@@ -90,6 +98,13 @@ function verifyUser(user, password) {
|
|
|
90
98
|
if (!user.password) return false;
|
|
91
99
|
return (0, import_bcryptjs.compare)(password, user.password);
|
|
92
100
|
}
|
|
101
|
+
async function checkAuthStatus(redirectUnauthorizedURL) {
|
|
102
|
+
const Cookie = await (0, import_headers.cookies)();
|
|
103
|
+
const isAuthorized = !!Cookie.get(REFRESH_COOKIE_NAME)?.value;
|
|
104
|
+
if (!isAuthorized && redirectUnauthorizedURL)
|
|
105
|
+
return (0, import_navigation.redirect)(redirectUnauthorizedURL);
|
|
106
|
+
return isAuthorized;
|
|
107
|
+
}
|
|
93
108
|
|
|
94
109
|
// src/auth/utils/errors.ts
|
|
95
110
|
var import_server2 = require("next/server");
|
|
@@ -147,7 +162,7 @@ async function verifyCaptcha(token, secret) {
|
|
|
147
162
|
var getContext = (keys, req) => {
|
|
148
163
|
const bearer = req.headers.get("authorization");
|
|
149
164
|
if (!bearer) {
|
|
150
|
-
const refresh = req.cookies.get(
|
|
165
|
+
const refresh = req.cookies.get(REFRESH_COOKIE_NAME)?.value;
|
|
151
166
|
const userId = getUserIdFromRefreshToken(keys.refresh, refresh);
|
|
152
167
|
if (userId) return { userId, isRefreshID: true };
|
|
153
168
|
return { userId: null };
|
|
@@ -177,7 +192,7 @@ var getDeleteRoute = (options) => async (req) => {
|
|
|
177
192
|
|
|
178
193
|
// src/auth/email/routes/get.ts
|
|
179
194
|
var getGetRoute = (options) => async (req) => {
|
|
180
|
-
const refresh = req.cookies.get(
|
|
195
|
+
const refresh = req.cookies.get(REFRESH_COOKIE_NAME)?.value;
|
|
181
196
|
const userID = getUserIdFromRefreshToken(options.keys.refresh, refresh);
|
|
182
197
|
if (userID) {
|
|
183
198
|
if (options.onRefresh) {
|
|
@@ -258,18 +273,14 @@ var import_server3 = require("next/server");
|
|
|
258
273
|
var import_uuid = require("uuid");
|
|
259
274
|
var getGoogleGetRoute = ({
|
|
260
275
|
getUserIdFromEmail,
|
|
261
|
-
|
|
276
|
+
redirectURL,
|
|
262
277
|
errorRedirectURL,
|
|
263
|
-
|
|
278
|
+
url,
|
|
264
279
|
clientId,
|
|
265
280
|
clientSecret,
|
|
266
|
-
|
|
281
|
+
keys
|
|
267
282
|
}) => {
|
|
268
|
-
const oauth2Client = new import_googleapis.google.auth.OAuth2(
|
|
269
|
-
clientId,
|
|
270
|
-
clientSecret,
|
|
271
|
-
authRoute
|
|
272
|
-
);
|
|
283
|
+
const oauth2Client = new import_googleapis.google.auth.OAuth2(clientId, clientSecret, url);
|
|
273
284
|
return async (req) => {
|
|
274
285
|
const code = req.nextUrl.searchParams.get("code");
|
|
275
286
|
const error = req.nextUrl.searchParams.get("error");
|
|
@@ -283,7 +294,7 @@ var getGoogleGetRoute = ({
|
|
|
283
294
|
state: state2,
|
|
284
295
|
include_granted_scopes: true,
|
|
285
296
|
prompt: "consent",
|
|
286
|
-
redirect_uri:
|
|
297
|
+
redirect_uri: url
|
|
287
298
|
});
|
|
288
299
|
const res = import_server3.NextResponse.redirect(authorizationUrl);
|
|
289
300
|
res.cookies.set("state", state2, {
|
|
@@ -292,13 +303,14 @@ var getGoogleGetRoute = ({
|
|
|
292
303
|
});
|
|
293
304
|
return res;
|
|
294
305
|
}
|
|
306
|
+
const errorURL = errorRedirectURL || redirectURL;
|
|
295
307
|
if (error) {
|
|
296
|
-
return import_server3.NextResponse.redirect(
|
|
308
|
+
return import_server3.NextResponse.redirect(errorURL);
|
|
297
309
|
}
|
|
298
310
|
const state = req.nextUrl.searchParams.get("state") || void 0;
|
|
299
311
|
if (code && state) {
|
|
300
312
|
const localState = req.cookies.get("state")?.value;
|
|
301
|
-
if (localState !== state) return import_server3.NextResponse.redirect(
|
|
313
|
+
if (localState !== state) return import_server3.NextResponse.redirect(errorURL);
|
|
302
314
|
const { tokens } = await oauth2Client.getToken(code);
|
|
303
315
|
oauth2Client.setCredentials(tokens);
|
|
304
316
|
const userInfoRequest = await import_googleapis.google.oauth2({
|
|
@@ -308,12 +320,16 @@ var getGoogleGetRoute = ({
|
|
|
308
320
|
const user = userInfoRequest.data;
|
|
309
321
|
if (user.email) {
|
|
310
322
|
const id = await getUserIdFromEmail(user);
|
|
311
|
-
const res = import_server3.NextResponse.redirect(
|
|
323
|
+
const res = import_server3.NextResponse.redirect(redirectURL);
|
|
312
324
|
if (id) {
|
|
313
|
-
res.cookies.set(
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
325
|
+
res.cookies.set(
|
|
326
|
+
REFRESH_COOKIE_NAME,
|
|
327
|
+
generateRefreshToken(id, keys.refresh),
|
|
328
|
+
{
|
|
329
|
+
httpOnly: true,
|
|
330
|
+
secure: true
|
|
331
|
+
}
|
|
332
|
+
);
|
|
317
333
|
}
|
|
318
334
|
res.cookies.set("state", "", {
|
|
319
335
|
httpOnly: true,
|
|
@@ -323,7 +339,7 @@ var getGoogleGetRoute = ({
|
|
|
323
339
|
return res;
|
|
324
340
|
}
|
|
325
341
|
}
|
|
326
|
-
return import_server3.NextResponse.redirect(
|
|
342
|
+
return import_server3.NextResponse.redirect(errorURL);
|
|
327
343
|
};
|
|
328
344
|
};
|
|
329
345
|
|
|
@@ -445,6 +461,7 @@ function initInstagramAuth(props) {
|
|
|
445
461
|
}
|
|
446
462
|
// Annotate the CommonJS export names for ESM import in node:
|
|
447
463
|
0 && (module.exports = {
|
|
464
|
+
checkAuthStatus,
|
|
448
465
|
getEmailAuthRoutes,
|
|
449
466
|
initGoogleAuth,
|
|
450
467
|
initInstagramAuth
|
package/dist/auth/index.d.mts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export { getEmailAuthRoutes } from './email/index.mjs';
|
|
2
2
|
export { initGoogleAuth } from './google/index.mjs';
|
|
3
3
|
export { initInstagramAuth } from './instagram/index.mjs';
|
|
4
|
+
export { checkAuthStatus } from './email/token.mjs';
|
|
4
5
|
import 'next/server';
|
|
5
6
|
import '../graphql/types.mjs';
|
|
6
7
|
import './email/types.mjs';
|
package/dist/auth/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export { getEmailAuthRoutes } from './email/index.js';
|
|
2
2
|
export { initGoogleAuth } from './google/index.js';
|
|
3
3
|
export { initInstagramAuth } from './instagram/index.js';
|
|
4
|
+
export { checkAuthStatus } from './email/token.js';
|
|
4
5
|
import 'next/server';
|
|
5
6
|
import '../graphql/types.js';
|
|
6
7
|
import './email/types.js';
|