@myauth/next 1.0.0 → 1.1.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/README.md +156 -0
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
# @myauth/next
|
|
2
|
+
|
|
3
|
+
Next.js SDK for MyAuth authentication service
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @myauth/next
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Middleware Protection
|
|
14
|
+
|
|
15
|
+
Protect your routes using the middleware:
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
// middleware.ts
|
|
19
|
+
import { withAuthMiddleware } from "@myauth/next";
|
|
20
|
+
|
|
21
|
+
export default withAuthMiddleware("your-client-id");
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### Server-Side Authentication
|
|
25
|
+
|
|
26
|
+
Get the current session on the server:
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import { auth } from "@myauth/next";
|
|
30
|
+
|
|
31
|
+
export default async function ProtectedPage() {
|
|
32
|
+
const session = await auth();
|
|
33
|
+
|
|
34
|
+
if (!session) {
|
|
35
|
+
return <div>Please log in</div>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return <div>Welcome, {session.user.email}!</div>;
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Client-Side Authentication
|
|
43
|
+
|
|
44
|
+
Wrap your app with the AuthProvider and use the useAuth hook:
|
|
45
|
+
|
|
46
|
+
```tsx
|
|
47
|
+
// app/layout.tsx
|
|
48
|
+
import { AuthProvider } from "@myauth/next";
|
|
49
|
+
|
|
50
|
+
export default function RootLayout({
|
|
51
|
+
children,
|
|
52
|
+
}: {
|
|
53
|
+
children: React.ReactNode;
|
|
54
|
+
}) {
|
|
55
|
+
return (
|
|
56
|
+
<html lang="en">
|
|
57
|
+
<body>
|
|
58
|
+
<AuthProvider>{children}</AuthProvider>
|
|
59
|
+
</body>
|
|
60
|
+
</html>
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// components/MyComponent.tsx
|
|
65
|
+
import { useAuth } from "@myauth/next";
|
|
66
|
+
|
|
67
|
+
export function MyComponent() {
|
|
68
|
+
const { user, login, logout, loading } = useAuth();
|
|
69
|
+
|
|
70
|
+
if (loading) return <div>Loading...</div>;
|
|
71
|
+
|
|
72
|
+
return (
|
|
73
|
+
<div>
|
|
74
|
+
{user ? (
|
|
75
|
+
<div>
|
|
76
|
+
<p>Welcome, {user.email}!</p>
|
|
77
|
+
<button onClick={logout}>Logout</button>
|
|
78
|
+
</div>
|
|
79
|
+
) : (
|
|
80
|
+
<button onClick={login}>Login</button>
|
|
81
|
+
)}
|
|
82
|
+
</div>
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Authentication Callback Handler
|
|
88
|
+
|
|
89
|
+
Handle authentication callbacks in your API routes:
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
// app/api/auth/callback/route.ts
|
|
93
|
+
import { createAuthCallbackHandler } from "@myauth/next";
|
|
94
|
+
|
|
95
|
+
export const GET = createAuthCallbackHandler({
|
|
96
|
+
successRedirect: "/",
|
|
97
|
+
failureRedirect: "/login",
|
|
98
|
+
});
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Authentication with Redirect Callback Component
|
|
102
|
+
|
|
103
|
+
For pages that handle redirects:
|
|
104
|
+
|
|
105
|
+
```tsx
|
|
106
|
+
// app/auth/callback/page.tsx
|
|
107
|
+
import { AuthenticateWithRedirectCallback } from "@myauth/next";
|
|
108
|
+
|
|
109
|
+
export default function AuthCallbackPage() {
|
|
110
|
+
return <AuthenticateWithRedirectCallback />;
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## API Reference
|
|
115
|
+
|
|
116
|
+
### Server Functions
|
|
117
|
+
|
|
118
|
+
- `auth()` - Returns the current session or null
|
|
119
|
+
- `createAuthCallbackHandler(options)` - Creates a handler for auth callbacks
|
|
120
|
+
- `getSessionToken()` - Gets the session token
|
|
121
|
+
|
|
122
|
+
### Client Components
|
|
123
|
+
|
|
124
|
+
- `AuthProvider` - React context provider for authentication
|
|
125
|
+
- `useAuth()` - Hook to access authentication state
|
|
126
|
+
- `AuthenticateWithRedirectCallback` - Component for handling auth redirects
|
|
127
|
+
|
|
128
|
+
### Middleware
|
|
129
|
+
|
|
130
|
+
- `withAuthMiddleware(clientId)` - Creates middleware to protect routes
|
|
131
|
+
|
|
132
|
+
## Types
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
type User = {
|
|
136
|
+
email: string;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
type AuthState = {
|
|
140
|
+
user: User | null;
|
|
141
|
+
token: string | null;
|
|
142
|
+
loading: boolean;
|
|
143
|
+
logout: () => Promise<void>;
|
|
144
|
+
login: () => Promise<void>;
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
type Session = {
|
|
148
|
+
user: User | null;
|
|
149
|
+
token: string | null;
|
|
150
|
+
};
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## License
|
|
154
|
+
|
|
155
|
+
ISC</content>
|
|
156
|
+
<parameter name="filePath">packages/next-sdk/README.md
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@myauth/next",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "Next.js SDK for MyAuth authentication service",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"author": "MyAuth Team",
|
|
34
34
|
"license": "ISC",
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@myauth/node": "^1.
|
|
36
|
+
"@myauth/node": "^1.1.0",
|
|
37
37
|
"dotenv": "^16.0.3"
|
|
38
38
|
},
|
|
39
39
|
"peerDependencies": {
|