-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nuxt-base): initial adapter setup
- Loading branch information
Showing
9 changed files
with
246 additions
and
10 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
104 changes: 104 additions & 0 deletions
104
space-plugins/nuxt-base/app-extension-auth/storyblok-auth-api/createSupabaseClient.ts
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,104 @@ | ||
import { createClient } from '@supabase/supabase-js'; | ||
|
||
export const supabaseClient = createClient( | ||
process.env.SUPABASE_URL || '', | ||
process.env.SUPABASE_KEY || '', | ||
); | ||
|
||
type AppSession = { | ||
appClientId: string; | ||
userId: string | number; | ||
spaceId: string | number; | ||
refreshToken: string; | ||
accessToken: string; | ||
expiresAt: number; | ||
roles: string[]; | ||
spaceName: string; | ||
userName: string; | ||
region: string; | ||
}; | ||
|
||
type GetSessionParams = Pick<AppSession, 'spaceId' | 'userId' | 'appClientId'>; | ||
|
||
type SetSession = (session: AppSession) => Promise<void>; | ||
type GetSession = (params: GetSessionParams) => Promise<void>; | ||
|
||
export type Adapter = { | ||
getSession: GetSession; | ||
setSession: SetSession; | ||
}; | ||
|
||
// ADD: update session to supabase? | ||
// ADD: isSessionValid? | ||
export const createSupabaseClient = (url: string, key: string): Adapter => { | ||
const supabaseClient = createClient(url || '', key || ''); | ||
|
||
const setSession: SetSession = async (session) => { | ||
const { | ||
spaceId, | ||
spaceName, | ||
userName, | ||
userId, | ||
region, | ||
refreshToken, | ||
roles, | ||
expiresAt, | ||
appClientId, | ||
accessToken, | ||
} = session; | ||
|
||
const { data, error } = await supabaseClient | ||
.from('session_kv_test') | ||
.upsert({ | ||
space_id: spaceId, | ||
user_id: userId, | ||
app_client_id: appClientId, | ||
key: 'session', | ||
value: { | ||
refresh_token: refreshToken, | ||
access_token: accessToken, | ||
expires_at: new Date(expiresAt).toISOString(), | ||
roles, | ||
space_name: spaceName, | ||
user_name: userName, | ||
region, | ||
}, | ||
}); | ||
|
||
console.log('error', error); | ||
}; | ||
|
||
const isSessionExpired = (session: AppSession) => { | ||
session; | ||
}; | ||
|
||
const getSession: GetSession = async (params) => { | ||
//TODO: refresh session | ||
const { spaceId, userId, appClientId } = params; | ||
const { data, error } = await supabaseClient | ||
.from('session_test') | ||
.select() | ||
.eq('space_id', spaceId) | ||
.eq('user_id', userId) | ||
.eq('app_client_id', appClientId); | ||
|
||
console.log('error', error); | ||
}; | ||
|
||
return { | ||
getSession, | ||
setSession, | ||
}; | ||
}; | ||
|
||
const cookieAdapter = () => { | ||
return { | ||
getSession: null, | ||
setSession: null, | ||
}; | ||
}; | ||
|
||
export const supabaseAdapter = createSupabaseAdapter( | ||
process.env.SUPABASE_URL || '', | ||
process.env.SUPABASE_KEY || '', | ||
); |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.