-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(webapp): use supabase auth token to make API calls
- Loading branch information
1 parent
06dbd0e
commit 543d1c6
Showing
7 changed files
with
356 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { createClient } from '@supabase/supabase-js' | ||
import { AppRouterInstance } from 'next/dist/shared/lib/app-router-context'; | ||
|
||
// Utility function to listen to auth state changes and set and remove cookies accordingly | ||
export function listenToAuthStateChangesAndUpdateCookies() { | ||
const supabase = createClient( | ||
process.env.NEXT_PUBLIC_SUPABASE_URL!, | ||
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY! | ||
) | ||
|
||
supabase.auth.onAuthStateChange((event, session) => { | ||
if (event === 'SIGNED_OUT') { | ||
// delete cookies on sign out | ||
const expires = new Date(0).toUTCString() | ||
document.cookie = `sb-access-token=; path=/; expires=${expires}; SameSite=Lax; secure` | ||
document.cookie = `sb-refresh-token=; path=/; expires=${expires}; SameSite=Lax; secure` | ||
} else if (event === 'SIGNED_IN' || event === 'TOKEN_REFRESHED') { | ||
// set new access + refresh tokens as cookies on sign in/token refresh. | ||
const maxAge = 100 * 365 * 24 * 60 * 60 // 100 years, never expires | ||
document.cookie = `sb-access-token=${session!.access_token}; path=/; max-age=${maxAge}; SameSite=Lax; secure` | ||
document.cookie = `sb-refresh-token=${session!.refresh_token}; path=/; max-age=${maxAge}; SameSite=Lax; secure` | ||
} | ||
}) | ||
} | ||
|
||
// Utility function to try and access current access token. If session retrieval | ||
// fails for any reason, logout will be called and the user will be redirected to auth | ||
export async function getAccessTokenOrRedirectToAuth(router: AppRouterInstance) { | ||
const supabase = createClient( | ||
process.env.NEXT_PUBLIC_SUPABASE_URL!, | ||
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY! | ||
) | ||
|
||
const { data: {session}, error } = await supabase.auth.getSession() | ||
|
||
if(error) { | ||
await supabase.auth.signOut() | ||
router.push('/auth/logout') | ||
return null | ||
} | ||
|
||
return session!.access_token; | ||
} | ||
|
||
// Utility function to check if API reponse has an authentication error. | ||
// If it does, logout will be called and the user will be redirected to auth | ||
export async function logoutIfAuthError(router: AppRouterInstance, res: Response) { | ||
const supabase = createClient( | ||
process.env.NEXT_PUBLIC_SUPABASE_URL!, | ||
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY! | ||
) | ||
|
||
if(res.status === 401) { | ||
await supabase.auth.signOut() | ||
router.push('/auth/logout') | ||
return | ||
} | ||
} |
Oops, something went wrong.