Benjiro
Benjiro

Reputation: 121

How to check if a user is online in laravel vue.js

I am trying to check if a user is online so I can show a green circle this is my template

<template>
<div>
    <span class="fas fa-circle pull-right text-success" v-if="checkUser"></span>
    <span class="fas fa-circle pull-right text-danger" v-else></span>
</div>
</template>

this is my script with props

<script>
export default {
    name: "OnlineUser",
    props: ['contact', 'onlineusers'],
    data(){
      return{
      }
    },
    methods:{

    },
    computed:{
        checkUser() {
            return  _.find(this.onlineusers, {id: this.contact});

        },
    },
    mounted() {

    },
    created() {

    }
}
</script>

when I check the vue dev tool, all the online users show in there but I tried saving the result of my function to see what it returns and it always return undefined intead of true or false

Upvotes: 0

Views: 915

Answers (2)

Robert Kujawa
Robert Kujawa

Reputation: 997

Looks like you are using lodash to check if the user exists in the onlineusers array.

This is not an error, this is expected behavior, just like the docs say for the _.find() method:

Returns the matched element, else undefined.

I would recommend using the _.some() method:

checkUser() {
    return  _.some(this.onlineusers, ['id', this.contact.id});
}

Just like the docs say:

Returns true if any element passes the predicate check, else false.

Upvotes: 0

GabMic
GabMic

Reputation: 1482

Well, the most easy way is to define it like this:

 ```<script>
        window.loggedIn = {!! json_encode([
        'signedIn'=>Auth::check(),
    ]) !!};
    <script/>```

That way you will have a global variable named "loggedIn" that will give you true or false if the user is online or not.

Upvotes: 0

Related Questions