|
| 1 | +--- |
| 2 | +title: Migration Guide |
| 3 | +description: Learn how to migrate @nuxtjs/supabase to take advantage of JWT signing keys support |
| 4 | +navigation: |
| 5 | + title: Migration |
| 6 | +--- |
| 7 | + |
| 8 | +This guide will help you migrate from `v1.x` to `v2.x`, which introduces support for Supabase's new JWT signing keys. |
| 9 | + |
| 10 | +## 🔐 What are JWT Signing Keys? |
| 11 | + |
| 12 | +Supabase has introduced a major security improvement by moving from symmetric to asymmetric JWT (JSON Web Token) signing. This change brings security and performance improvements in how your applications handle authentication. |
| 13 | + |
| 14 | +### The Problem with Symmetric Keys |
| 15 | + |
| 16 | +Previously, Supabase used a single shared secret key for both creating and verifying tokens. This meant your app had to constantly call `supabase.auth.getUser()` to check if sessions were valid, creating network delays calling the Supabase Auth server. |
| 17 | + |
| 18 | +### The Solution with Asymmetric JWT Signing Keys |
| 19 | +The new system uses two separate keys: |
| 20 | +- A private key (kept secure by Supabase) for creating tokens. |
| 21 | +- A public key (safe to share in your application) for verifying them. |
| 22 | + |
| 23 | +Your application can now verify user sessions locally without contacting Supabase servers, making it faster and more reliable. This new key is named as `publishable key` on Supabase. |
| 24 | + |
| 25 | +### Key Benefits |
| 26 | + |
| 27 | +This upgrade eliminates authentication bottlenecks, improves security, and makes your applications work better at the edge (no extra calls to the Supabase Auth server). |
| 28 | + |
| 29 | +::note{to="https://supabase.com/blog/jwt-signing-keys"} |
| 30 | +Read the full technical explanation in the official Supabase blog post. |
| 31 | +:: |
| 32 | + |
| 33 | +## 🚨 Breaking changes |
| 34 | + |
| 35 | +### 1. `useSupabaseUser` Type Changes |
| 36 | + |
| 37 | +`useSupabaseUser` now returns **JWT claims** instead of the full **User object**. |
| 38 | + |
| 39 | +- **Before (v1.x):** `useSupabaseUser` returned the full `User` object from [auth.getUser()](https://supabase.com/docs/reference/javascript/auth-getuser) |
| 40 | +- **After (v2.x):** `useSupabaseUser` returns `Claims` object from [auth.getClaims()](https://supabase.com/docs/reference/javascript/auth-getclaims) as a [JWT Payload](https://supabase.com/docs/guides/auth/jwts) |
| 41 | + |
| 42 | +::code-group |
| 43 | +```json [auth.getUser() as .json] |
| 44 | +{ |
| 45 | + id: "11111111-1111-1111-1111-111111111111", |
| 46 | + aud: "authenticated", |
| 47 | + role: "authenticated", |
| 48 | + |
| 49 | + email_confirmed_at: "2024-01-01T00:00:00Z", |
| 50 | + phone: "", |
| 51 | + confirmed_at: "2024-01-01T00:00:00Z", |
| 52 | + last_sign_in_at: "2024-01-01T00:00:00Z", |
| 53 | + app_metadata: {}, |
| 54 | + user_metadata: {}, |
| 55 | + identities: [] |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +```json [auth.getClaims().claims as .json] |
| 60 | +{ |
| 61 | + session_id: "11111111-1111-1111-1111-111111111111", |
| 62 | + sub: "11111111-1111-1111-1111-111111111111", |
| 63 | + aud: "authenticated", |
| 64 | + role: "authenticated", |
| 65 | + |
| 66 | + aal: "aal1", |
| 67 | + amr: [], |
| 68 | + exp: 1715769600, |
| 69 | + iat: 1715766000, |
| 70 | + is_anonymous: false, |
| 71 | + iss: "https://project-id.supabase.co/auth/v1", |
| 72 | + phone: "+13334445555", |
| 73 | + app_metadata: {}, |
| 74 | + user_metadata: {}, |
| 75 | + // identities is missing |
| 76 | + // last_sign_in_at is missing |
| 77 | + // confirmed_at is missing |
| 78 | + // email_confirmed_at is missing |
| 79 | +} |
| 80 | +``` |
| 81 | +:: |
| 82 | + |
| 83 | +::tip |
| 84 | +If you need the full User object, you'll need to explicitly call `auth.getUser()` but if you only need basic info (email, role...) no changes are required. |
| 85 | +:: |
| 86 | + |
| 87 | + |
| 88 | +### 2. Environment variables changes |
| 89 | + |
| 90 | +**Remaining key** → **`SUPABASE_KEY`** is now storing the `publishable key` instead of the `anon key` |
| 91 | + |
| 92 | +**New key** → **`SUPABASE_SECRET_KEY`** is now storing the `secret key` of your Supabase project |
| 93 | + |
| 94 | +**Deprecated key** → **`SUPABASE_SERVICE_KEY`** was previously storing the `service_role key` but is now deprecated in favor of `SUPABASE_SECRET_KEY` |
| 95 | + |
| 96 | +## 📋 Migration Steps |
| 97 | + |
| 98 | +### 1. Types changes |
| 99 | + |
| 100 | +The first thing you need to do is to ensure the `useSupabaseUser` changes do not affect your existing code. |
| 101 | + |
| 102 | +::tip |
| 103 | +Once you've updated the types, you can already test your application and it should still work as expected. |
| 104 | +:: |
| 105 | + |
| 106 | +### 2. Environment variables changes |
| 107 | + |
| 108 | +The **Good news** ☀️ is that everything will continue to work as-is with your existing `SUPABASE_KEY` (aka `anon key`). |
| 109 | + |
| 110 | +Same for `SUPABASE_SERVICE_KEY`, if you're using it to bypass Row Level Security, you will face a deprecation warning but it will still work. |
| 111 | + |
| 112 | +::warning{to="https://supabase.com/blog/jwt-signing-keys"} |
| 113 | +However, we highly recommend you to enable JWT signing keys in your Supabase project and use your publishable key as `SUPABASE_KEY` and your secret key as `SUPABASE_SECRET_KEY`. |
| 114 | +:: |
| 115 | + |
| 116 | +#### 2.a. Enable JWT Signing Keys in Supabase Dashboard |
| 117 | + |
| 118 | +Before migrating your environment variables, you need to enable JWT signing keys and thus create your JWT and API keys in your Supabase project: |
| 119 | + |
| 120 | +<iframe |
| 121 | + width="560" |
| 122 | + height="315" |
| 123 | + src="https://www.youtube.com/embed/rwnOal_xRtM" |
| 124 | + frameborder="0" |
| 125 | + allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" |
| 126 | + allowfullscreen |
| 127 | +></iframe> |
| 128 | +
|
| 129 | +::tip |
| 130 | +**Watch this video tutorial** from **6:20 to 12:20** to understand how to enable JWT signing keys in your Supabase dashboard and get your new secret key. |
| 131 | +:: |
| 132 | + |
| 133 | +#### 2.b. Use your new keys in your `.env` file |
| 134 | + |
| 135 | +```bash [.env] |
| 136 | +SUPABASE_KEY=<your_publishable_key> |
| 137 | +SUPABASE_SECRET_KEY=<your_secret_key> |
| 138 | +``` |
0 commit comments