DEV Community

How to add authentication to your universal Nuxt app using nuxt/auth module?

Mandeep Singh Gulati on September 30, 2019

Recently I was working on a Nuxt.js app and had to add authentication to it. First thing I thought was to use vuex to store two fields in a state...
Collapse
 
slidenerd profile image
slidenerd

I appreciate this post but you have not shown anything about what you have done on the server side

Collapse
 
cds profile image
cds

how can i save user to database(mongodb). Can we save the callback/ response from google (user/profile ) to database while adding to vuex store. If yes then how ? I had done some (hack) approch now i am saving user data to db after the user/ profile save to vuex store or store is set to user but i need to save callback directly from google or better approch

Collapse
 
cds profile image
cds

I am working on my own project i dont want to save users password in my db just want to implement google/facebook login without local auth. But i need user email (uniqueid) to save other data for reference later

Collapse
 
bbarbour profile image
Brian Barbour

What's happening on your API end points? Are you returning a token or something?

Collapse
 
richardcullen profile image
RichardCullen

Need to include some sample code to show how to implement the server-side of the Auth API.

Collapse
 
zloypacifist profile image
zloypacifist • Edited

I've made 2 api endpoints on my server localhost:8080 for login and profile.
On login endpoint: if user credentials is valid, set cookie 'myauth' with some unique value (token), and save this token for user in DB.
On profile endpoint: if cookie 'myauth' set and user found by token, return json with it's profile info, for example:
{
"id: 1,
"name": "Jon Snow",
"email": "jon.snow@asoiaf.com"
}
else return 401 status code with empty response.

After this manipulating my nuxt app start to login users. I've used auth middleware in nuxt config for all routings. I've used Lumen framework for api server

Collapse
 
dkwlb profile image
DKWLB

Good article, thanks. But what should i fix with cookie approach if i redirect always to login page if i refresh the page?
Other words if i refresh - i get loggedIn false for some seconds

Collapse
 
johnywhyte profile image
johnywhyte

i can't preserve my auth sate. everything is cleared when i refresh

Collapse
 
muriithigachanja profile image
Ken Gachanja

Nice article but there is no data persistence

Collapse
 
leyterpalacios profile image
leyter palacios

hello, thanks for the tutorial, very good ... you will have an example of this tutorial using auth token from spring security please!

Collapse
 
johnywhyte profile image
johnywhyte

I can't preserve my auth sate. everything is cleared when I refresh

Collapse
 
yiyasha15 profile image
Aishwarya Shrestha • Edited

I followed the same method but my auth state still goes to null on page refresh!
What could be the issue?

Collapse
 
yiyasha15 profile image
Aishwarya Shrestha

Solved it using vuex-persist lugin!