Skip to content

Commit

Permalink
feat: cookies auth
Browse files Browse the repository at this point in the history
  • Loading branch information
geromegrignon committed May 28, 2024
1 parent ed634ea commit 138452c
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 1 deletion.
29 changes: 29 additions & 0 deletions angular-hub/src/app/services/supabase.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Injectable } from '@angular/core';
import { AuthSession, SupabaseClient } from '@supabase/supabase-js';
import { createBrowserClient } from '@supabase/ssr';

@Injectable({
providedIn: 'root',
})
export class SupabaseService {
public supabase: SupabaseClient = createBrowserClient(
process.env['SUPABASE_URL'] as string,
process.env['SUPABASE_KEY'] as string,
);
_session: AuthSession | null = null;

init(): void {
this.supabase.auth.onAuthStateChange((event, session) => {
this._session = session;
});
}

async signInWithGithub(): Promise<void> {
await this.supabase.auth.signInWithOAuth({
provider: 'github',
options: {
redirectTo: window.location.origin + '/api/v1/auth/callback',
},
});
}
}
43 changes: 43 additions & 0 deletions angular-hub/src/server/routes/v1/auth/callback.get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {
defineEventHandler,
getQuery,
sendRedirect,
setCookie,
parseCookies,
deleteCookie,
} from 'h3';
import { createServerClient } from '@supabase/ssr';

export default defineEventHandler(async (event) => {
try {
const { code, next }: { code: string; next: string } = getQuery(event);
const cookies = parseCookies(event);

if (code) {
const supabase = createServerClient(
process.env['SUPABASE_URL'] as string,
process.env['SUPABASE_KEY'] as string,
{
cookies: {
get: (name) => {
return cookies[name];
},
set: async (name, value, options) => {
setCookie(event, name, value, options);
},
remove: (name) => {
deleteCookie(event, name);
},
},
},
);

await supabase.auth.exchangeCodeForSession(code);
}

return sendRedirect(event, next || '/', 302);
} catch (e) {
// TODO to be handled properly
console.error('callback error', e);
}
});
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
"@angular/pwa": "18.0.1",
"@angular/router": "18.0.0",
"@angular/service-worker": "18.0.0",
"@supabase/ssr": "^0.3.0",
"@supabase/supabase-js": "^2.43.4",
"date-fns": "^2.30.0",
"front-matter": "^4.0.2",
"marked": "^5.0.2",
Expand Down
82 changes: 81 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3125,6 +3125,71 @@
resolved "https://registry.yarnpkg.com/@sindresorhus/merge-streams/-/merge-streams-2.3.0.tgz#719df7fb41766bc143369eaa0dd56d8dc87c9958"
integrity sha512-LtoMMhxAlorcGhmFYI+LhPgbPZCkgP6ra1YL604EeF6U98pLlQ3iWIGMdWSC+vWmPBWBNgmDBAhnAobLROJmwg==

"@supabase/[email protected]":
version "2.64.2"
resolved "https://registry.yarnpkg.com/@supabase/auth-js/-/auth-js-2.64.2.tgz#fe6828ed2c9844bf2e71b27f88ddfb635f24d1c1"
integrity sha512-s+lkHEdGiczDrzXJ1YWt2y3bxRi+qIUnXcgkpLSrId7yjBeaXBFygNjTaoZLG02KNcYwbuZ9qkEIqmj2hF7svw==
dependencies:
"@supabase/node-fetch" "^2.6.14"

"@supabase/[email protected]":
version "2.3.1"
resolved "https://registry.yarnpkg.com/@supabase/functions-js/-/functions-js-2.3.1.tgz#bddc12a97872f3978a733b66bddac53370721589"
integrity sha512-QyzNle/rVzlOi4BbVqxLSH828VdGY1RElqGFAj+XeVypj6+PVtMlD21G8SDnsPQDtlqqTtoGRgdMlQZih5hTuw==
dependencies:
"@supabase/node-fetch" "^2.6.14"

"@supabase/[email protected]", "@supabase/node-fetch@^2.6.14":
version "2.6.15"
resolved "https://registry.yarnpkg.com/@supabase/node-fetch/-/node-fetch-2.6.15.tgz#731271430e276983191930816303c44159e7226c"
integrity sha512-1ibVeYUacxWYi9i0cf5efil6adJ9WRyZBLivgjs+AUpewx1F3xPi7gLgaASI2SmIQxPoCEjAsLAzKPgMJVgOUQ==
dependencies:
whatwg-url "^5.0.0"

"@supabase/[email protected]":
version "1.15.2"
resolved "https://registry.yarnpkg.com/@supabase/postgrest-js/-/postgrest-js-1.15.2.tgz#c0a725706e3d534570d014d7b713cea12553ab98"
integrity sha512-9/7pUmXExvGuEK1yZhVYXPZnLEkDTwxgMQHXLrN5BwPZZm4iUCL1YEyep/Z2lIZah8d8M433mVAUEGsihUj5KQ==
dependencies:
"@supabase/node-fetch" "^2.6.14"

"@supabase/[email protected]":
version "2.9.5"
resolved "https://registry.yarnpkg.com/@supabase/realtime-js/-/realtime-js-2.9.5.tgz#22b7de952a7f37868ffc25d32d19f03f27bfcb40"
integrity sha512-TEHlGwNGGmKPdeMtca1lFTYCedrhTAv3nZVoSjrKQ+wkMmaERuCe57zkC5KSWFzLYkb5FVHW8Hrr+PX1DDwplQ==
dependencies:
"@supabase/node-fetch" "^2.6.14"
"@types/phoenix" "^1.5.4"
"@types/ws" "^8.5.10"
ws "^8.14.2"

"@supabase/ssr@^0.3.0":
version "0.3.0"
resolved "https://registry.yarnpkg.com/@supabase/ssr/-/ssr-0.3.0.tgz#bb151be9b31be2e59cd7c3ac538cf01d95f78156"
integrity sha512-lcVyQ7H6eumb2FB1Wa2N+jYWMfq6CFza3KapikT0fgttMQ+QvDgpNogx9jI8bZgKds+XFSMCojxFvFb+gwdbfA==
dependencies:
cookie "^0.5.0"
ramda "^0.29.0"

"@supabase/[email protected]":
version "2.5.5"
resolved "https://registry.yarnpkg.com/@supabase/storage-js/-/storage-js-2.5.5.tgz#2958e2a2cec8440e605bb53bd36649288c4dfa01"
integrity sha512-OpLoDRjFwClwc2cjTJZG8XviTiQH4Ik8sCiMK5v7et0MDu2QlXjCAW3ljxJB5+z/KazdMOTnySi+hysxWUPu3w==
dependencies:
"@supabase/node-fetch" "^2.6.14"

"@supabase/supabase-js@^2.43.4":
version "2.43.4"
resolved "https://registry.yarnpkg.com/@supabase/supabase-js/-/supabase-js-2.43.4.tgz#62c956b16bb01d5cb59e3ad73cf7628e3e9835c0"
integrity sha512-/pLPaxiIsn5Vaz3s32HC6O/VNwfeddnzS0bZRpOW0AKcPuXroD8pT9G8mpiBlZfpKsMmq6k7tlhW7Sr1PAQ1lw==
dependencies:
"@supabase/auth-js" "2.64.2"
"@supabase/functions-js" "2.3.1"
"@supabase/node-fetch" "2.6.15"
"@supabase/postgrest-js" "1.15.2"
"@supabase/realtime-js" "2.9.5"
"@supabase/storage-js" "2.5.5"

"@swc-node/core@^1.12.0":
version "1.13.1"
resolved "https://registry.yarnpkg.com/@swc-node/core/-/core-1.13.1.tgz#d8f72653e8e35cc4d6b881c60aea661e728dc350"
Expand Down Expand Up @@ -3475,6 +3540,11 @@
resolved "https://registry.yarnpkg.com/@types/parse-json/-/parse-json-4.0.2.tgz#5950e50960793055845e956c427fc2b0d70c5239"
integrity sha512-dISoDXWWQwUquiKsyZ4Ng+HX2KsPL7LyHKHQwgGFEA3IaKac4Obd+h2a/a6waisAoepJlBcx9paWqjA8/HVjCw==

"@types/phoenix@^1.5.4":
version "1.6.4"
resolved "https://registry.yarnpkg.com/@types/phoenix/-/phoenix-1.6.4.tgz#cceac93a827555473ad38057d1df7d06eef1ed71"
integrity sha512-B34A7uot1Cv0XtaHRYDATltAdKx0BvVKNgYNqE4WjtPUa4VQJM7kxeXcVKaH+KS+kCmZ+6w+QaUdcljiheiBJA==

"@types/qs@*":
version "6.9.15"
resolved "https://registry.yarnpkg.com/@types/qs/-/qs-6.9.15.tgz#adde8a060ec9c305a82de1babc1056e73bd64dce"
Expand Down Expand Up @@ -5001,6 +5071,11 @@ [email protected]:
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.6.0.tgz#2798b04b071b0ecbff0dbb62a505a8efa4e19051"
integrity sha512-U71cyTamuh1CRNCfpGY6to28lxvNwPG4Guz/EVjgf3Jmzv0vlDp1atT9eS5dDjMYHucpHbWns6Lwf3BKz6svdw==

cookie@^0.5.0:
version "0.5.0"
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.5.0.tgz#d1f5d71adec6558c58f389987c366aa47e994f8b"
integrity sha512-YZ3GUyn/o8gfKJlnlX7g7xq4gyO6OSuhGPKaaGssGB2qgDUS0gPgtTvoyZLTt9Ab6dC4hfc9dV5arkvc/OCmrw==

copy-anything@^2.0.1:
version "2.0.6"
resolved "https://registry.yarnpkg.com/copy-anything/-/copy-anything-2.0.6.tgz#092454ea9584a7b7ad5573062b2a87f5900fc480"
Expand Down Expand Up @@ -10170,6 +10245,11 @@ radix3@^1.1.0, radix3@^1.1.2:
resolved "https://registry.yarnpkg.com/radix3/-/radix3-1.1.2.tgz#fd27d2af3896c6bf4bcdfab6427c69c2afc69ec0"
integrity sha512-b484I/7b8rDEdSDKckSSBA8knMpcdsXudlE/LNL639wFoHKwLbEkQFZHWEYwDC0wa0FKUcCY+GAF73Z7wxNVFA==

ramda@^0.29.0:
version "0.29.1"
resolved "https://registry.yarnpkg.com/ramda/-/ramda-0.29.1.tgz#408a6165b9555b7ba2fc62555804b6c5a2eca196"
integrity sha512-OfxIeWzd4xdUNxlWhgFazxsA/nl3mS4/jGZI5n00uWOoSSFRhC1b6gl6xvmzUamgmqELraWp0J/qqVlXYPDPyA==

randombytes@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/randombytes/-/randombytes-2.1.0.tgz#df6f84372f0270dc65cdf6291349ab7a473d4f2a"
Expand Down Expand Up @@ -12282,7 +12362,7 @@ wrappy@1:
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"
integrity sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==

ws@^8.13.0, ws@^8.16.0:
ws@^8.13.0, ws@^8.14.2, ws@^8.16.0:
version "8.17.0"
resolved "https://registry.yarnpkg.com/ws/-/ws-8.17.0.tgz#d145d18eca2ed25aaf791a183903f7be5e295fea"
integrity sha512-uJq6108EgZMAl20KagGkzCKfMEjxmKvZHG7Tlq0Z6nOky7YF7aq4mOx6xK8TJ/i1LeK4Qus7INktacctDgY8Ow==
Expand Down

0 comments on commit 138452c

Please sign in to comment.