mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +00:00
improved login process to use fetch() without full page reload and comments
This commit is contained in:
parent
fb2c03a0b9
commit
cc354886f2
@ -49,16 +49,27 @@ Create a new page in your Nuxt app for user registration. This page should have
|
||||
error.value = null;
|
||||
|
||||
// perform the login
|
||||
try {
|
||||
await $fetch("/api/register", {
|
||||
method: "POST",
|
||||
body: form.value,
|
||||
onResponseError: () => {
|
||||
error.value = "Error Registering User";
|
||||
},
|
||||
});
|
||||
} catch (e) {
|
||||
// if there's an error, set the error message and return early
|
||||
error.value = "Error Registering User";
|
||||
return;
|
||||
}
|
||||
// refresh the session status now that the user is logged in
|
||||
const { fetch } = useUserSession();
|
||||
await fetch();
|
||||
// you may want to use something like Pinia to manage global state of the logged-in user
|
||||
// update Pinia state here...
|
||||
|
||||
// we're not using Nuxt Router here so that we can easily trigger a whole page load and get everything refreshed now that the user is logged in
|
||||
window.location.href = "/login";
|
||||
// take the user to the auth-only users index page now that they're logged in
|
||||
await navigateTo("/users");
|
||||
|
||||
// Alternative - Don't use Nuxt Router here so that we can easily trigger a whole page load and get the whole UI refreshed now that the user is logged in.
|
||||
// window.location.href = "/users";
|
||||
}
|
||||
</script>
|
||||
|
||||
@ -124,6 +135,10 @@ export default defineEventHandler(async (event) => {
|
||||
email: body.email,
|
||||
password: passwordHash,
|
||||
});
|
||||
|
||||
// log the user in as the user that was just created
|
||||
const user = (await db.select().from(users).where(eq(users.email, body.email)).limit(1))[0];
|
||||
await auth.login(event, user);
|
||||
});
|
||||
```
|
||||
|
||||
@ -151,16 +166,28 @@ This is a very simple login form example, so you'd definitely want to add more v
|
||||
error.value = null;
|
||||
|
||||
// perform the login
|
||||
try {
|
||||
await $fetch("/api/auth/login", {
|
||||
method: "POST",
|
||||
body: form.value,
|
||||
onResponseError: () => {
|
||||
error.value = "Error logging in";
|
||||
},
|
||||
body: loginForm.value,
|
||||
});
|
||||
} catch (e) {
|
||||
// if there's an error, set the error message and return early
|
||||
error.value = "Error logging in";
|
||||
return;
|
||||
}
|
||||
|
||||
// we're not using Nuxt Router here so that we can easily trigger a whole page load and get everything refreshed now that the user is logged in
|
||||
window.location.href = "/";
|
||||
// refresh the session status now that the user is logged in
|
||||
const { fetch } = useUserSession();
|
||||
await fetch();
|
||||
// you may want to use something like Pinia to manage global state of the logged-in user
|
||||
// update Pinia state here...
|
||||
|
||||
// take the user to the auth-only users index page now that they're logged in
|
||||
await navigateTo("/users");
|
||||
|
||||
// Alternative - Don't use Nuxt Router here so that we can easily trigger a whole page load and get the whole UI refreshed now that the user is logged in.
|
||||
// window.location.href = "/users";
|
||||
}
|
||||
</script>
|
||||
|
||||
@ -221,7 +248,7 @@ import bcrypt from "bcrypt";
|
||||
}
|
||||
|
||||
// set the session
|
||||
await setUserSession(event, {
|
||||
await replaceUserSession(event, {
|
||||
user: {
|
||||
id: user.id,
|
||||
name: user.name,
|
||||
|
Loading…
Reference in New Issue
Block a user