-
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.
Merge pull request #132 from vuejs-jp/feature/invite-user-admin-setup…
…-etc feat: invite user, admin setup
- Loading branch information
Showing
22 changed files
with
606 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
# Supabase | ||
|
||
昨年に続いて [Supabase](https://supabase.com/) のお世話になります。今年は事前に **本番投入前チェックのひとつ** として、Supabase 公式より出されていた [Production Checklist](https://supabase.com/docs/guides/platform/going-into-prod) も一読しておくと良いように考えています。 | ||
|
||
## Supabase 環境を構築 | ||
|
||
ダッシュボードより各種変数を発行、手元の環境でそれらを使えるよう準備します。 | ||
|
||
データベースへの追加、更新する際は URL (SUPABASE_URL) と Anon Key (SUPABASE_KEY) が必要となります。 | ||
|
||
```.env | ||
SUPABASE_URL= | ||
SUPABASE_KEY= | ||
``` | ||
|
||
[`useRuntimeConfig`](https://nuxt.com/docs/api/composables/use-runtime-config) を利用して、各種変数へアクセスできることを確認してください。 | ||
|
||
```ts | ||
export default defineNuxtConfig({ | ||
runtimeConfig: { | ||
public: { | ||
supabaseUrl: process.env.SUPABASE_URL, | ||
supabaseKey: process.env.SUPABASE_KEY, | ||
}, | ||
}, | ||
}) | ||
``` | ||
|
||
なお、ここで supabase.redirect に `false` を設定しないと、強制的にログイン画面へ遷移されるようなります。 | ||
|
||
```ts | ||
export default defineNuxtConfig({ | ||
supabase: { | ||
redirect: false, | ||
}, | ||
}) | ||
``` | ||
|
||
### メールアドレスを使ってユーザーを招待 | ||
|
||
[`inviteUserByEmail`](https://supabase.com/docs/reference/javascript/auth-admin-inviteuserbyemail) のお世話になります。事前に Supabase の Auth Admin クライアントを作成する必要があり、直接 Web ブラウザからそれを操作することができません。 | ||
|
||
Service Role Key も発行しつつ、合わせてこちらも `useRuntimeConfig` を利用してアクセスできることを確認してください。 | ||
|
||
```ts | ||
import { defineEventHandler, useRuntimeConfig } from '#imports' | ||
import { createClient } from '@supabase/supabase-js' | ||
|
||
export default defineEventHandler(async (event) => { | ||
const config = useRuntimeConfig() | ||
const supabaseUrl = config.public.supabaseUrl | ||
const serviceKey = config.public.serviceKey | ||
|
||
if (!supabaseUrl || !serviceKey) { | ||
return event.node.res.end('No authentication') | ||
} | ||
|
||
const supabase = createClient(supabaseUrl, serviceKey) | ||
const { error } = await supabase.auth.admin.inviteUserByEmail(email) | ||
|
||
if (error) { | ||
return event.node.res.end(error) | ||
} | ||
}) | ||
``` | ||
|
||
raw_user_meta_data に `{ user_role: 'admin' }` を付与しつつ、それが追加された時に限って public.admin_users へデータが insert される設計を取りました。 | ||
|
||
```ts | ||
const { error } = await supabase.auth.admin.inviteUserByEmail( | ||
email, | ||
{ data: { user_role: 'admin' } }, | ||
) | ||
``` | ||
|
||
### API を利用してユーザーを削除 | ||
|
||
Supabase 管理画面よりユーザーを削除する操作を行えないため、API ([`deleteUser`](https://supabase.com/docs/reference/javascript/auth-admin-deleteuser)) のお世話になります。 | ||
|
||
ユーザーの招待時と同じく、事前に Supabase の Auth Admin クライアントを作成する必要があり、直接 Web ブラウザからそれを操作することができません。 | ||
|
||
```ts | ||
import { defineEventHandler, useRuntimeConfig } from '#imports' | ||
import { createClient } from '@supabase/supabase-js' | ||
|
||
export default defineEventHandler(async (event: H3Event) => { | ||
const config = useRuntimeConfig() | ||
const supabaseUrl = config.public.supabaseUrl | ||
const serviceKey = config.public.serviceKey | ||
|
||
if (!supabaseUrl || !serviceKey) { | ||
return event.node.res.end('No authentication') | ||
} | ||
|
||
const supabase = createClient(supabaseUrl, serviceKey) | ||
const { error } = await supabase.auth.admin.deleteUser(id) | ||
|
||
if (error) { | ||
return event.node.res.end(error) | ||
} | ||
}) | ||
``` |
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,25 @@ | ||
<script setup lang="ts"> | ||
import type { AdminPage } from '@vuejs-jp/model' | ||
interface ListProps { | ||
page: AdminPage | ||
} | ||
const props = defineProps<ListProps>() | ||
const pageText = props.page.replace(/^[a-z]/g, function (val) { | ||
return val.toUpperCase() | ||
}) | ||
</script> | ||
|
||
<template> | ||
<div class="tab-content"> | ||
<h2>{{ pageText }}</h2> | ||
</div> | ||
</template> | ||
|
||
<style scoped> | ||
.tab-content { | ||
margin: 0 1.5%; | ||
} | ||
</style> |
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,26 @@ | ||
import { useSupabaseClient } from '#imports' | ||
import type { AuthProvider, RedirectPath } from '@vuejs-jp/model' | ||
import { REDIRECT_URL } from '~/utils/environment.constants' | ||
|
||
export function useAuth() { | ||
const supabase = useSupabaseClient() | ||
|
||
async function signIn(provider: AuthProvider, path: RedirectPath) { | ||
const { error } = await supabase.auth.signInWithOAuth({ | ||
provider, | ||
options: { | ||
redirectTo: `${REDIRECT_URL}${path}`, | ||
} | ||
}) | ||
if (error) console.log(error) | ||
} | ||
|
||
async function signOut() { | ||
const { error } = await supabase.auth.signOut() | ||
if (error) { | ||
throw new Error('can not signout') | ||
} | ||
} | ||
|
||
return { signIn, signOut } | ||
} |
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,46 @@ | ||
import { createClient, type AuthChangeEvent } from '@supabase/supabase-js' | ||
import { match } from 'ts-pattern' | ||
import { useRuntimeConfig } from '#imports' | ||
import { computed, ref } from 'vue' | ||
|
||
export function useAuthSession() { | ||
const config = useRuntimeConfig() | ||
const supabaseUrl = config.public.supabaseUrl | ||
const supabaseKey = config.public.supabaseKey | ||
|
||
let _onAuthChanged: (e: AuthChangeEvent) => void = () => {} | ||
const onAuthChanged = (callback: (e: AuthChangeEvent) => void) => { | ||
_onAuthChanged = callback | ||
} | ||
|
||
if (!supabaseUrl || !supabaseKey) { | ||
return { onAuthChanged } | ||
} | ||
const supabase = createClient(supabaseUrl, supabaseKey) | ||
|
||
const signedUserId = ref<string | null>(null) | ||
const setSignedUserId = (target: string | null) => signedUserId.value = target | ||
|
||
const hasAuth = computed(() => signedUserId.value !== null) | ||
|
||
supabase.auth.onAuthStateChange((e, session) => { | ||
match(e) | ||
.with('INITIAL_SESSION', 'SIGNED_IN', () => { | ||
if (session?.user) setSignedUserId(session.user.id) | ||
_onAuthChanged(e) | ||
}) | ||
.with('SIGNED_OUT', () => { | ||
setSignedUserId(null) | ||
}) | ||
.with( | ||
'MFA_CHALLENGE_VERIFIED', | ||
'PASSWORD_RECOVERY', | ||
'TOKEN_REFRESHED', | ||
'USER_UPDATED', | ||
() => {}, | ||
) | ||
.exhaustive() | ||
}) | ||
|
||
return { hasAuth, onAuthChanged } | ||
} |
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,32 @@ | ||
import { computed, ref } from 'vue' | ||
import { useFormError } from './useFormError' | ||
|
||
export function useInvitation() { | ||
const email = ref('') | ||
const id = ref('') | ||
const { ...rest } = useFormError() | ||
|
||
const isSubmittingId = computed(() => { | ||
if (!id.value) return false | ||
return rest.idError.value === '' | ||
}) | ||
|
||
const isSubmittingEmail = computed(() => { | ||
if (!email.value) return false | ||
return rest.emailError.value === '' | ||
}) | ||
|
||
async function publish(type: 'invite' | 'delete', target: string) { | ||
await $fetch(`/api/${type}-user`, { | ||
method: 'post', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Access-Control-Allow-Origin': '*', | ||
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type', | ||
}, | ||
body: JSON.stringify(type === 'invite' ? { email: target } : { id: target }), | ||
}) | ||
} | ||
|
||
return { publish, isSubmittingId, isSubmittingEmail, email, id, ...rest } | ||
} |
Oops, something went wrong.