File tree Expand file tree Collapse file tree 6 files changed +92
-9
lines changed
Expand file tree Collapse file tree 6 files changed +92
-9
lines changed Original file line number Diff line number Diff line change 11{
22 "base" : {
3+ "action" : {
4+ "open_user_menu" : " Open user menu"
5+ },
36 "beaconchain_homepage" : " Beaconchain Homepage" ,
47 "common" : {
58 "close" : " Close" ,
Original file line number Diff line number Diff line change 1+ export const useUserSession = ( ) => {
2+ const data = useState < Awaited < ReturnType < typeof getUser > > > ( 'user-session' )
3+ const hasSession = computed ( ( ) => ! ! data . value )
4+ const requestFetch = useRequestFetch ( )
5+ const getUser = async ( ) => {
6+ const result = await requestFetch ( '/api/bff/users/me' )
7+ . catch ( ( error ) => {
8+ // session_id invalid or expired
9+ if ( error ?. status === 401 ) return
10+ throw createError ( {
11+ statusCode : error ?. status || 500 ,
12+ statusMessage : error ?. statusMessage || 'Unknown error' ,
13+ } )
14+ } )
15+ data . value = result
16+ return result
17+ }
18+ return {
19+ data,
20+ getUser,
21+ hasSession,
22+ }
23+ }
Original file line number Diff line number Diff line change 1+ export default defineNuxtRouteMiddleware ( async ( ) => {
2+ const {
3+ getUser,
4+ hasSession,
5+ } = useUserSession ( )
6+ if ( ! hasSession . value ) {
7+ await callOnce ( async ( ) => {
8+ await getUser ( )
9+ } )
10+ }
11+ } )
Original file line number Diff line number Diff line change 1+ export default defineNuxtConfig ( {
2+ $meta : {
3+ name : 'auth' ,
4+ } ,
5+ } )
Original file line number Diff line number Diff line change 1+ import type { FetchError } from 'ofetch'
2+ import type { InternalGetUserInfoResponse } from '~/types/api/user'
3+
4+ const config = useRuntimeConfig ( )
5+ const headers = {
6+ 'x-ssr-secret' : config . private . ssrSecret ,
7+ }
8+
9+ export default defineEventHandler ( async ( event ) => {
10+ const hasSessionCookie = getCookie ( event , 'session_id' )
11+ if ( ! hasSessionCookie ) return
12+
13+ try {
14+ return await event . $fetch < InternalGetUserInfoResponse > ( '/users/me' , {
15+ baseURL : config . public . apiClient ,
16+ headers,
17+ } ) . then ( ( { data } ) => data )
18+ }
19+ catch ( error ) {
20+ throw createError ( {
21+ statusCode : ( error as FetchError ) . statusCode ,
22+ statusMessage : ( error as FetchError ) . statusMessage ,
23+ } )
24+ }
25+ } )
Original file line number Diff line number Diff line change 11<script setup lang="ts">
22import type { BaseNavigationItem } from ' #layers/base/app/components/BaseNavigationItem.vue'
33
4- const { navigateToV1Login } = useV1Login ()
4+ defineProps <{
5+ items: BaseNavigationItem [],
6+ }>()
7+ const {
8+ navigateToV1Login,
9+ } = useV1Login ()
510const { t : $t } = useTranslation ()
611const emit = defineEmits <{
712 (e : ' open' ): void ,
813}>()
914const handleClick = () => {
1015 emit (' open' )
1116}
12- defineProps <{
13- items: BaseNavigationItem [],
14- }>()
17+ const { hasSession } = useUserSession ()
18+ const v1Domain = useV1Domain ()
1519 </script >
1620
1721<template >
@@ -50,11 +54,23 @@ defineProps<{
5054 />
5155 </li >
5256 </ul >
53- <BaseButton
54- @click =" navigateToV1Login"
55- >
56- {{ $t('base.common.log_in') }}
57- </BaseButton >
57+ <span >
58+ <span v-if =" hasSession" >
59+ <BaseButtonIcon
60+ variant =" secondary"
61+ screenreader-text =" base.action.open_user_menu"
62+ name =" user"
63+ @click =" navigateTo(`${v1Domain}/user/settings`, { external: true })"
64+ />
65+ </span >
66+ <span v-else >
67+ <BaseButton
68+ @click =" navigateToV1Login"
69+ >
70+ {{ $t('base.common.log_in') }}
71+ </BaseButton >
72+ </span >
73+ </span >
5874 </div >
5975 </nav >
6076</template >
You can’t perform that action at this time.
0 commit comments