File tree Expand file tree Collapse file tree 4 files changed +34
-22
lines changed Expand file tree Collapse file tree 4 files changed +34
-22
lines changed Original file line number Diff line number Diff line change @@ -20,19 +20,26 @@ const route = useRoute();
2020
2121const { isAuthenticated, isMePending, authUrl } = storeToRefs (authStore );
2222// Doing this here instead than in the middleware allow reactivity on the auth user
23- watchEffect (async () => {
24- if (isMePending .value ) {
25- return ;
23+ watchEffect (
24+ async () => {
25+ if (isMePending .value ) {
26+ return ;
27+ }
28+ const shouldRedirectToLogin =
29+ ! isAuthenticated .value &&
30+ authUrl .value &&
31+ route .fullPath !== authUrl .value ;
32+ if (shouldRedirectToLogin ) {
33+ return navigateTo (authUrl .value , { external: true });
34+ }
35+ const shouldRedirectToHomepage =
36+ isAuthenticated .value && route .fullPath === authUrl .value ;
37+ if (shouldRedirectToHomepage ) {
38+ return navigateTo (" /" );
39+ }
40+ },
41+ {
42+ flush: " pre" ,
2643 }
27- const shouldRedirectToLogin =
28- ! isAuthenticated .value && authUrl .value && route .fullPath !== authUrl .value ;
29- if (shouldRedirectToLogin ) {
30- return navigateTo (authUrl .value , { external: true });
31- }
32- const shouldRedirectToHomepage =
33- isAuthenticated .value && route .fullPath === authUrl .value ;
34- if (shouldRedirectToHomepage ) {
35- return navigateTo (" /" );
36- }
37- });
44+ );
3845 </script >
Original file line number Diff line number Diff line change 5757defineOptions ({
5858 inheritAttrs: false ,
5959});
60- const props = defineProps <{
60+ defineProps <{
6161 inputId: string ;
6262}>();
6363 </script >
Original file line number Diff line number Diff line change 33 <div class =" col-2 col-offset-8" ></div >
44 <div class =" col-2" >
55 <p
6- v-if =" username "
6+ v-if =" authStore.me?.email "
77 v-t =" {
88 path: 'components.layout.appHeader.welcome',
9- args: { username },
9+ args: { username: authStore.me.email },
1010 }"
1111 ></p >
1212 </div >
1616import { useAuthUser } from " ~/store/auth" ;
1717
1818const authStore = useAuthUser ();
19-
20- const username = authStore .me .email || " " ;
2119 </script >
Original file line number Diff line number Diff line change @@ -21,15 +21,22 @@ const logout = (fetcher: AppFetch<any>) => {
2121} ;
2222
2323export const useAuthUser = defineStore ( "auth-store" , ( ) => {
24- const me = ref ( ) ;
24+ const me : Ref < null | User > = ref ( null ) ;
2525 const { error, resetError, setError } = useBasicError ( ) ;
2626 const isMePending = ref ( false ) ;
2727 const authUrl = ref ( "/auth/login" ) ;
2828 const refresh = async ( $appFetch : AppFetch < User | undefined > ) => {
2929 resetError ( ) ;
3030 isMePending . value = true ;
3131 try {
32- const res = await $appFetch ( "auth/me" ) ;
32+ const res = await $appFetch ( "auth/me" , {
33+ // This override the default behavior of onResponse interceptor from $appFetch
34+ // Because when logout, the next /me gonna answer 401 but you dont want to display any errors
35+ onResponse : ( ) => { } ,
36+ } ) ;
37+ if ( ! res ) {
38+ throw createError ( "res expect a value" ) ;
39+ }
3340 me . value = res ;
3441 } catch ( exception : any ) {
3542 isMePending . value = false ;
@@ -48,7 +55,7 @@ export const useAuthUser = defineStore("auth-store", () => {
4855 } ;
4956 const logoutUser = async ( fetch : AppFetch < any > ) => {
5057 await logout ( fetch ) ;
51- resetAuth ( ) ;
58+ await refresh ( fetch ) ;
5259 } ;
5360
5461 return {
You can’t perform that action at this time.
0 commit comments