Nuxt.js: save state on server side

Created on 3 Apr 2017  路  10Comments  路  Source: nuxt/nuxt.js

Hi guys,
i like to save the auth state (token and user) on the server-side so that when i refresh the page i keep the user logged in?

i tried using nuxtServerInit() like that

image

but when i refresh on a url that have an auth middleware, i get a redirection to login page.

This question is available on Nuxt.js community (#c440)
help-wanted

Most helpful comment

@aft3rlife I don't have experience in Laravel and last time I coded in PHP was 10 years ago, but it would be awesome to see someone porting Nuxt.js to Laravel!

All 10 comments

Hi @aymenpeak

You need to use sessions on your server and use req.session.

You can use the express with the session middleware: https://github.com/expressjs/session

but express exclude laravel api , no?

How are you using laravel with nuxt.js exactly?

hi @Atinux
i'm implementing a single page app for streaming videos, i get all the data using laravel api, but the user needs to be authenticated to use some features,
i managed to login and get the token from the laravel server but when i refresh i loose the data saved in nuxt store.

so is there a way to the state saved even when there is a server rendering (refresh)?
thanks for asking by the way because im really strugglin^^

@aymenpeak please take a look at this example: https://github.com/nuxt/example-todomvc

Mainly to this file: https://github.com/nuxt/example-todomvc/blob/master/server.js

In this file I use the express-session middleware to keep the todos for each user, so when he refresh the page the todos are server-rendered (see demo: https://todomvc.nuxtjs.org/)

@Atinux yeah but my problem was that when i refresh my auth middleware redirect me before fetching the user data. So i fixed it by saving the user data in the cookies then retreive them in the req variable.
there is my middleware code
image

Thanks for helping :)

@aymenpeak Could you post an example of your code somewhere? I'm struggling with this as well. I'm using Laravel too and I've seen the auth-0 example but it's not working for me

@Atinux Are you guys planning some sort of Laranuxt at some point? That'd be awesome!

@aft3rlife I don't have experience in Laravel and last time I coded in PHP was 10 years ago, but it would be awesome to see someone porting Nuxt.js to Laravel!

@aft3rlife yeah ofc , first i get rid of some bugs then i will post it :)

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

VincentLoy picture VincentLoy  路  3Comments

bimohxh picture bimohxh  路  3Comments

nassimbenkirane picture nassimbenkirane  路  3Comments

gary149 picture gary149  路  3Comments

jaredreich picture jaredreich  路  3Comments