Building a Strong Friendship Between Nuxt-auth and Nuxt-i18n

I am facing a challenge while trying to integrate the nuxt@auth module and nuxt-i18n together. The issue arises when I need to have different routes for the login page based on language. For instance:

pages: {
 login: {
  en: '/authentication',
  fr: '/autorisation'
 }
}

The routes are functioning correctly, but I encounter a problem when using nuxt@auth with all pages restricted. The default redirect requests the /login page. In the nuxt.config.js file, I can modify the redirect route, but it only allows me to set one redirect.

auth: {
 redirect: {
  login: '/fr/autorisation'
 }
}

Is there a way to instruct the auth module to request the route based on the selected language? Any help would be greatly appreciated!

Answer №1

I trust this will benefit someone =)

export default ({ app, $auth }) => {
 $auth.onRedirect((to, from) => {
   return app.localePath(to)
  })
}

Answer №2

There seems to be a workaround for the issue mentioned in this link, but unfortunately I am unable to access the onRedirect method in the current release.

To solve this, I implemented a workaround by creating a plugin called auth-lang-redirect.js. This plugin overrides the redirect option specified in the nuxt.config.js file.

export default ({ app }) => {
  var redirect = app.$auth.$storage.options.redirect
  for (var key in redirect) {
    redirect[key] = '/' + app.i18n.locale + redirect[key]
  }
  app.$auth.$storage.options.redirect = redirect
}

It's important to note that I do not utilize the nuxt-i18n module, but the concept remains the same. To use this plugin, you must register it in the nuxt.config.js file like so:

auth: {
    strategies: { ... },
    redirect: {
      login: '/login',
      logout: '/',
      callback: '/login',
      home: '/user/profile'
    },
    plugins: ['@/plugins/auth-lang-redirect.js']
  },

Answer №3

const updateRedirects = ({ app, $auth }) => {
  const originalRedirect = { ...$auth.$storage.options.redirect };

  const modifyRedirects = () => {
    for (const key in originalRedirect) {
      $auth.$storage.options.redirect[key] = app.localePath(originalRedirect[key]);
    }
  };

  modifyRedirects();

  app.i18n.onLanguageChange = () => {
    modifyRedirects();
  };
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble fetching information from a JSON file stored in a local directory while using Angular 7

I am currently experiencing an issue with my code. It works fine when fetching data from a URL, but when I try to read from a local JSON file located in the assets folder, it returns an error. searchData() { const url: any = 'https://jsonplaceholde ...

Sorting tables in Vue.js 2

I am in the process of implementing a sortable table using Vue.js 2. I've successfully generated the table and now I'm seeking guidance on how to implement sorting functionality. Below is an excerpt of my code: <thead> < ...

Learn how to manipulate nested objects and arrays within JSON data by utilizing AJAX and JQuery for seamless access and processing

In my complex data structure, there are nested objects and arrays. How can I efficiently retrieve specific values or keys like 1, 105, and 1055? Here is an example of the data structure: [{"1":{"url":"http:\/\/web.com\/","catname":"HOBBIES ...

Mobile issue: unable to scroll to the top of the page

Despite my efforts in searching and attempting various solutions from SO, I have still been unsuccessful. I have explored options like the iscroll library and setting timeouts, but to no avail. My objective is to enable scrolling to the top of the window/ ...

Retrieving data from JavaScript to populate an HTML list in PHP

So here's the issue in a nutshell: I'm using jQuery on the client side to manipulate some list items within an HTML document. The goal is to store the list items and their respective order into an array, then pass that array into PHP for further ...

Despite my attempts to force a repaint, the progress bar remained static during intensive tasks

My JavaScript method works fine in Chrome, taking about 2000 ms to iterate over ~200 inputs, insert values, and trigger onchange events. However, it's a different story in IE where it takes about 10000 ms. To show the progress of this process, I deci ...

Is it possible to modify a hyperlink title using the named title attribute?

I am currently exploring the possibility of changing the color of a link by polling the title attribute. I have an HTML page that is generated from a script creating an accordion list of hyperlinks. I want to highlight certain hyperlinks and plan to use Ja ...

Utilizing an NPM package in your project

Trying to incorporate a node module called "tagify" into my node.js application has been challenging. Following the instructions in the package's readme file (https://www.npmjs.com/package/@yaireo/tagify#installation), I executed the setup steps as ou ...

How do I locate the scope of a controller that has been loaded in AngularJS?

How can I find the scope of a loaded controller? I want to trigger an event after the scope is initialized in order to predefine the model for the view. Is there a way to listen for $rootScope.$on("$controllerLoaded") ...

When null is multiplied by any number in Typescript, the result should be null, not 0

In C#, I encountered a situation: decimal? x; // number decimal? y; // null c=x*y //c returns null Contrastingly, in TypeScript: let x:number=null; let y:number=12; c=x*y //c returns 0 **How can I achieve null instead of 0 in TypeScript? I'm look ...

Submission event in jQuery fails to trigger

I'm struggling with submitting a form in jQuery <form id="solicitud" action="ventas.php?content=nuevo" method="post"> ... </form> Even though I have the code to submit the form, it's not working as expected $("#nombres").click(fu ...

Interactive Image Viewing Feature Across Multiple Browsers

My attempt to create an image preview effect using JavaScript involves opening any image clicked in full screen mode. The code centers the image based on its size. CSS | View All | Result position: fixed; top: 50%; left: 50%; Simplified Javascript | Vie ...

Glass-pane or angular/HTML overlay on a designated element

Is there a way to create a glass-pane effect, like an hourglass symbol on a semi-transparent layer, within the boundaries of an HTML element E? The goal is to have the glass-pane only overlap the area within E's boundaries. When the glass-pane is ac ...

Ways to retrieve the replacement string using JavaScript

const str = 'Hello! My name is +++Layla+++.'; console.log(str.replace(/\++(.*)\++/, 'X')); In the coding snippet above, I am replacing Layla with X. Is there a way to retrieve the original word Layla in place of X? I want th ...

Improving the performance of Javascript

Currently, I am developing an application with six similar sections on the homepage. Although the app is functional, I find myself duplicating the following block of code: The only variations between each section are the IDs: #grid and #close, as these ID ...

How to send the file path to an API in JavaScript to convert it

I am currently working on a web application that allows users to upload a wav file and convert it to mp3 format. I have successfully implemented the wav to mp3 conversion and saving it to the project folder. However, I am facing an issue with dynamically p ...

"Utilize Ajax to dynamically generate options in a dropdown menu with multiple

I am having trouble populating my multiple dropdown list using AJAX. The dropdown list is dependent on another single selection dropdown list. Here is the HTML code: <div class="form-group"> <label for="InputGender">Select Course</ ...

Placing a div tag directly beneath another div tag

Imagine you have a div element with the class name divstudent, let's call this one div1. Now, you want to dynamically create another div element below div1, but outside of it using JavaScript. How can you achieve this? "div1" <div class="divstuden ...

What is the process of implementing a service in AngularJS?

For my Angular JS project, I am working on creating a module to include my service (file: utils/productos.js) and then load it as a dependency for my ventas module (ventas/ventas.js). However, I encountered the following error: Error: [$injector:nomod] Mo ...

"How can I extract father's details by clicking on a button

After clicking, I need to access the parent element. Here is the HTML code I have: <button mat-icon-button (click)="download($event)"> The generated HTML code is as follows: <button _ngcontent-wsc-c153="" mat-icon-button=&q ...