@next-nest-auth/nextauth 0.1.9 → 0.2.1

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/package.json CHANGED
@@ -1,52 +1,52 @@
1
1
  {
2
- "name": "@next-nest-auth/nextauth",
3
- "version": "0.1.9",
4
- "main": "dist/index.js",
5
- "types": "dist/index.d.ts",
6
- "scripts": {
7
- "dev": "tsc -w",
8
- "build": "tsc"
9
- },
10
- "keywords": [
11
- "nextjs",
12
- "authentication",
13
- "nextauth",
14
- "auth",
15
- "jwt",
16
- "token authentication",
17
- "refresh tokens",
18
- "secure authentication",
19
- "session management",
20
- "nestjs integration",
21
- "nextjs authentication",
22
- "nextjs auth",
23
- "cookie authentication",
24
- "login",
25
- "multi-provider authentication",
26
- "nextjs login",
27
- "user authentication"
28
- ],
29
- "author": "Md Shafkat Hussain Tanvir <tanvir0604@gmail.com>",
30
- "repository": "https://github.com/tanvir0604/nextauth",
31
- "bugs": "https://github.com/tanvir0604/nextauth/issues",
32
- "license": "MIT",
33
- "type": "commonjs",
34
- "description": "NextAuth is a frontend authentication package designed for Next.js applications, providing easy integration with NestJS-based backends. It supports login, session management, and token handling (including JWT and refresh tokens) to ensure secure user authentication. With customizable authentication flows and compatibility with multiple providers, NextAuth enables seamless integration between NestJS and Next.js apps.",
35
- "dependencies": {
36
- "axios": "^1.13.2",
37
- "js-cookie": "^3.0.5",
38
- "jsonwebtoken": "^9.0.3",
39
- "jwt-decode": "^4.0.0",
40
- "next": "^16.1.1"
41
- },
42
- "devDependencies": {
43
- "@types/js-cookie": "^3.0.6",
44
- "@types/node": "^25.0.3",
45
- "@types/react": "^19.2.7",
46
- "typescript": "^5.9.3"
47
- },
48
- "files": [
49
- "dist/",
50
- "src/"
51
- ]
2
+ "name": "@next-nest-auth/nextauth",
3
+ "version": "0.2.1",
4
+ "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
+ "scripts": {
7
+ "dev": "tsc -w",
8
+ "build": "tsc"
9
+ },
10
+ "keywords": [
11
+ "nextjs",
12
+ "authentication",
13
+ "nextauth",
14
+ "auth",
15
+ "jwt",
16
+ "token authentication",
17
+ "refresh tokens",
18
+ "secure authentication",
19
+ "session management",
20
+ "nestjs integration",
21
+ "nextjs authentication",
22
+ "nextjs auth",
23
+ "cookie authentication",
24
+ "login",
25
+ "multi-provider authentication",
26
+ "nextjs login",
27
+ "user authentication"
28
+ ],
29
+ "author": "Md Shafkat Hussain Tanvir <tanvir0604@gmail.com>",
30
+ "repository": "https://github.com/tanvir0604/nextauth",
31
+ "bugs": "https://github.com/tanvir0604/nextauth/issues",
32
+ "license": "MIT",
33
+ "type": "commonjs",
34
+ "description": "NextAuth is a frontend authentication package designed for Next.js applications, providing easy integration with NestJS-based backends. It supports login, session management, and token handling (including JWT and refresh tokens) to ensure secure user authentication. With customizable authentication flows and compatibility with multiple providers, NextAuth enables seamless integration between NestJS and Next.js apps.",
35
+ "dependencies": {
36
+ "axios": "^1.13.2",
37
+ "js-cookie": "^3.0.5",
38
+ "jsonwebtoken": "^9.0.3",
39
+ "jwt-decode": "^4.0.0",
40
+ "next": "^16.1.1"
41
+ },
42
+ "devDependencies": {
43
+ "@types/js-cookie": "^3.0.6",
44
+ "@types/node": "^25.0.3",
45
+ "@types/react": "^19.2.7",
46
+ "typescript": "^5.9.3"
47
+ },
48
+ "files": [
49
+ "dist/",
50
+ "src/"
51
+ ]
52
52
  }
package/src/auth.ts CHANGED
@@ -1,217 +1,232 @@
1
- import axios from 'axios';
2
- import { jwtDecode } from 'jwt-decode';
3
- import { cookies } from 'next/headers';
4
- import { NextRequest, NextResponse } from 'next/server';
1
+ import axios from "axios";
2
+ import { jwtDecode } from "jwt-decode";
3
+ import { cookies } from "next/headers";
4
+ import { NextRequest, NextResponse } from "next/server";
5
5
 
6
6
  const API_URL = process.env.API_BASE_URL || process.env.NEXT_PUBLIC_API_URL;
7
7
 
8
8
  interface TokenResponse {
9
- accessToken: string;
10
- refreshToken: string;
11
- accessTokenExpiresIn: string;
12
- refreshTokenExpiresIn: string;
9
+ accessToken: string;
10
+ refreshToken: string;
11
+ accessTokenExpiresIn: string;
12
+ refreshTokenExpiresIn: string;
13
13
  }
14
14
 
15
15
  interface User {
16
- id: string;
17
- name: string;
18
- email: string;
19
- mobile: string;
20
- role: string;
21
- pic: string;
22
- macId: string;
23
- [key: string]: any;
16
+ sub: string;
17
+ name: string;
18
+ email: string;
19
+ mobile: string;
20
+ role: string;
21
+ pic: string;
22
+ macId: string;
23
+ [key: string]: any;
24
24
  }
25
25
 
26
26
  const convertToSeconds = (expiresIn: string) => {
27
- const match = expiresIn.match(/(\d+)([mhd])/);
28
- if (!match) return 0;
29
-
30
- const value = parseInt(match[1], 10);
31
- const unit = match[2];
32
-
33
- switch (unit) {
34
- case 'm':
35
- return value * 60;
36
- case 'h':
37
- return value * 60 * 60;
38
- case 'd':
39
- return value * 60 * 60 * 24;
40
- default:
41
- return 0;
42
- }
27
+ const match = expiresIn.match(/(\d+)([mhd])/);
28
+ if (!match) return 0;
29
+
30
+ const value = parseInt(match[1], 10);
31
+ const unit = match[2];
32
+
33
+ switch (unit) {
34
+ case "m":
35
+ return value * 60;
36
+ case "h":
37
+ return value * 60 * 60;
38
+ case "d":
39
+ return value * 60 * 60 * 24;
40
+ default:
41
+ return 0;
42
+ }
43
43
  };
44
44
 
45
45
  export async function refreshToken(req: NextRequest) {
46
- try {
47
- const refreshToken = req.cookies.get('refresh_token')?.value;
48
- if (!refreshToken) {
49
- throw new Error('Token refresh failed, no refresh token');
50
- }
51
- const response: TokenResponse = await post(
52
- `${API_URL}/nestauth/refresh-token`,
53
- {
54
- refresh_token: refreshToken,
55
- },
56
- {},
57
- false
58
- );
59
-
60
- if (!response || !response.accessToken || !response.refreshToken) {
61
- throw new Error('Token refresh failed, no response from api');
62
- }
63
-
64
- const res = NextResponse.next();
65
- res.cookies.set('access_token', response.accessToken, {
66
- httpOnly: true,
67
- secure: process.env.NODE_ENV === 'production',
68
- sameSite: 'strict',
69
- path: '/',
70
- maxAge: convertToSeconds(response.accessTokenExpiresIn ?? ''),
71
- });
72
-
73
- if (!process.env.AUTOEXPIRE_REFRESH_TOKEN) {
74
- if (process.env.NODE_ENV === 'development') {
75
- console.log('refresh token is not expired and updating expires in');
76
- }
77
- res.cookies.set('refresh_token', response.refreshToken, {
78
- httpOnly: true,
79
- secure: process.env.NODE_ENV === 'production',
80
- sameSite: 'strict',
81
- path: '/',
82
- maxAge: convertToSeconds(response.refreshTokenExpiresIn ?? ''),
83
- });
84
- }
85
-
86
- return res;
87
- } catch (error) {
88
- throw new Error(error);
46
+ try {
47
+ const refreshToken = req.cookies.get("refresh_token")?.value;
48
+ if (!refreshToken) {
49
+ throw new Error("Token refresh failed, no refresh token");
50
+ }
51
+ const response: TokenResponse = await post(
52
+ `${API_URL}/nestauth/refresh-token`,
53
+ {
54
+ refresh_token: refreshToken,
55
+ },
56
+ {},
57
+ false,
58
+ );
59
+
60
+ if (!response || !response.accessToken || !response.refreshToken) {
61
+ throw new Error("Token refresh failed, no response from api");
89
62
  }
63
+
64
+ const res = NextResponse.next();
65
+ res.cookies.set("access_token", response.accessToken, {
66
+ httpOnly: true,
67
+ secure: process.env.NODE_ENV === "production",
68
+ sameSite: "lax",
69
+ path: "/",
70
+ maxAge: convertToSeconds(response.accessTokenExpiresIn ?? ""),
71
+ });
72
+
73
+ if (!process.env.AUTOEXPIRE_REFRESH_TOKEN) {
74
+ if (process.env.NODE_ENV === "development") {
75
+ console.log("refresh token is not expired and updating expires in");
76
+ }
77
+ res.cookies.set("refresh_token", response.refreshToken, {
78
+ httpOnly: true,
79
+ secure: process.env.NODE_ENV === "production",
80
+ sameSite: "lax",
81
+ path: "/",
82
+ maxAge: convertToSeconds(response.refreshTokenExpiresIn ?? ""),
83
+ });
84
+ }
85
+
86
+ return res;
87
+ } catch (error: any) {
88
+ throw new Error(error?.message ?? "Token refresh failed");
89
+ }
90
90
  }
91
91
 
92
92
  export async function authenticate(params: any) {
93
- try {
94
- const response: TokenResponse = await post(`${API_URL}/nestauth/login`, params, {}, false);
95
- if (
96
- !response ||
97
- !response.accessToken ||
98
- !response.refreshToken ||
99
- !response.accessTokenExpiresIn ||
100
- !response.refreshTokenExpiresIn
101
- ) {
102
- throw new Error(
103
- 'Login failed' +
104
- ' API URL: ' +
105
- API_URL +
106
- ' params: ' +
107
- JSON.stringify(params) +
108
- ' response: ' +
109
- JSON.stringify(response)
110
- );
111
- }
112
- const cookieStore = await cookies();
113
- cookieStore.set('access_token', response.accessToken, {
114
- httpOnly: true,
115
- secure: process.env.NODE_ENV === 'production',
116
- sameSite: 'strict',
117
- path: '/',
118
- maxAge: convertToSeconds(response.accessTokenExpiresIn ?? ''),
119
- });
120
-
121
- cookieStore.set('refresh_token', response.refreshToken, {
122
- httpOnly: true,
123
- secure: process.env.NODE_ENV === 'production',
124
- sameSite: 'strict',
125
- path: '/',
126
- maxAge: convertToSeconds(response.refreshTokenExpiresIn ?? ''),
127
- });
128
-
129
- return response;
130
- } catch (error) {
131
- // console.log(error);
132
- throw new Error(error);
93
+ try {
94
+ const response: TokenResponse = await post(
95
+ `${API_URL}/nestauth/login`,
96
+ params,
97
+ {},
98
+ false,
99
+ );
100
+ if (
101
+ !response ||
102
+ !response.accessToken ||
103
+ !response.refreshToken ||
104
+ !response.accessTokenExpiresIn ||
105
+ !response.refreshTokenExpiresIn
106
+ ) {
107
+ throw new Error(
108
+ "Login failed" +
109
+ " API URL: " +
110
+ API_URL +
111
+ " params: " +
112
+ JSON.stringify(params) +
113
+ " response: " +
114
+ JSON.stringify(response),
115
+ );
133
116
  }
117
+ const cookieStore = await cookies();
118
+ cookieStore.set("access_token", response.accessToken, {
119
+ httpOnly: true,
120
+ secure: process.env.NODE_ENV === "production",
121
+ sameSite: "lax",
122
+ path: "/",
123
+ maxAge: convertToSeconds(response.accessTokenExpiresIn ?? ""),
124
+ });
125
+
126
+ cookieStore.set("refresh_token", response.refreshToken, {
127
+ httpOnly: true,
128
+ secure: process.env.NODE_ENV === "production",
129
+ sameSite: "lax",
130
+ path: "/",
131
+ maxAge: convertToSeconds(response.refreshTokenExpiresIn ?? ""),
132
+ });
133
+
134
+ return response;
135
+ } catch (error: any) {
136
+ // console.log(error);
137
+ throw new Error(error?.message ?? "Login failed");
138
+ }
134
139
  }
135
140
 
136
141
  export async function getAccessToken() {
137
- const cookieStore = await cookies();
138
- const access_token = cookieStore.get('access_token')?.value;
139
- return access_token ?? null;
142
+ const cookieStore = await cookies();
143
+ const access_token = cookieStore.get("access_token")?.value;
144
+ return access_token ?? null;
140
145
  }
141
146
 
142
147
  export async function getRefreshToken() {
143
- const cookieStore = await cookies();
144
- const refresh_token = cookieStore.get('refresh_token')?.value;
145
- return refresh_token ?? null;
148
+ const cookieStore = await cookies();
149
+ const refresh_token = cookieStore.get("refresh_token")?.value;
150
+ return refresh_token ?? null;
146
151
  }
147
152
 
148
153
  export async function checkAuth() {
149
- const accessToken = await getAccessToken();
150
- return !!accessToken;
154
+ const accessToken = await getAccessToken();
155
+ return !!accessToken;
151
156
  }
152
157
 
153
158
  export async function getUserInfo() {
154
- const accessToken = await getAccessToken();
155
- if (!accessToken) {
156
- return null;
157
- }
158
- try {
159
- const decoded = jwtDecode(accessToken) as User;
160
- return decoded;
161
- } catch (error) {
162
- return null;
163
- }
159
+ const accessToken = await getAccessToken();
160
+ if (!accessToken) {
161
+ return null;
162
+ }
163
+ try {
164
+ const decoded = jwtDecode(accessToken) as User;
165
+ return decoded;
166
+ } catch (error) {
167
+ return null;
168
+ }
164
169
  }
165
170
 
166
171
  export async function logout() {
167
- (await cookies()).delete('access_token');
168
- (await cookies()).delete('refresh_token');
172
+ (await cookies()).delete("access_token");
173
+ (await cookies()).delete("refresh_token");
169
174
  }
170
175
 
171
- export async function get(url: string, params: any = {}, headers: any = {}, secured = true) {
172
- const headerData: Record<string, string> = {
173
- Authorization: '',
174
- ...headers,
175
- };
176
- if (secured) {
177
- const accessToken = await getAccessToken();
178
- headerData.Authorization = 'Bearer ' + accessToken;
179
- }
180
-
181
- try {
182
- const response = await axios.get(url, {
183
- headers: headerData,
184
- params: params,
185
- withCredentials: true,
186
- });
187
- if (response.status === 200 || response.status === 201) {
188
- return response.data;
189
- }
190
- return null;
191
- } catch (error) {
192
- return error;
176
+ export async function get(
177
+ url: string,
178
+ params: any = {},
179
+ headers: any = {},
180
+ secured = true,
181
+ ) {
182
+ const headerData: Record<string, string> = {
183
+ Authorization: "",
184
+ ...headers,
185
+ };
186
+ if (secured) {
187
+ const accessToken = await getAccessToken();
188
+ headerData.Authorization = "Bearer " + accessToken;
189
+ }
190
+
191
+ try {
192
+ const response = await axios.get(url, {
193
+ headers: headerData,
194
+ params: params,
195
+ withCredentials: true,
196
+ });
197
+ if (response.status === 200 || response.status === 201) {
198
+ return response.data;
193
199
  }
200
+ return null;
201
+ } catch (error) {
202
+ return error;
203
+ }
194
204
  }
195
205
 
196
- export async function post(url: string, data: any = {}, headers: any = {}, secured = true) {
197
- const headerData: Record<string, string> = {
198
- Authorization: '',
199
- ...headers,
200
- };
201
- if (secured) {
202
- const accessToken = await getAccessToken();
203
- headerData.Authorization = 'Bearer ' + accessToken;
204
- }
205
- try {
206
- const response = await axios.post(url, data, {
207
- headers: headerData,
208
- withCredentials: true,
209
- });
210
- if (response.status === 200 || response.status === 201) {
211
- return response.data;
212
- }
213
- return null;
214
- } catch (error) {
215
- return error;
206
+ export async function post(
207
+ url: string,
208
+ data: any = {},
209
+ headers: any = {},
210
+ secured = true,
211
+ ) {
212
+ const headerData: Record<string, string> = {
213
+ Authorization: "",
214
+ ...headers,
215
+ };
216
+ if (secured) {
217
+ const accessToken = await getAccessToken();
218
+ headerData.Authorization = "Bearer " + accessToken;
219
+ }
220
+ try {
221
+ const response = await axios.post(url, data, {
222
+ headers: headerData,
223
+ withCredentials: true,
224
+ });
225
+ if (response.status === 200 || response.status === 201) {
226
+ return response.data;
216
227
  }
228
+ return null;
229
+ } catch (error) {
230
+ return error;
231
+ }
217
232
  }