UTDallasEPICS / UTDesign-Monitor-Dashboard

new repo, old one borked
0 stars 0 forks source link

adding auth files #15

Closed harshptl05 closed 1 month ago

harshptl05 commented 1 month ago

Added new code in .env file added new layout.js added route.js per documentation

Review the files to make sure it was done right, not fully done, just added what I could with the lab time I had.

FegelSamuel commented 1 month ago

Actually, upon further review, this is dubious because you're using next here instead of nuxt

FegelSamuel commented 1 month ago

To integrate Auth0 with a Nuxt.js application, you can follow these steps:

1. Install dependencies:

You’ll need to install the necessary Auth0 packages and other dependencies to manage authentication.

npm install @nuxtjs/auth-next @nuxtjs/axios

2. Configure @nuxtjs/auth-next module:

You'll need to add the necessary configurations for Auth0 in your nuxt.config.js file.

Modify nuxt.config.js:

export default {
  // Add @nuxtjs/axios and @nuxtjs/auth-next in buildModules or modules
  modules: ['@nuxtjs/axios', '@nuxtjs/auth-next'],

  auth: {
    strategies: {
      auth0: {
        domain: 'YOUR_AUTH0_DOMAIN',
        clientId: 'YOUR_AUTH0_CLIENT_ID',
        audience: 'YOUR_AUTH0_API_IDENTIFIER',  // Optional if using API
        logoutRedirectUri: 'http://localhost:3000' // Redirect after logout
      }
    }
  },

  axios: {
    baseURL: '/', // Or your API URL
  }
}

3. Set up Auth0 in Nuxt pages:

You can use the $auth object to handle authentication (login, logout, etc.).

Example: Login button in a page component

<template>
  <div>
    <button @click="$auth.loginWith('auth0')">Login with Auth0</button>
    <button v-if="$auth.loggedIn" @click="$auth.logout()">Logout</button>
    <p v-if="$auth.loggedIn">Hello, {{ $auth.user.name }}</p>
  </div>
</template>

<script>
export default {
  computed: {
    isAuthenticated() {
      return this.$auth.loggedIn
    },
    user() {
      return this.$auth.user
    }
  }
}
</script>

4. Protecting routes:

If you want to make certain routes accessible only to authenticated users, you can use the auth middleware.

Example: Protected route in nuxt.config.js

export default {
  router: {
    middleware: ['auth']
  }
}

Example: Applying middleware in a specific page component

<template>
  <div>
    <p>Welcome, only logged-in users can see this page!</p>
  </div>
</template>

<script>
export default {
  middleware: 'auth'
}
</script>

5. Handling redirects:

Ensure that your Auth0 settings allow redirect URIs to the Nuxt app. Go to the Auth0 dashboard and navigate to your application settings:


With these steps, you should have a working integration of Auth0 in your Nuxt.js app! Let me know if you need help with specific parts.

FegelSamuel commented 1 month ago

new pr made