@floatingpixels/supabase-nuxt 0.1.6 → 0.1.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/README.md +95 -6
- package/dist/module.json +1 -1
- package/dist/module.mjs +5 -1
- package/dist/runtime/server/auth/callback.mjs +17 -0
- package/dist/runtime/server/auth/confirm.d.ts +2 -0
- package/package.json +1 -1
- /package/dist/runtime/server/{api/confirm.d.ts → auth/callback.d.ts} +0 -0
- /package/dist/runtime/server/{api → auth}/confirm.mjs +0 -0
package/README.md
CHANGED
|
@@ -154,9 +154,98 @@ const signInWithOtp = async () => {
|
|
|
154
154
|
|
|
155
155
|
> ⚠️ Ensure to activate and configure the authentication providers you want to use in the Supabase Dashboard under `Authentication -> Providers`.
|
|
156
156
|
|
|
157
|
-
Once the authorization flow is triggered using the `auth` wrapper of the `useSupabaseClient` composable, the session management is handled automatically.
|
|
157
|
+
Once the authorization flow is triggered using the `auth` wrapper of the `useSupabaseClient` composable, the session management is handled automatically. For the authentication flow PKCE is used, which requires an exchange between your server and the Supabase authentication server for some authentication methods. Please make sure you update your Supabase settings accordingly.
|
|
158
158
|
|
|
159
|
-
|
|
159
|
+
### E-Mail Authentication
|
|
160
|
+
|
|
161
|
+
When using e-mail authentication, a confirmation e-mail is sent to new users, and an e-mail containing a magic link is sent to existing users. For those links to work with your application, you need to adjust the e-mail templates in your Supabase settings under `Aithentication -> Email Templates`. The generated links must contain a `token_hash` and `type` URL parameter, and point to the confirmation URL of your app, which is `/supabase/confirm` by default. In addition you can set the URL parameter `next` to determine the route users will be forwarded to in your app when authorization is successful. If `next` is omitted, it will route to `/`. An example template looks like this:
|
|
162
|
+
|
|
163
|
+
```html
|
|
164
|
+
<h2>Confirm your signup</h2>
|
|
165
|
+
|
|
166
|
+
<p>Follow this link to confirm your user:</p>
|
|
167
|
+
<p>
|
|
168
|
+
<a href="{{ .SiteURL }}/supabase/confirm?token_hash={{ .TokenHash }}&type=email&next=/path-to-your-page"
|
|
169
|
+
>Confirm your email</a
|
|
170
|
+
>
|
|
171
|
+
</p>
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
The confirmation route on your server is provided by this module, so you don't need to implement it yourself. It's available at `/supabase/confirm`. It will automatically confirm the user and redirect to the `next` route. If you want to customize the confirmation route, you can do so by creating a server route to handle the request, and point to it in your Supabase e-mail template. Your custom route will receive the `token_hash` and `type` URL parameters, and the `next` URL parameter if provided. You can use the `useSupabaseClient` composable to confirm the user and redirect to the `next` route:
|
|
175
|
+
|
|
176
|
+
> ⚠️ You can use the provided confirm route at `/supabase/confirm`, the implementation of a custom route is optional!
|
|
177
|
+
|
|
178
|
+
```ts [server/api/confirm.ts]
|
|
179
|
+
import { EmailOtpType } from '@supabase/supabase-js'
|
|
180
|
+
import { supabaseServerClient } from '#supabase/server'
|
|
181
|
+
|
|
182
|
+
export default defineEventHandler(async event => {
|
|
183
|
+
const query = getQuery(event)
|
|
184
|
+
const token_hash = query.token_hash as string
|
|
185
|
+
const type = query.type as EmailOtpType | null
|
|
186
|
+
const next = (query.next as string) ?? '/'
|
|
187
|
+
|
|
188
|
+
if (!token_hash || !type) {
|
|
189
|
+
throw createError({ statusMessage: 'Invalid token' })
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const supabase = await supabaseServerClient(event)
|
|
193
|
+
const { error } = await supabase.auth.verifyOtp({ type, token_hash })
|
|
194
|
+
|
|
195
|
+
if (error) {
|
|
196
|
+
throw createError({ statusMessage: error.message })
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
await sendRedirect(event, next, 302)
|
|
200
|
+
})
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### OAuth Authentication
|
|
204
|
+
|
|
205
|
+
When using OAuth authentication, you need to configure the OAuth provider in your Supabase settings under `Authentication -> Providers`. You can then use the `signInWithOAuth` method of the `auth` wrapper of the `useSupabaseClient` composable to initiate the authorization flow. This module provides a default callback under `/supabase/callback` that you can provide to the authentication function:
|
|
206
|
+
|
|
207
|
+
```ts [pages/login.vue]
|
|
208
|
+
const signInWithOAuth = async () => {
|
|
209
|
+
const { error } = await supabase.auth.signInWithOAuth({
|
|
210
|
+
provider: 'github',
|
|
211
|
+
options: {
|
|
212
|
+
redirectTo: 'http://<your-site-url>/supabase/callback',
|
|
213
|
+
},
|
|
214
|
+
})
|
|
215
|
+
if (error) console.log(error)
|
|
216
|
+
}
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
You can customize the callback by creating your own server route, and point to it when calling `signInWithOAuth`. The callback route will receive a code, that needs to be exchanged for a session. Here is an example:
|
|
220
|
+
|
|
221
|
+
> ⚠️ You can use the provided callback route at `/supabase/callback`, the implementation of a custom callback is optional!
|
|
222
|
+
|
|
223
|
+
```ts [server/api/callback.ts]
|
|
224
|
+
import { supabaseServerClient } from '#supabase/server'
|
|
225
|
+
|
|
226
|
+
export default defineEventHandler(async event => {
|
|
227
|
+
const query = getQuery(event)
|
|
228
|
+
const code = query.code as string
|
|
229
|
+
const next = (query.next as string) ?? '/'
|
|
230
|
+
|
|
231
|
+
if (!code) {
|
|
232
|
+
throw createError({ statusMessage: 'No code provided' })
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const supabase = await supabaseServerClient(event)
|
|
236
|
+
const { error } = await supabase.auth.exchangeCodeForSession(code)
|
|
237
|
+
|
|
238
|
+
if (error) {
|
|
239
|
+
throw createError({ statusMessage: error.message })
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
await sendRedirect(event, next, 302)
|
|
243
|
+
})
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
### Redirection for non-authorized users
|
|
247
|
+
|
|
248
|
+
If `redirect` is set to `true` in the module options, users will be automatically routed to the login page when they are not authenticated. If you want to allow access to "public" pages, you just need to add them in the `exclude` `redirect` option, and they will not redirect unauthenticated users.
|
|
160
249
|
|
|
161
250
|
## Composables
|
|
162
251
|
|
|
@@ -164,7 +253,7 @@ If `redirect` is set to `true` in the module options, users will be automaticall
|
|
|
164
253
|
|
|
165
254
|
This composable can be used to make requests to the Supabase API. It's autoimported and ready to use in your components. It's using [supabase-js](https://github.com/supabase/supabase-js/) under the hood, it gives access to the [Supabase client](https://supabase.com/docs/reference/javascript/initializing) and all of its features.
|
|
166
255
|
|
|
167
|
-
|
|
256
|
+
#### Database Request
|
|
168
257
|
|
|
169
258
|
Please check [Supabase](https://supabase.com/docs/reference/javascript/select) documentation on how to fully use the Supabase client.
|
|
170
259
|
|
|
@@ -182,7 +271,7 @@ const { data: restaurant } = await useAsyncData('restaurant', async () => {
|
|
|
182
271
|
</script>
|
|
183
272
|
```
|
|
184
273
|
|
|
185
|
-
|
|
274
|
+
#### Realtime
|
|
186
275
|
|
|
187
276
|
Based on [Supabase Realtime](https://github.com/supabase/realtime), listen to changes in your PostgreSQL Database and broadcasts them over WebSockets.
|
|
188
277
|
|
|
@@ -221,7 +310,7 @@ onUnmounted(() => {
|
|
|
221
310
|
</script>
|
|
222
311
|
```
|
|
223
312
|
|
|
224
|
-
|
|
313
|
+
#### Type Support
|
|
225
314
|
|
|
226
315
|
You can pass Database typings to the client. Check Supabase [documentation](https://supabase.com/docs/reference/javascript/release-notes#typescript-support) for further information.
|
|
227
316
|
|
|
@@ -232,7 +321,7 @@ const client = useSupabaseClient<Database>()
|
|
|
232
321
|
</script>
|
|
233
322
|
```
|
|
234
323
|
|
|
235
|
-
|
|
324
|
+
#### Authentication Client
|
|
236
325
|
|
|
237
326
|
The useSupabaseClient composable is providing all methods to manage authorization under `useSupabaseClient().auth`. For more details please see the [supabase-js auth documentation](https://supabase.com/docs/reference/javascript/auth-api). Here is an example for signing in and out:
|
|
238
327
|
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -60,7 +60,11 @@ const module = defineNuxtModule({
|
|
|
60
60
|
});
|
|
61
61
|
addServerHandler({
|
|
62
62
|
route: "/supabase/confirm",
|
|
63
|
-
handler: resolve("./runtime/server/
|
|
63
|
+
handler: resolve("./runtime/server/auth/confirm")
|
|
64
|
+
});
|
|
65
|
+
addServerHandler({
|
|
66
|
+
route: "/supabase/callback",
|
|
67
|
+
handler: resolve("./runtime/server/auth/callback")
|
|
64
68
|
});
|
|
65
69
|
if (options.redirect) {
|
|
66
70
|
addPlugin(resolve("./runtime/plugins/auth-redirect"));
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { defineEventHandler } from "#imports";
|
|
2
|
+
import { createError, getQuery, sendRedirect } from "h3";
|
|
3
|
+
import { supabaseServerClient } from "#supabase/server";
|
|
4
|
+
export default defineEventHandler(async (event) => {
|
|
5
|
+
const query = getQuery(event);
|
|
6
|
+
const code = query.code;
|
|
7
|
+
const next = query.next ?? "/";
|
|
8
|
+
if (!code) {
|
|
9
|
+
throw createError({ statusMessage: "No code provided" });
|
|
10
|
+
}
|
|
11
|
+
const supabase = await supabaseServerClient(event);
|
|
12
|
+
const { error } = await supabase.auth.exchangeCodeForSession(code);
|
|
13
|
+
if (error) {
|
|
14
|
+
throw createError({ statusMessage: error.message });
|
|
15
|
+
}
|
|
16
|
+
await sendRedirect(event, next, 302);
|
|
17
|
+
});
|
package/package.json
CHANGED
|
File without changes
|
|
File without changes
|