Skip to main content

Show User Data and Logout

Premium
8 min read

In this lesson, we will show the user's name and email and will build the Log out button.

user details in the navigation


Composable Method

First, in the auth Composable, let's add a reactive user object.

resources/composables/auth.js:

import { ref, reactive } from 'vue'
import { useRouter } from "vue-router";
 
const user = reactive({
name: '',
email: '',
})
// ...

Then, after successful login, we need to assign the values to that object.

resources/composables/auth.js:

import { ref, reactive, inject } from 'vue'
import { useRouter } from 'vue-router';
 
const user = reactive({
name: '',
email: '',
})
 
export default function useAuth() {
// ...
 
const loginUser = (response) => {
user.name = response.data.name
user.email = response.data.email
 
localStorage.setItem('loggedIn', JSON.stringify(true))
router.push({ name: 'posts.index' })
}
// ...
return { loginForm, validationErrors, processing, submitLogin }
return { loginForm, validationErrors, processing, submitLogin, user }
}

Now, let's build a method to use...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (30 h 41 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

ZJ
Zsolt Janes ✓ Link copied!

Hi! I like this course so much :)

I found one typo at the end of the lesson: After refreshing the "pag",

N
Nerijus ✓ Link copied!

Thanks!

DS
Dmytro Sakharuk ✓ Link copied!

Actually, we are using the reactive 'user' object exclusively in the Authenticated component. It's unclear why we need to call 'getUser' after mounting the rootComponent?! It would be more appropriate, in my opinion, to call 'getUser' after mounting the Authenticated component. In this case, you don't need to take the reactive 'user' object out of the exported 'useAuth' function.

I
Istvan ✓ Link copied!

Hi! In the getUser method calling the loginUser can make an undesirable redirect.For example Create page refresh will redirect to the Posts page. You can make a showUser method inside it with the response userdata(name, email) and call that in submitLogin and getUser.

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.