Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nuxt auth page reload problem #1283

Closed
mostafizurhimself opened this issue Aug 31, 2021 · 12 comments
Closed

Nuxt auth page reload problem #1283

mostafizurhimself opened this issue Aug 31, 2021 · 12 comments
Labels

Comments

@mostafizurhimself
Copy link

Version

module: ^5.0.0-1624817847.21691f1
nuxt: "^2.15.7"

When I log in with nuxt auth, I redirect the user to his profile page. But after reload on the profile page I get an error like this:

Can not read property 'name' of null

I have searched this problem related to @nuxtjs/auth-next ssr problem. Most of the solution is to downgrade the @nuxtjs/axios version. I have already tried this but not fixed.

Here is my profile page code:

<template>
    <div v-show="$auth.user">
        <!-- profile image -->
        <div class="w-full flex">
            <div>
                <div class="bg-gray-200 p-5 w-40 h-44"></div>
            </div>
            <div class="ml-6">
                <h2 class="font-bold text-2xl mb-4">Profile Details</h2>
                <h6 class="font-semibold text-lg">{{$auth.user.name}}</h6>
                <p><span class="font-medium">Email: </span>{{$auth.user.email}}</p>
                <p><span class="font-medium">Phone: </span>{{$auth.user.phone}}</p>
                <p><span class="font-medium">Last Login: </span>{{$auth.user.lastLoginForHuman}}</p>
                <nuxt-link to="/customer/change-password" class="block mt-3 text-primary-500 text-sm hover:underline">Change Password</nuxt-link>
            </div>

        </div>

        <!-- details -->
        <div class="w-full mt-5">
            <div class="grid grid-cols-2 sm:grid-cols-2 gap-4">
                <input-field label="First name"></input-field>
                <input-field label="Last name"></input-field>
                <input-field type="email" label="Email"></input-field>
                <input-field label="Phone"></input-field>
            </div>

            <div class="flex justify-end">
                <button-field label="Update" class="bg-primary-500 text-white py-3 px-10"></button-field>
            </div>
        </div>

    </div>
</template>

<script>
export default {
    name: "Profile",

    data() {
        return {
            form: {
                firstName: this.$auth.user?.firstName,
                lastName: this.$auth.user?.lastName,
                email: this.$auth.user?.email,
                phone: this.$auth.user?.phone,
            },
            photo: null,
        };
    },
};
</script>
@CodyBontecou
Copy link

This line <h6 class="font-semibold text-lg">{{$auth.user.name}}</h6> is probably what's causing the issue. Are you sure your user has a name value?

Check your dev tools vuex User value to see if your user state has a name value. If not, make sure your backend is providing the data you expect.

Screen Shot 2021-08-31 at 13 10 58

@mostafizurhimself
Copy link
Author

mostafizurhimself commented Aug 31, 2021 via email

@devzom
Copy link

devzom commented Aug 31, 2021

Shouldn't be a problem with axios or nuxt-auth.

for the first maybe use v-if here

<template>
    <div v-if="$auth.user">

and check if after refresh the page there is going a request to the endpoint which fetch user data mostly -> auth/me set by default in nuxt-auth. Check the vue-dev-tools if $auth.user exist

@alimuradov
Copy link

If you set the ssr: false in file nuxt.config.js then the problem will be solved.
But this solution is probably not suitable for everyone.

@mjedari
Copy link

mjedari commented Sep 26, 2021

Yeah, I have the exact problem. Auth middleware does not recognize user is logged in when on ssr mode.

@Tcharlyson
Copy link

Same problem here

@mjedari
Copy link

mjedari commented Oct 20, 2021

Same problem here

I've fixed it by passing referer header in nuxt-auth requests.

@Tcharlyson
Copy link

Same problem here

I've fixed it by passing referer header in nuxt-auth requests.

Do you have an example of your implementation ? Thank you

@mjedari
Copy link

mjedari commented Oct 22, 2021

Same problem here

I've fixed it by passing referer header in nuxt-auth requests.

Do you have an example of your implementation ? Thank you

Sure, here I completely explained it:

#1244 (comment)

@Tcharlyson
Copy link

Same problem here

I've fixed it by passing referer header in nuxt-auth requests.

Do you have an example of your implementation ? Thank you

Sure, here I completely explained it:

#1244 (comment)

Unfortunately, this is not working

@irsyaadbp
Copy link

irsyaadbp commented Dec 27, 2021

i have same problem, but now i fixed it
if you setting the proxy with axios there is the problem, because the proxy not work in SSR mode
so i setting base url too
image

then in the endpoint setting in nuxt/auth, i remove the /api (proxy) only on user endpoint
image

and dont forget to setting autofetch to false, because this run in client side, and because we remove the proxy(/api) from user endpoint it will cause error 404 not found
image

so when the login page we manually fetch the user
image

you can see my full config for nuxt/auth here https://pastebin.com/9d5PbfBN
and yeah its work!

@bmulholland
Copy link
Contributor

I think all the tips here are already captured in #1197

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants