Skip to content

Commit 2c79836

Browse files
committed
fix: exit edit view without auth
1 parent 5d66b55 commit 2c79836

File tree

4 files changed

+42
-11
lines changed

4 files changed

+42
-11
lines changed

app/composables/useAuth.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import { match } from 'ts-pattern'
44
import { AuthProvider, FormUser } from '~/types/app'
55
import { storeKey } from '~/atoms/user'
66

7+
export enum AuthChangeEvent {
8+
INITIAL_SESSION = 'INITIAL_SESSION',
9+
SIGNED_IN = 'SIGNED_IN',
10+
SIGNED_OUT = 'SIGNED_OUT',
11+
}
12+
713
const initialUser = {
814
user_id: '',
915
full_name: '',
@@ -39,11 +45,15 @@ const useAuth = () => {
3945
})
4046

4147
const store = inject(storeKey)
48+
let _onAuthChanged: (evt: string) => void = () => {}
49+
const onAuthChanged = (callback: (evt: string) => void) => {
50+
_onAuthChanged = callback
51+
}
4252

4353
const supabase = getClient()
4454
supabase.auth.onAuthStateChange((evt, session) => {
4555
match(evt)
46-
.with('INITIAL_SESSION', 'SIGNED_IN', () => {
56+
.with(AuthChangeEvent.INITIAL_SESSION, AuthChangeEvent.SIGNED_IN, () => {
4757
if (session?.user) {
4858
const { user } = session
4959
signedUser.user_id = user.id || ''
@@ -57,8 +67,9 @@ const useAuth = () => {
5767
// signedUser.receipt_id = user?.receipt_id || ''
5868
store?.setUser(signedUser)
5969
}
70+
_onAuthChanged(evt)
6071
})
61-
.with('SIGNED_OUT', () => {
72+
.with(AuthChangeEvent.SIGNED_OUT, () => {
6273
Object.entries(initialUser).forEach(([key, value]) => {
6374
signedUser[key as keyof FormUser] = value
6475
})
@@ -95,7 +106,7 @@ const useAuth = () => {
95106
return signedUser.user_id !== ''
96107
})
97108

98-
return { signOut, signIn, signedUser, hasAuth }
109+
return { signOut, signIn, signedUser, hasAuth, onAuthChanged }
99110
}
100111

101112
export function getClient() {

app/pages/users/[id].vue

+7-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ definePageMeta({
1919
2020
const route = useRoute()
2121
const userId = route.params.id as string
22+
const urlBasePath = useRuntimeConfig().app.baseURL
2223
const { signOut, hasAuth } = useAuth()
2324
const { signedUser } = useUserStore()
2425
const { eventUser, error } = await useUser(userId)
@@ -105,7 +106,7 @@ useHead({
105106
:opacity="eventUser?.activated_at ? 1 : 0.6"
106107
/>
107108
<!-- 再編集 -->
108-
<RoundButton class="btn-update" :to="`/users/edit/${userId}`">
109+
<RoundButton class="btn-update" :to="`/users/edit/${userId}`" :disabled="!hasAuth">
109110
{{ $t('words.re_edit') }}
110111
</RoundButton>
111112
<!--
@@ -131,7 +132,7 @@ useHead({
131132
{{ $t('words.add_to_calendar') }}
132133
</RoundButton>
133134
<!-- トップに戻る -->
134-
<RoundButton to="/" outline>
135+
<RoundButton :to="urlBasePath" outline>
135136
{{ $t('words.back_top') }}
136137
</RoundButton>
137138
<div v-if="eventUser?.activated_at" class="social">
@@ -173,7 +174,7 @@ useHead({
173174
<style lang="ts" scoped>
174175
css({
175176
'section': {
176-
marginTop: '120px',
177+
marginTop: '140px',
177178
display: 'grid',
178179
placeItems: 'center',
179180
gap: '40px',
@@ -196,6 +197,9 @@ css({
196197
gap: 'calc({space.8} * 5)',
197198
},
198199
'@mobile': {
200+
'section': {
201+
marginTop: '120px',
202+
},
199203
'.btn-update, .btn-save, .btn-calendar': {
200204
marginTop: 'calc({space.8} * 0)'
201205
},

app/pages/users/edit/[id].vue

+20-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import UploadLogo from '~/assets/logo/upload_logo.svg'
77
import TextButton from '~/components/button/TextButton.vue'
88
import DragDropArea from '~/components/DragDropArea.vue'
99
import UserForDev from '~/components/UserForDev.vue'
10-
import useAuth from '~/composables/useAuth'
10+
import useAuth, { AuthChangeEvent } from '~/composables/useAuth'
1111
import { useUserStore } from '~/composables/useUserStore'
1212
import { useUser } from '~/composables/useUser'
1313
import { useSupabase } from '~/composables/useSupabase'
@@ -18,9 +18,20 @@ definePageMeta({
1818
middleware: ['error'],
1919
})
2020
21+
const router = useRouter()
2122
const route = useRoute()
2223
const userId = route.params.id as string
23-
const { hasAuth, signOut } = useAuth()
24+
const { hasAuth, signOut, onAuthChanged } = useAuth()
25+
const userPagePath = `/users/${userId}`
26+
27+
onAuthChanged((evt: string) => {
28+
if (evt === AuthChangeEvent.INITIAL_SESSION) {
29+
if (!hasAuth.value) {
30+
router.push(userPagePath)
31+
}
32+
}
33+
})
34+
2435
const { signedUser } = useUserStore()
2536
const { updateEventUser, uploadAvatar, getFullAvatarUrl } = useSupabase()
2637
const { eventUser } = await useUser(userId)
@@ -134,7 +145,7 @@ const updateReceiptId = (value: string) => {
134145

135146
<div class="link-box">
136147
<!-- キャンセル -->
137-
<RoundButton :to="`/users/${userId}`" outline> キャンセル </RoundButton>
148+
<RoundButton :to="userPagePath" outline> キャンセル </RoundButton>
138149
<!-- 確定 -->
139150
<SubmitButton :disabled="!isSubmitting"> 確定 </SubmitButton>
140151
</div>
@@ -162,7 +173,7 @@ const updateReceiptId = (value: string) => {
162173
<style lang="ts" scoped>
163174
css({
164175
'section': {
165-
marginTop: '120px',
176+
marginTop: '140px',
166177
display: 'grid',
167178
placeItems: 'center',
168179
gap: '40px',
@@ -220,5 +231,10 @@ css({
220231
justifyContent: 'center',
221232
gap: '40px'
222233
},
234+
'@mobile': {
235+
'section': {
236+
marginTop: '120px',
237+
},
238+
},
223239
})
224240
</style>

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
// https://nuxt.com/docs/guide/concepts/typescript
33
"extends": "./.nuxt/tsconfig.json",
44
"compilerOptions": {
5-
"types": ["@nuxt/types", "@nuxtjs/i18n", "cypress", "node"]
5+
"types": ["@nuxt/types", "@nuxtjs/i18n", "@nuxtjs/device", "cypress", "node"]
66
}
77
}

0 commit comments

Comments
 (0)