Passport authentication leading to incorrect view redirection in Express

I'm struggling to understand why the URL is updating but leading to the incorrect view.

Once a user is authenticated with passport, the URL changes to my code (/clients) but it redirects back to the homepage view.

After authentication, I want the user to be redirected to my angular app.

folder structure

  server
     --config
          -auth
          -express
          -mongoose
          -passport
          -routes
     --controllers
     --data
     --utils
     --views
         -client (ng app)
             -css
             -js
             -views
             index.jade (angular app index)
         -shared
              layout.jade
         -users
              login.jade
         index.jade (express index)

express.js

app.set('view engine', 'jade');
  app.set('views', config.rootPath + '/server/views/');

auth.js

login: function(req, res, next) {
    var auth = passport.authenticate('local', function(err, user) {
        if (err) return next(err);
        if (!user) {
            req.session.error = 'Invalid Username or Password!';
            res.redirect('/login');
        }

        req.logIn(user, function(err) {
            if (err) return next(err);
            res.redirect('/client');
            console.log();
        })
    });
        auth(req, res, next);
    },...

Routes.js

var auth = require('./auth'),
    controllers = require('../controllers');

module.exports = function(app) {
    app.get('/register', controllers.users.getRegister);
    app.post('/register', controllers.users.createUser);

    app.post('/login', auth.login);
    app.get('/logout', auth.logout);
    app.get('/login', controllers.users.getLogin);

    app.get('/', function (req, res) {
        res.render('index', {currentUser: req.user});
    });

    app.get('*', function (req, res) {
        res.render('index', {currentUser: req.user});
    });
};

If anyone can offer guidance on this issue, I would greatly appreciate it.

Answer №1

Create a new path

   app.get('/customers', function (request, response) {
     response.render('customers/main', {loggedInUser: request.session.user});
   });

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

Creating an Ajax Post request for API invocation

I'm currently setting up an Ajax Post request to interact with an API. Here is a mock curl command for reference: curl -X POST \ --header 'Content-Type: application/json' \ --header 'Accept: application/json' \ --h ...

Guide on modifying values using both input fields and buttons at the same time

I am facing an issue with my input field that is designed to accept numbers. I want the value to be changed both via keyboard input and buttons. The buttons are functioning correctly unless I try to use keyboard input as well. For instance, if I enter 10 ...

Upgrade the package to the latest major version using NPM

When working on a Node.js project, the process of upgrading a package to a major release may seem unclear at first glance. For example, let's say I want to install stylelint: npm install --save stylelint By default, this command adds "stylelin ...

Dealing with API responses in Angular 2

Hello there! I am a beginner in Angular 2 and might ask some basic questions, so please bear with me. I am struggling to understand how to handle an API response. Below is my NodeJS Server API function (which has been checked and is working fine): router ...

Empower the user with the ability to interact through touch on dynamically

I am trying to make a div touchable and draggable. I have dynamically created 1 to 10 divs within another div. Currently, these divs can only be dragged using the mouse or cursor. However, I want to enable dragging through touch as well. Can anyone provi ...

Tips on making a fresh form appear during the registration process

After clicking on a submit button labeled as "continue" within a form, a new form will appear for you to complete in order to continue the registration process. ...

Utilize select2 for dynamically loading an external html component

When the page is loaded, it includes another HTML page with a select element that I want to style using select2. The basic page structure looks like this: <select class="selectAddItem" id="selectAddItem" name="selectAddItem" style="width: 150px;" clas ...

Production environment experiences issues with Angular animations

In my MEAN stack application, I started with Sails.js. Everything was working smoothly during development, especially with angular-animate. However, once I changed the Sails environment to production, I encountered issues. Grunt is set up to concatenate a ...

Execute controller action upon item selection within KODataTable MVC table

I am displaying data in a table using AJAX to call an Action that returns a JSON list. Output: I want each user (row in the table) to be clickable and linkable to an edit page (Admin/Edit/Id). This can be done either by clicking on them or by having an E ...

Unable to launch a stand-alone parse.com server on Debian OS

Have you heard of the standalone version of parse.com service called parse server? Check it out on https://github.com/ParsePlatform/parse-server I recently installed parse server on my Debian 8.3 system and encountered some issues with starting the servic ...

Restricting array elements through union types in TypeScript

Imagine a scenario where we have an event type defined as follows: interface Event { type: 'a' | 'b' | 'c'; value: string; } interface App { elements: Event[]; } Now, consider the following code snippet: const app: App ...

Do you think my approach is foolproof against XSS attacks?

My website has a chat feature and I am wondering if it is protected against XSS attacks. Here is how my method works: To display incoming messages from an AJAX request, I utilize the following jQuery code: $("#message").prepend(req.msg); Although I am a ...

Using raphaeljs and freetransform in conjunction with clip-path

Currently, I am utilizing Raphael along with Elberts FreeTransform Plugin. You can view my progress so far by visiting MyWork The issue arises when I attempt to translate or rotate the set of rectangles within my clip path. Once rotated or translated, it ...

Jade file unable to load js/css files

As a newcomer to jade in my node.js project, I'm facing an issue where no external JavaScript and CSS files are being loaded. My setup includes jade as the template engine, bower_component, angular.js for JavaScript MVC, bootstrap for CSS framework, a ...

Applying hover effect to material-ui IconButton component

As stated in the React Material-UI documentation, I have access to a prop called hoveredStyle: http://www.material-ui.com/#/components/icon-button I intend to utilize the IconButton for two specific purposes: Make use of its tooltip prop for improved ac ...

Converting a 'Functional Component' to a 'Class Component' in React with Material-UI: A Step-by-Step Guide

Can anyone help me convert this Material-UI example into a Class Component? import React from 'react'; import Button from '@material-ui/core/Button'; import Menu from '@material-ui/core/Menu'; import MenuItem from '@mate ...

Node.js encountered an error: Module "express" not found

I just created my first node.js application, but I'm having trouble finding the express library: C:\ChatServer\Server>node server.js module.js:340 throw err; ^ Error: Cannot find module 'express' at Function. ...

tips for applying a where clause on a jsonb column in an included table with sequelize

I currently have 2 tables stored in a postgres database. Table_A -------- id BIGINT metadata JSONB Table_B -------- id BIGINT a_id BIGINT metadata JSONB Data Table_A id | metadata 1 | {'gender': 'Male', 'name': 'xyz&ap ...

Error: unable to locate React scripts

I am encountering an issue when trying to run npm start in a newly created project. Strangely, this problem only occurs within this specific folder (since it's a git repository, it needs to stay here). If I create a new project in a different director ...

Jquery validation is ineffective when it fails to validate

I have implemented real-time jQuery validation for names. It functions correctly in real-time, however, when I attempt to submit the form, it still submits the value even after displaying an error message. Below is the code snippet for the validation: $ ...