@civic/auth 0.1.2 → 0.1.3
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/.turbo/turbo-build.log +3 -3
- package/CHANGELOG.md +3 -0
- package/README.md +329 -116
- package/package.json +3 -3
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
|
|
2
2
|
|
|
3
|
-
> @civic/auth@0.1.2
|
|
3
|
+
> @civic/auth@0.1.2 build /Users/kevincolgan/code/civic-auth/packages/civic-auth-client
|
|
4
4
|
> pnpm build:cjs && pnpm build:esm
|
|
5
5
|
|
|
6
6
|
|
|
7
|
-
> @civic/auth@0.1.2
|
|
7
|
+
> @civic/auth@0.1.2 build:cjs /Users/kevincolgan/code/civic-auth/packages/civic-auth-client
|
|
8
8
|
> tsc -p tsconfig.cjs.json --noEmit false && tsc-alias -p tsconfig.cjs.json
|
|
9
9
|
|
|
10
10
|
|
|
11
|
-
> @civic/auth@0.1.2
|
|
11
|
+
> @civic/auth@0.1.2 build:esm /Users/kevincolgan/code/civic-auth/packages/civic-auth-client
|
|
12
12
|
> tsc -p tsconfig.esm.json --noEmit false && tsc-alias -p tsconfig.esm.json
|
|
13
13
|
|
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -1,198 +1,411 @@
|
|
|
1
|
-
|
|
1
|
+
- [Civic Auth Client SDK](#civic-auth-client-sdk)
|
|
2
|
+
- [Quick Start](#quick-start)
|
|
3
|
+
- [npm](#npm)
|
|
4
|
+
- [yarn](#yarn)
|
|
5
|
+
- [pnpm](#pnpm)
|
|
6
|
+
- [bun](#bun)
|
|
7
|
+
- [Integration](#integration)
|
|
8
|
+
- [React](#react)
|
|
9
|
+
- [Usage](#usage)
|
|
10
|
+
- [The User Button](#the-user-button)
|
|
11
|
+
- [Getting User Information on the Frontend](#getting-user-information-on-the-frontend)
|
|
12
|
+
- [Advanced Configuration](#advanced-configuration)
|
|
13
|
+
- [Display Mode](#display-mode)
|
|
14
|
+
- [API](#api)
|
|
15
|
+
- [User Context](#user-context)
|
|
16
|
+
- [User](#user)
|
|
17
|
+
- [Base User Fields](#base-user-fields)
|
|
18
|
+
- [Token Fields](#token-fields)
|
|
19
|
+
- [Forwarded Tokens](#forwarded-tokens)
|
|
20
|
+
- [Embedded Login Iframe](#embedded-login-iframe)
|
|
21
|
+
- [Next.JS](#nextjs)
|
|
22
|
+
- [Quick Start](#quick-start-1)
|
|
23
|
+
- [**1. Add the Civic Auth Plugin**](#1-add-the-civic-auth-plugin)
|
|
24
|
+
- [**2. Create an API Route**](#2-create-an-api-route)
|
|
25
|
+
- [**3. Middleware**](#3-middleware)
|
|
26
|
+
- [Middleware Chaining](#middleware-chaining)
|
|
27
|
+
- [**4. Frontend Integration**](#4-frontend-integration)
|
|
28
|
+
- [Usage](#usage-1)
|
|
29
|
+
- [Getting User Information on the Frontend](#getting-user-information-on-the-frontend-1)
|
|
30
|
+
- [Getting User Information on the Backend](#getting-user-information-on-the-backend)
|
|
31
|
+
- [Advanced Configuration](#advanced-configuration-1)
|
|
2
32
|
|
|
3
|
-
|
|
33
|
+
# Civic Auth Client SDK
|
|
34
|
+
Civic Auth offers a simple, flexible, and fast way to integrate authentication into your applications.
|
|
4
35
|
|
|
5
|
-
|
|
36
|
+
You can find the full, official documentation [here](https://docs.civic.com/auth), with a quick-start version in this README.
|
|
6
37
|
|
|
7
|
-
|
|
8
|
-
- Support for various authentication flows (iframe, redirect, new tab)
|
|
9
|
-
- Token management and refresh
|
|
10
|
-
- User information retrieval
|
|
11
|
-
- Customizable UI components
|
|
12
|
-
- TypeScript support
|
|
38
|
+
## Quick Start
|
|
13
39
|
|
|
14
|
-
|
|
40
|
+
Sign up for Civic Auth in less than a minute at [auth.civic.com](https://auth.civic.com) to get your Client ID.
|
|
15
41
|
|
|
16
|
-
Install the
|
|
42
|
+
Install the library in your app using your installer of choice:
|
|
17
43
|
|
|
44
|
+
### npm
|
|
18
45
|
```bash
|
|
19
46
|
npm install @civic/auth
|
|
20
47
|
```
|
|
21
|
-
|
|
22
|
-
or
|
|
23
|
-
|
|
48
|
+
### yarn
|
|
24
49
|
```bash
|
|
25
50
|
yarn add @civic/auth
|
|
26
51
|
```
|
|
52
|
+
### pnpm
|
|
53
|
+
```bash
|
|
54
|
+
pnpm install @civic/auth
|
|
55
|
+
```
|
|
56
|
+
### bun
|
|
57
|
+
```bash
|
|
58
|
+
bun add @civic/auth
|
|
59
|
+
```
|
|
27
60
|
|
|
28
|
-
##
|
|
61
|
+
## Integration
|
|
62
|
+
|
|
63
|
+
Choose your framework for instructions on how to integrate Civic Auth into your application.
|
|
64
|
+
* [ReactJS](#react)
|
|
65
|
+
* [NextJS](#nextjs)
|
|
66
|
+
|
|
29
67
|
|
|
30
|
-
|
|
68
|
+
For integrating in other environments using any OIDC or OAuth 2.0-compliant client libraries, see [here]([integration/other.md](https://docs.civic.com/auth/integration/other)).
|
|
31
69
|
|
|
32
|
-
### Setup CivicAuthProvider
|
|
33
70
|
|
|
34
|
-
|
|
71
|
+
# React
|
|
35
72
|
|
|
36
|
-
|
|
37
|
-
|
|
73
|
+
Integrate Civic Auth into your React application with ease, just wrap your app with the Civic Auth provider and add your Client ID (provided after you [sign up](https://auth.civic.com)). A working example is available in our [github examples repo](https://github.com/civicteam/civic-auth-examples/tree/main/packages/civic-auth/reactjs).
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
import { CivicAuthProvider, UserButton } from "@civic/auth/react";
|
|
38
77
|
|
|
39
78
|
function App({ children }) {
|
|
40
79
|
return (
|
|
41
|
-
<CivicAuthProvider clientId="
|
|
80
|
+
<CivicAuthProvider clientId="YOUR CLIENT ID">
|
|
81
|
+
<UserButton />
|
|
42
82
|
{children}
|
|
43
83
|
</CivicAuthProvider>
|
|
44
|
-
)
|
|
84
|
+
)
|
|
45
85
|
}
|
|
46
86
|
```
|
|
47
87
|
|
|
48
|
-
|
|
88
|
+
## Usage
|
|
49
89
|
|
|
50
|
-
###
|
|
90
|
+
### The User Button
|
|
51
91
|
|
|
52
|
-
|
|
92
|
+
The Civic Auth SDK comes with a multi-purpose styled component called the `UserButton`
|
|
53
93
|
|
|
54
|
-
```
|
|
55
|
-
import { CivicAuthProvider } from
|
|
94
|
+
```typescript
|
|
95
|
+
import { UserButton, CivicAuthProvider } from '@civic/auth/react';
|
|
96
|
+
|
|
97
|
+
export function TitleBar() {
|
|
98
|
+
return (
|
|
99
|
+
<div className="flex justify-between items-center">
|
|
100
|
+
<h1>My App</h1>
|
|
101
|
+
<UserButton />
|
|
102
|
+
</div>
|
|
103
|
+
);
|
|
104
|
+
};
|
|
56
105
|
|
|
57
106
|
function App({ children }) {
|
|
58
107
|
return (
|
|
59
|
-
<CivicAuthProvider
|
|
60
|
-
|
|
61
|
-
redirectUrl="https://your-app.com/callback"
|
|
62
|
-
config={{
|
|
63
|
-
endpoints: {
|
|
64
|
-
auth: "https://your-auth-server.com/oauth/auth",
|
|
65
|
-
token: "https://your-auth-server.com/oauth/token",
|
|
66
|
-
userinfo: "https://your-auth-server.com/oauth/userinfo",
|
|
67
|
-
},
|
|
68
|
-
}}
|
|
69
|
-
>
|
|
70
|
-
{children}
|
|
108
|
+
<CivicAuthProvider clientId="YOUR CLIENT ID">
|
|
109
|
+
<TitleBar />
|
|
71
110
|
</CivicAuthProvider>
|
|
72
|
-
)
|
|
111
|
+
)
|
|
73
112
|
}
|
|
74
113
|
```
|
|
75
114
|
|
|
76
|
-
|
|
115
|
+
This component is context-dependent. If the user is logged in, it will show their profile picture and name. If the user is not logged in, it will show a Log In button.
|
|
77
116
|
|
|
78
|
-
|
|
117
|
+
### Getting User Information on the Frontend
|
|
79
118
|
|
|
80
|
-
|
|
81
|
-
import { useAuth, useUser } from "@civic/auth/react";
|
|
119
|
+
Use the Civic Auth SDK to retrieve user information on the frontend.
|
|
82
120
|
|
|
83
|
-
|
|
84
|
-
|
|
121
|
+
```typescript
|
|
122
|
+
import { useUser } from '@civic/auth/react';
|
|
123
|
+
|
|
124
|
+
export function MyComponent() {
|
|
85
125
|
const { user } = useUser();
|
|
126
|
+
|
|
127
|
+
if (!user) return <div>User not logged in</div>
|
|
128
|
+
|
|
129
|
+
return <div>Hello { user.name }!</div>
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
We use `name` as an example here, but you can call any user object property from the user fields schema, as shown [below](react.md#base-user-fields).
|
|
134
|
+
|
|
135
|
+
## Advanced Configuration
|
|
136
|
+
|
|
137
|
+
Civic Auth is a "low-code" solution, so all configuration takes place via the [dashboard](https://auth.civic.com). Changes you make there will be updated automatically in your integration without any code changes. The only required parameter you need to provide is the client ID.
|
|
138
|
+
|
|
139
|
+
The integration provides additional run-time settings and hooks that you can use to customize the library's integration with your own app. If you need any of these, you can add them to the CivicAuthProvider as follows:
|
|
86
140
|
|
|
87
|
-
|
|
88
|
-
|
|
141
|
+
```typescript
|
|
142
|
+
<CivicAuthProvider
|
|
143
|
+
clientId="YOUR CLIENT ID"
|
|
144
|
+
...other configuration
|
|
145
|
+
>
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
See below for the list of all configuration options
|
|
149
|
+
|
|
150
|
+
<table><thead><tr><th width="115">Field</th><th width="70">Required</th><th width="96">Default</th><th width="251">Example</th><th>Description</th></tr></thead><tbody><tr><td>clientId</td><td>Yes</td><td>-</td><td><code>2cc5633d-2c92-48da-86aa-449634f274b9</code></td><td>The key obtained on signup to <a href="https://auth.civic.com">auth.civic.com</a></td></tr><tr><td>nonce</td><td>No</td><td>-</td><td>1234</td><td>A single-use ID used during login, binding a login token with a given client. Needed in advanced authentication processes only</td></tr><tr><td>onSignIn</td><td>No</td><td>-</td><td><p></p><pre class="language-typescript"><code class="lang-typescript">(error?: Error) => {
|
|
151
|
+
if (error) {
|
|
152
|
+
// handle error
|
|
153
|
+
} else {
|
|
154
|
+
// handle successful login
|
|
89
155
|
}
|
|
156
|
+
}
|
|
157
|
+
</code></pre></td><td>A hook that executes after a sign-in attempt, whether successful or not.</td></tr><tr><td>onSignOut</td><td>No</td><td>-</td><td><pre class="language-typescript"><code class="lang-typescript">() => {
|
|
158
|
+
// handle signout
|
|
159
|
+
}
|
|
160
|
+
</code></pre></td><td>A hook that executes after a user logs out.</td></tr><tr><td>redirectUrl</td><td>No</td><td>currentURL</td><td>/authenticating</td><td>An override for the page that OAuth will redirect to to perform token-exchange. By default Civic will redirect to the current URL and Authentication will be finished by the Civic provider automatically. Only use if you'd like to have some custom display or logic during OAuth token-exchange. The redirect page must have the CivicAuthProvider running in order to finish authentication.</td></tr><tr><td><p></p><p>modalIframe</p></td><td>No</td><td>true</td><td>modalIframe={true}</td><td>Set to <code>true</code> if you want to embed the login iframe in your app rather than opening the iframe in a modal. See <a href="react.md#embedded-login-iframe">Embedded Login Iframe section</a> below.</td></tr></tbody></table>
|
|
90
161
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
162
|
+
### Display Mode
|
|
163
|
+
|
|
164
|
+
The display mode indicates where the Civic login UI will be displayed. The following display modes are supported:
|
|
165
|
+
|
|
166
|
+
* `iframe` (default): the UI loads in an iframe that shows in an overlay on top of the existing page content
|
|
167
|
+
* `redirect`: the UI redirects the current URL to a Civic login screen, then redirects back to your site when login is complete
|
|
168
|
+
* `new_tab`: the UI opens in a new tab or popup window (depending on browser preferences), and after login is complete, the tab or popup closes to return the user to your site
|
|
169
|
+
|
|
170
|
+
## API
|
|
171
|
+
|
|
172
|
+
### User Context
|
|
173
|
+
|
|
174
|
+
The full user context object (provided by `useUser`) looks like this:
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
{
|
|
178
|
+
user: User | null;
|
|
179
|
+
isLoading: boolean;
|
|
180
|
+
error: Error | null;
|
|
181
|
+
signIn: (displayMode?: DisplayMode) => Promise<void>;
|
|
182
|
+
signOut: () => Promise<void>;
|
|
97
183
|
}
|
|
98
184
|
```
|
|
185
|
+
### User
|
|
186
|
+
|
|
187
|
+
The `User` object looks like this:
|
|
188
|
+
|
|
189
|
+
```typescript
|
|
190
|
+
type BaseUser = {
|
|
191
|
+
id: string;
|
|
192
|
+
email?: string;
|
|
193
|
+
name?: string;
|
|
194
|
+
picture?: string;
|
|
195
|
+
given_name?: string;
|
|
196
|
+
family_name?: string;
|
|
197
|
+
updated_at?: Date;
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
type Tokens = {
|
|
201
|
+
idToken: string;
|
|
202
|
+
accessToken: string;
|
|
203
|
+
refreshToken: string;
|
|
204
|
+
forwardedTokens: ForwardedTokens;
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
type User = BaseUser & Tokens
|
|
208
|
+
```
|
|
99
209
|
|
|
100
|
-
|
|
210
|
+
Field descriptions:
|
|
101
211
|
|
|
102
|
-
|
|
212
|
+
#### Base User Fields
|
|
103
213
|
|
|
104
|
-
|
|
105
|
-
import { UserButton } from "@civic/auth/react";
|
|
214
|
+
<table><thead><tr><th width="174">Field</th><th></th></tr></thead><tbody><tr><td>id</td><td>The user's unique ID with respect to your app. You can use this to look up the user in the <a href="https://auth.civic.com/dashboard">dashboard</a>.</td></tr><tr><td>email</td><td>The user's email address</td></tr><tr><td>name</td><td>The user's full name</td></tr><tr><td>given_name</td><td>The user's given name</td></tr><tr><td>family_name</td><td>The user's family name</td></tr><tr><td>updated_at</td><td>The time at which the user's profile was most recently updated.</td></tr></tbody></table>
|
|
106
215
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
216
|
+
#### Token Fields
|
|
217
|
+
|
|
218
|
+
Typically developers will not need to interact with the token fields, which are used only for advanced use cases.
|
|
219
|
+
|
|
220
|
+
<table><thead><tr><th width="185">Field</th><th></th></tr></thead><tbody><tr><td>idToken</td><td>The OIDC id token, used to request identity information about the user</td></tr><tr><td>accessToken</td><td>The OAuth 2.0 access token, allowing a client to make API calls to Civic Auth on behalf of the user.</td></tr><tr><td>refreshToken</td><td>The OAuth 2.0 refresh token, allowing a login session to be extended automatically without requiring user interaction. <br>The Civic Auth SDK handles refresh automatically, so you do not need to do this.</td></tr><tr><td>forwardedTokens</td><td>If the user authenticated using SSO (single-sign-on login) with a federated OAuth provider such as Google, this contains the OIDC and OAuth 2.0 tokens from that provider.<br><br></td></tr></tbody></table>
|
|
221
|
+
|
|
222
|
+
#### Forwarded Tokens
|
|
223
|
+
|
|
224
|
+
Use forwardedTokens if you need to make requests to the source provider, such as find out provider-specific information.
|
|
225
|
+
|
|
226
|
+
\
|
|
227
|
+
An example would be, if a user logged in via Google, using the Google forwarded token to query the Google Groups that the user is a member of.
|
|
228
|
+
|
|
229
|
+
For example:
|
|
230
|
+
|
|
231
|
+
```typescript
|
|
232
|
+
const googleAccessToken = user.forwardedTokens?.google?.accessToken;
|
|
115
233
|
```
|
|
116
234
|
|
|
117
|
-
|
|
235
|
+
#### Embedded Login Iframe
|
|
118
236
|
|
|
119
|
-
|
|
237
|
+
If you want to have the Login screen open directly on a page without the user having to click on button, you can import the `CivicAuthIframeContainer` component along with the AuthProvider option `modalIframe={false}` 
|
|
120
238
|
|
|
121
|
-
|
|
239
|
+
You just need to ensure that the `CivicAuthIframeContainer` is a child under a `CivicAuthProvider`
|
|
122
240
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
import { CivicAuthProvider } from "@civic/auth/react";
|
|
241
|
+
```typescript
|
|
242
|
+
import { CivicAuthIframeContainer } from "@civic/auth/react";
|
|
126
243
|
|
|
127
|
-
|
|
244
|
+
|
|
245
|
+
const Login = () => {
|
|
128
246
|
return (
|
|
129
|
-
|
|
130
|
-
clientId="your-client-id"
|
|
131
|
-
redirectUrl="https://your-app.com/callback"
|
|
132
|
-
modalIframe={false}
|
|
133
|
-
>
|
|
134
|
-
{children}
|
|
135
|
-
<div className="flex min-h-[200px] items-center justify-center">
|
|
247
|
+
<div class="login-container">
|
|
136
248
|
<CivicAuthIframeContainer />
|
|
137
249
|
</div>
|
|
138
|
-
|
|
250
|
+
);
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
const App = () => {
|
|
254
|
+
return (
|
|
255
|
+
<CivicAuthProvider
|
|
256
|
+
clientId={"YOUR CLIENT ID"}
|
|
257
|
+
modalIframe={false}
|
|
258
|
+
>
|
|
259
|
+
<Login />
|
|
260
|
+
</CivicAuthProvider>
|
|
139
261
|
);
|
|
140
262
|
}
|
|
141
263
|
```
|
|
142
264
|
|
|
143
|
-
### Token Management with useToken Hook
|
|
144
265
|
|
|
145
|
-
The `useToken` hook can be used to access and manage tokens within your application. This hook provides the current access and ID tokens, a refresh function, and token loading/error states.
|
|
146
266
|
|
|
147
|
-
|
|
148
|
-
import { useToken } from "@civic/auth/react";
|
|
267
|
+
# Next.JS
|
|
149
268
|
|
|
150
|
-
|
|
151
|
-
const { accessToken, idToken, refreshToken, isLoading, error } = useToken();
|
|
269
|
+
## Quick Start
|
|
152
270
|
|
|
153
|
-
|
|
154
|
-
return <div>Loading tokens...</div>;
|
|
155
|
-
}
|
|
271
|
+
Integrate Civic Auth into your Next.js application using the following steps (a working example is available in our [github examples repo](https://github.com/civicteam/civic-auth-examples/tree/main/packages/civic-auth/nextjs)):
|
|
156
272
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
273
|
+
### **1. Add the Civic Auth Plugin**
|
|
274
|
+
|
|
275
|
+
This is where you give your app the Client ID provided when you sign up at [auth.civic.com](https://auth.civic.com).
|
|
276
|
+
|
|
277
|
+
The defaults should work out of the box for most customers, but if you want to configure your app, see [below](next.js.md#configuration) for details.
|
|
278
|
+
```typescript
|
|
279
|
+
import { createCivicAuthPlugin } from "@civic/auth/nextjs"
|
|
280
|
+
import type { NextConfig } from "next";
|
|
281
|
+
|
|
282
|
+
const nextConfig: NextConfig = {
|
|
283
|
+
/* config options here */
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
const withCivicAuth = createCivicAuthPlugin({
|
|
287
|
+
clientId: 'YOUR CLIENT ID'
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
export default withCivicAuth(nextConfig)
|
|
291
|
+
```
|
|
292
|
+
### **2. Create an API Route**
|
|
293
|
+
|
|
294
|
+
This is where your app will handle login and logout requests.
|
|
295
|
+
|
|
296
|
+
Create this file at the following path:
|
|
297
|
+
|
|
298
|
+
`src/app/api/auth/[...civicauth]/route.ts`
|
|
299
|
+
|
|
300
|
+
```typescript
|
|
301
|
+
import { handler } from '@civic/auth/nextjs'
|
|
302
|
+
|
|
303
|
+
export const GET = handler()
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
These steps apply to the [App Router](https://nextjs.org/docs/app). If you are using the Pages Router, please [contact us](https://discord.com/invite/MWmhXauJw8/?referrer=home-discord) for integration steps.
|
|
307
|
+
|
|
308
|
+
### **3. Middleware**
|
|
309
|
+
|
|
310
|
+
Middleware is used to protect your backend routes, server components and server actions from unauthenticated requests. 
|
|
311
|
+
|
|
312
|
+
Using the Civic Auth middleware ensures that only logged-in users have access to secure parts of your service.
|
|
313
|
+
|
|
314
|
+
```typescript
|
|
315
|
+
import { authMiddleware } from '@civic/auth/nextjs/middleware'
|
|
316
|
+
|
|
317
|
+
export default authMiddleware()
|
|
318
|
+
|
|
319
|
+
export const config = {
|
|
320
|
+
// include the paths you wish to secure here
|
|
321
|
+
matcher: [ '/api/:path*', '/admin/:path*' ]
|
|
322
|
+
}
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
#### Middleware Chaining
|
|
326
|
+
|
|
327
|
+
If you are already using middleware in your Next.js app, then you can chain them with Civic Auth as follows:
|
|
328
|
+
|
|
329
|
+
```typescript
|
|
330
|
+
import { auth } from '@civic/auth/nextjs'
|
|
331
|
+
import { NextRequest, NextResponse } from "next/server";
|
|
332
|
+
|
|
333
|
+
const withCivicAuth = auth()
|
|
334
|
+
|
|
335
|
+
const otherMiddleware = (request: NextRequest) => {
|
|
336
|
+
console.log('my middleware')
|
|
337
|
+
return NextResponse.next()
|
|
338
|
+
}
|
|
160
339
|
|
|
340
|
+
export default withCivicAuth(otherMiddleware)
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
### **4. Frontend Integration**
|
|
344
|
+
|
|
345
|
+
Add the Civic Auth context to your app to give your frontend access to the logged-in user.
|
|
346
|
+
|
|
347
|
+
```typescript
|
|
348
|
+
import { CivicAuthProvider } from "@civic/auth/nextjs";
|
|
349
|
+
|
|
350
|
+
function Layout({ children }) {
|
|
161
351
|
return (
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
);
|
|
352
|
+
// ... the rest of your app layout
|
|
353
|
+
<CivicAuthProvider>
|
|
354
|
+
{children}
|
|
355
|
+
</CivicAuthProvider>
|
|
356
|
+
)
|
|
168
357
|
}
|
|
169
358
|
```
|
|
170
359
|
|
|
171
|
-
|
|
360
|
+
Unlike the pure [React](react.md) integration, you do _not_ have to add your client ID again here!
|
|
361
|
+
|
|
362
|
+
## Usage
|
|
363
|
+
|
|
364
|
+
### Getting User Information on the Frontend
|
|
365
|
+
|
|
366
|
+
[Next.JS client components](https://nextjs.org/docs/app/building-your-application/rendering/client-components) can use the Civic Auth React tools to obtain user information as well as display convenient login, and logout buttons. See the [React Usage page](react.md) for details.
|
|
367
|
+
|
|
368
|
+
### Getting User Information on the Backend
|
|
172
369
|
|
|
173
|
-
|
|
174
|
-
- `redirectUrl`: (Optional) The URL to which users will be redirected after authentication.
|
|
175
|
-
- `endpoints`: (Optional) Object containing the URLs for the authentication endpoints (`auth`, `token`, `userinfo`). Use this if you want to point to your own authentication server.
|
|
370
|
+
Retrieve user information on backend code, such as in React Server Components, React Server Actions, or api routes using `getUser`:
|
|
176
371
|
|
|
177
|
-
|
|
372
|
+
```typescript
|
|
373
|
+
import { getUser } from '@civic/auth/nextjs';
|
|
178
374
|
|
|
179
|
-
|
|
375
|
+
const user = await getUser();
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
For example, in a NextJS Server Component:
|
|
180
379
|
|
|
181
|
-
|
|
182
|
-
|
|
380
|
+
```typescript
|
|
381
|
+
import { getUser } from '@civic/auth/nextjs';
|
|
183
382
|
|
|
184
|
-
|
|
185
|
-
|
|
383
|
+
export async function MyServerComponent() {
|
|
384
|
+
const user = await getUser();
|
|
385
|
+
|
|
386
|
+
if (!user) return <div>User not logged in</div>
|
|
387
|
+
|
|
388
|
+
return <div>Hello { user.name }!</div>
|
|
389
|
+
}
|
|
390
|
+
```
|
|
186
391
|
|
|
187
|
-
|
|
188
|
-
the current page remains open and a new tab is open to show the Civic auth UI. When authentication is complete, the tab is closed.
|
|
392
|
+
The `name` property is used as an example here, check out the [React Usage page](react.md) to see the entire basic user object structure. 
|
|
189
393
|
|
|
190
|
-
|
|
394
|
+
## Advanced Configuration
|
|
191
395
|
|
|
192
|
-
|
|
396
|
+
Civic Auth is a "low-code" solution, so most of the configuration takes place via the [dashboard](https://auth.civic.com). Changes you make there will be updated automatically in your integration without any code changes. The only required parameter you need to provide is the client ID.
|
|
193
397
|
|
|
194
|
-
|
|
398
|
+
The integration also offers the ability customize the library according to the needs of your Next.js app. For example, to restrict authentication checks to specific pages and routes in your app. You can do so inside `next.config.js` as follows:
|
|
399
|
+
|
|
400
|
+
```typescript
|
|
401
|
+
const withCivicAuth = createCivicAuthPlugin({
|
|
402
|
+
clientId: 'YOUR CLIENT ID'
|
|
403
|
+
...other config
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
export default withCivicAuth(nextConfig) // your next config here
|
|
407
|
+
```
|
|
195
408
|
|
|
196
|
-
|
|
409
|
+
Here are the available configuration options:
|
|
197
410
|
|
|
198
|
-
|
|
411
|
+
<table><thead><tr><th width="133">Field</th><th width="100">Required</th><th width="171">Default</th><th>Example</th><th>Description</th></tr></thead><tbody><tr><td>clientId</td><td>Yes</td><td>-</td><td><code>2cc5633d-2c92-48da-86aa-449634f274b9</code></td><td>The key obtained on signup to <a href="https://auth.civic.com">auth.civic.com</a></td></tr><tr><td>callbackUrl</td><td>No</td><td>/api/auth/callback</td><td>/api/myroute/callback</td><td>The path to route the browser to after a succesful login. Set this value if you are hosting your civic auth API route somewhere other than the default recommended <a href="next.js.md#create-an-api-route">above</a>.</td></tr><tr><td>loginUrl</td><td>No</td><td>/</td><td>/admin</td><td>The path your user will be sent to if they access a resource that needs them to be logged in. If you have a dedicated login page, you can set it here.</td></tr><tr><td>logoutUrl</td><td>No</td><td>/</td><td>/goodbye</td><td>The path your user will be sent to after a successful log-out.</td></tr><tr><td>include</td><td>No</td><td>['/*']</td><td><p>[</p><p> '/admin/*', '/api/admin/*'</p><p>]</p></td><td>An array of path <a href="https://man7.org/linux/man-pages/man7/glob.7.html">globs</a> that require a user to be logged-in to access. If not set, will include all paths matched by your Next.js <a href="next.js.md#middleware">middleware</a>.</td></tr><tr><td>exclude</td><td>No</td><td>-</td><td>['public/home']</td><td>An array of path <a href="https://man7.org/linux/man-pages/man7/glob.7.html">globs</a> that are excluded from the Civic Auth <a href="next.js.md#middleware">middleware</a>. In some cases, it might be easier and safer to specify exceptions rather than keep an inclusion list up to date.</td></tr></tbody></table>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@civic/auth",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"module": "dist/index.js",
|
|
6
6
|
"main": "dist/index.ts",
|
|
@@ -89,8 +89,8 @@
|
|
|
89
89
|
"vite": "^5.4.8",
|
|
90
90
|
"vite-plugin-dts": "^4.2.3",
|
|
91
91
|
"vitest": "^2.1.2",
|
|
92
|
-
"@repo/
|
|
93
|
-
"@repo/
|
|
92
|
+
"@repo/eslint-config": "0.0.0",
|
|
93
|
+
"@repo/typescript-config": "0.0.0"
|
|
94
94
|
},
|
|
95
95
|
"optionalDependency": {
|
|
96
96
|
"next": "^14"
|