Skip to content

Commit 97a8c6e

Browse files
committed
chore: redirect in auth
1 parent 334ff4b commit 97a8c6e

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

packages/nuxt/playground/pages/authentication.vue

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ const auth = useFirebaseAuth()!
2828
const user = useCurrentUser()
2929
let credential: AuthCredential | null = null
3030
31+
const route = useRoute()
32+
const router = useRouter()
33+
34+
// automatically redirect the user if they are logged in but was rejected on the server beacuse of an outdated cookie
35+
onMounted(async () => {
36+
const currentUser = await getCurrentUser()
37+
if (
38+
currentUser &&
39+
route.query.redirect &&
40+
typeof route.query.redirect === 'string'
41+
) {
42+
router.push(route.query.redirect)
43+
}
44+
})
45+
3146
// new user
3247
const email = ref('')
3348
const password = ref('')
@@ -82,30 +97,20 @@ onMounted(() => {
8297
<template>
8398
<main>
8499
<h1>Auth playground</h1>
85-
<button @click="signOut(auth)">
86-
SignOut
87-
</button>
88-
<button @click="signInAnonymously(auth)">
89-
Anonymous signIn
90-
</button>
91-
<button @click="signinPopup()">
92-
Signin Google (popup)
93-
</button>
94-
<button @click="signinRedirect()">
95-
Signin Google (redirect)
96-
</button>
97-
<button @click="changeUserImage">
98-
Change User picture
99-
</button>
100+
<button @click="signOut(auth)">SignOut</button>
101+
<button @click="signInAnonymously(auth)">Anonymous signIn</button>
102+
<button @click="signinPopup()">Signin Google (popup)</button>
103+
<button @click="signinRedirect()">Signin Google (redirect)</button>
104+
<button @click="changeUserImage">Change User picture</button>
100105

101106
<form @submit.prevent="signUp()">
102107
<fieldset>
103108
<legend>New User</legend>
104109

105-
<label> Email: <input v-model="email" type="email" required> </label>
110+
<label> Email: <input v-model="email" type="email" required /> </label>
106111

107112
<label>
108-
Password: <input v-model="password" type="password" required>
113+
Password: <input v-model="password" type="password" required />
109114
</label>
110115

111116
<button>Create</button>
@@ -116,26 +121,26 @@ onMounted(() => {
116121
<fieldset>
117122
<legend>Sign in</legend>
118123

119-
<label> Email: <input v-model="email" type="email" required> </label>
124+
<label> Email: <input v-model="email" type="email" required /> </label>
120125

121126
<label>
122-
Password: <input v-model="password" type="password" required>
127+
Password: <input v-model="password" type="password" required />
123128
</label>
124129

125130
<button>Signin</button>
126131
</fieldset>
127132
</form>
128133

129134
<p v-if="user">
130-
Name: {{ user.displayName }} <br>
135+
Name: {{ user.displayName }} <br />
131136
<img
132137
v-if="user.photoURL"
133138
:src="user.photoURL"
134139
referrerpolicy="no-referrer"
135-
>
140+
/>
136141
</p>
137142

138-
<hr>
143+
<hr />
139144

140145
<!-- this is for debug purposes only, displaying it on the server would create a hydration mismatch -->
141146
<ClientOnly>

0 commit comments

Comments
 (0)