Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement Auth0 server-side with Nuxtjs?

I have a Nuxt app with authentication already running in universal mode.

I'm trying to convert the authentication service to Auth0. I'm following the Vue quickstart, but I discovered that auth0-js is a client side library since it uses a lot of 'window'-stuff that is not available on the server-side of Nuxt.

However, I got it kind of working by making it a client-side plugin and wrap all functions (that is calling the authservice in the lifecycle hooks) in a process.client check. It works "kind of" because when going to the protected page whilst not logged in, it flashes the page before being redirected to login page (since its rendered on the server-side as well, but the check only happens once it's delivered on the client side I presume).

My question now is:
What can I do in order to add the check to server-side as well? (or at least make sure that the protected pages isn't flashed before being redirected).

What I've tried so far:

  • Saving the payload and the logged-in state in the store and check in some custom middleware, but that didn't do the trick.

Also, it seems to me that @nuxt/auth is outdated or something and the nuxt auth0 example as well. It uses auth0-lock while I'm using the new auth0 universal.

Anyone have suggestions on how to solve this issue? Thanks in advance!

like image 751
JC97 Avatar asked Jul 14 '19 15:07

JC97


1 Answers

not sure if this will be any help and have only answered a few questions (other account long time ago).


Update.. I read my answer then the question title (I think my answer does cover some of your context), but in regards to the title you could also look at using auth as a plugin. You can then handle stuff there before the page is hit.


I am not sure how your code is implemented, but this may help (hopefully).

If you are not using Vuex, I strong recommend it. Nuxt Vuex Store Guide

// index/store.js
// At least have the store initialized, but its most likely going to be used..
// page.vue
<template>
...
<div v-else-if="!$auth.loggedIn">
  {{ test }}
</div>
...
...

data() {
  if (!this.$auth.loggedIn) {
    const test = 'Only this will load, no flash'
    return { test }
  }
}

$auth.loggedIn is built in, I read it ..somewhere.. in the docs

This will solve the no flash issue, you can also take advantage of a loader screen and asyncData to check the state before rendering the view to avoid a flash and populate data if it hangs.

You could also try using Vuex Actions, I am currently playing with these 2 in the process of where I am now. Learning about nuxtServerInit()

// store/index.js
import axios from 'axios'

export const actions = {
  nuxtServerInit ({commit}, {request}) {
    // This is good if you have the user in your request or other server side stuff
    if (request.user) commit('SET_USER', request.user)
  },
  async GET_USER({ commit }, username) {
    const user = await axios.get(`/user/${username}`)
    if (user) commit('SET_USER', user)
  }
}

export const mutations = {
  SET_USER(state, user) {
    // simple set for now
    state.auth.user = user || null
  }
}

The second one is combined using the fetch() method on the page itself.

// page.vue
async fetch({ $auth, store }) {
  await store.dispatch('GET_USER', $auth.$state.user)
}

Now you can call $auth.user in your code as needed.

$auth.user is another built in I read ..somewhere..

You can also call $auth.user with the $auth.loggedIn to check if user exists on top of being logged in $auth.user && $auth.loggedIn.

It may be this.$auth.<value> depending on where you are trying to reference it.

I learned the asyncData() gets call first and logs in my server, then data() logs values in the server console as well (false, null), but in my Brave console they're undefined, i'd like an answer to that lol

I have been struggling with trying to get Auth0 to work how I wanted with JWTs, but as I kept crawling I found useful bits along the way (even in old demos such as the one you mentioned, just nothing with the lock stuff...). Also in terms of express and my API in general... Anyways, hope this helped (someone).

like image 85
rabbitt Avatar answered Nov 10 '22 05:11

rabbitt